mirror of
https://github.com/nushell/nushell.git
synced 2025-05-28 10:31:23 +00:00
# Description This PR introduces a switch `--serialize` that allows serializing of types that cannot be deserialized. Right now it only serializes closures as strings in `to toml`, `to json`, `to nuon`, `to text`, some indirect `to html` and `to yaml`. A lot of the changes are just weaving the engine_state through calling functions and the rest is just repetitive way of getting the closure block span and grabbing the span's text. In places where it has to report `<Closure 123>` I changed it to `closure_123`. It always seemed like the `<>` were not very nushell-y. This is still a breaking change. I think this could also help with systematic translation of old config to new config file. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
156 lines
5.1 KiB
Rust
156 lines
5.1 KiB
Rust
use nu_engine::command_prelude::*;
|
|
|
|
#[derive(Clone)]
|
|
pub struct ToNuon;
|
|
|
|
impl Command for ToNuon {
|
|
fn name(&self) -> &str {
|
|
"to nuon"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("to nuon")
|
|
.input_output_types(vec![(Type::Any, Type::String)])
|
|
.switch(
|
|
"raw",
|
|
"remove all of the whitespace (default behaviour and overwrites -i and -t)",
|
|
Some('r'),
|
|
)
|
|
.named(
|
|
"indent",
|
|
SyntaxShape::Number,
|
|
"specify indentation width",
|
|
Some('i'),
|
|
)
|
|
.named(
|
|
"tabs",
|
|
SyntaxShape::Number,
|
|
"specify indentation tab quantity",
|
|
Some('t'),
|
|
)
|
|
.switch(
|
|
"serialize",
|
|
"serialize nushell types that cannot be deserialized",
|
|
Some('s'),
|
|
)
|
|
.category(Category::Formats)
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Converts table data into Nuon (Nushell Object Notation) text."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let metadata = input
|
|
.metadata()
|
|
.unwrap_or_default()
|
|
.with_content_type(Some("application/x-nuon".into()));
|
|
|
|
let serialize_types = call.has_flag(engine_state, stack, "serialize")?;
|
|
let style = if call.has_flag(engine_state, stack, "raw")? {
|
|
nuon::ToStyle::Raw
|
|
} else if let Some(t) = call.get_flag(engine_state, stack, "tabs")? {
|
|
nuon::ToStyle::Tabs(t)
|
|
} else if let Some(i) = call.get_flag(engine_state, stack, "indent")? {
|
|
nuon::ToStyle::Spaces(i)
|
|
} else {
|
|
nuon::ToStyle::Raw
|
|
};
|
|
|
|
let span = call.head;
|
|
let value = input.into_value(span)?;
|
|
|
|
match nuon::to_nuon(engine_state, &value, style, Some(span), serialize_types) {
|
|
Ok(serde_nuon_string) => Ok(Value::string(serde_nuon_string, span)
|
|
.into_pipeline_data_with_metadata(Some(metadata))),
|
|
_ => Ok(Value::error(
|
|
ShellError::CantConvert {
|
|
to_type: "NUON".into(),
|
|
from_type: value.get_type().to_string(),
|
|
span,
|
|
help: None,
|
|
},
|
|
span,
|
|
)
|
|
.into_pipeline_data_with_metadata(Some(metadata))),
|
|
}
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Outputs a NUON string representing the contents of this list, compact by default",
|
|
example: "[1 2 3] | to nuon",
|
|
result: Some(Value::test_string("[1, 2, 3]"))
|
|
},
|
|
Example {
|
|
description: "Outputs a NUON array of ints, with pretty indentation",
|
|
example: "[1 2 3] | to nuon --indent 2",
|
|
result: Some(Value::test_string("[\n 1,\n 2,\n 3\n]")),
|
|
},
|
|
Example {
|
|
description: "Overwrite any set option with --raw",
|
|
example: "[1 2 3] | to nuon --indent 2 --raw",
|
|
result: Some(Value::test_string("[1, 2, 3]"))
|
|
},
|
|
Example {
|
|
description: "A more complex record with multiple data types",
|
|
example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --indent 2",
|
|
result: Some(Value::test_string("{\n date: 2000-01-01T00:00:00+00:00,\n data: [\n 1,\n [\n 2,\n 3\n ],\n 4.56\n ]\n}"))
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use nu_cmd_lang::eval_pipeline_without_terminal_expression;
|
|
|
|
use crate::{Get, Metadata};
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use super::ToNuon;
|
|
use crate::test_examples;
|
|
test_examples(ToNuon {})
|
|
}
|
|
|
|
#[test]
|
|
fn test_content_type_metadata() {
|
|
let mut engine_state = Box::new(EngineState::new());
|
|
let delta = {
|
|
// Base functions that are needed for testing
|
|
// Try to keep this working set small to keep tests running as fast as possible
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
working_set.add_decl(Box::new(ToNuon {}));
|
|
working_set.add_decl(Box::new(Metadata {}));
|
|
working_set.add_decl(Box::new(Get {}));
|
|
|
|
working_set.render()
|
|
};
|
|
|
|
engine_state
|
|
.merge_delta(delta)
|
|
.expect("Error merging delta");
|
|
|
|
let cmd = "{a: 1 b: 2} | to nuon | metadata | get content_type";
|
|
let result = eval_pipeline_without_terminal_expression(
|
|
cmd,
|
|
std::env::temp_dir().as_ref(),
|
|
&mut engine_state,
|
|
);
|
|
assert_eq!(
|
|
Value::test_record(record!("content_type" => Value::test_string("application/x-nuon"))),
|
|
result.expect("There should be a result")
|
|
);
|
|
}
|
|
}
|