1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct ToNuon;
5
6impl Command for ToNuon {
7 fn name(&self) -> &str {
8 "to nuon"
9 }
10
11 fn signature(&self) -> Signature {
12 Signature::build("to nuon")
13 .input_output_types(vec![(Type::Any, Type::String)])
14 .switch(
15 "raw",
16 "Remove all of the whitespace (overwrites -i and -t).",
17 Some('r'),
18 )
19 .named(
20 "indent",
21 SyntaxShape::Number,
22 "Specify indentation width.",
23 Some('i'),
24 )
25 .named(
26 "tabs",
27 SyntaxShape::Number,
28 "Specify indentation tab quantity.",
29 Some('t'),
30 )
31 .switch(
32 "serialize",
33 "Serialize nushell types that cannot be deserialized.",
34 Some('s'),
35 )
36 .switch(
37 "raw-strings",
38 "Use raw string syntax (r#'...'#) for strings with quotes or backslashes.",
39 Some('R'),
40 )
41 .switch(
42 "list-of-records",
43 "Serialize table values as list-of-records instead of table syntax.",
44 Some('l'),
45 )
46 .category(Category::Formats)
47 }
48
49 fn description(&self) -> &str {
50 "Converts table data into Nuon (Nushell Object Notation) text."
51 }
52
53 fn run(
54 &self,
55 engine_state: &EngineState,
56 stack: &mut Stack,
57 call: &Call,
58 mut input: PipelineData,
59 ) -> Result<PipelineData, ShellError> {
60 let serialize_types = call.has_flag(engine_state, stack, "serialize")?;
61 let raw_strings = call.has_flag(engine_state, stack, "raw-strings")?;
62 let list_of_records = call.has_flag(engine_state, stack, "list-of-records")?;
63 let style = if call.has_flag(engine_state, stack, "raw")? {
64 nuon::ToStyle::Raw
65 } else if let Some(t) = call.get_flag(engine_state, stack, "tabs")? {
66 nuon::ToStyle::Tabs(t)
67 } else if let Some(i) = call.get_flag(engine_state, stack, "indent")? {
68 nuon::ToStyle::Spaces(i)
69 } else {
70 nuon::ToStyle::Default
71 };
72
73 let span = call.head;
74 let metadata = input
75 .take_metadata()
76 .unwrap_or_default()
77 .with_content_type(Some("application/x-nuon".into()));
78
79 let value = input.into_value(span)?;
80
81 let config = nuon::ToNuonConfig::default()
82 .style(style)
83 .span(Some(span))
84 .serialize_types(serialize_types)
85 .raw_strings(raw_strings)
86 .list_of_records(list_of_records);
87
88 match nuon::to_nuon(engine_state, &value, config) {
89 Ok(serde_nuon_string) => Ok(Value::string(serde_nuon_string, span)
90 .into_pipeline_data_with_metadata(Some(metadata))),
91 Err(error) => {
92 Ok(Value::error(error, span).into_pipeline_data_with_metadata(Some(metadata)))
93 }
94 }
95 }
96
97 fn examples(&self) -> Vec<Example<'_>> {
98 vec![
99 Example {
100 description: "Outputs a NUON string representing the contents of this list, compact by default.",
101 example: "[1 2 3] | to nuon",
102 result: Some(Value::test_string("[1, 2, 3]")),
103 },
104 Example {
105 description: "Outputs a NUON array of ints, with pretty indentation.",
106 example: "[1 2 3] | to nuon --indent 2",
107 result: Some(Value::test_string("[\n 1,\n 2,\n 3\n]")),
108 },
109 Example {
110 description: "Overwrite any set option with --raw.",
111 example: "[1 2 3] | to nuon --indent 2 --raw",
112 result: Some(Value::test_string("[1,2,3]")),
113 },
114 Example {
115 description: "A more complex record with multiple data types.",
116 example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --indent 2",
117 result: Some(Value::test_string(
118 "{\n date: 2000-01-01T00:00:00+00:00,\n data: [\n 1,\n [\n 2,\n 3\n ],\n 4.56\n ]\n}",
119 )),
120 },
121 Example {
122 description: "A more complex record with --raw.",
123 example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --raw",
124 result: Some(Value::test_string(
125 "{date:2000-01-01T00:00:00+00:00,data:[1,[2,3],4.56]}",
126 )),
127 },
128 Example {
129 description: "Use raw string syntax for strings with quotes or backslashes.",
130 example: r#"'hello "world"' | to nuon --raw-strings"#,
131 result: Some(Value::test_string(r#"r#'hello "world"'#"#)),
132 },
133 Example {
134 description: "Serialize table values as a list of records instead of table syntax.",
135 example: "[[a, b]; [1, 2], [3, 4]] | to nuon --list-of-records",
136 result: Some(Value::test_string("[{a: 1, b: 2}, {a: 3, b: 4}]")),
137 },
138 Example {
139 description: "Serialize table values as list of records with pretty indentation.",
140 example: "[[a, b]; [1, 2], [3, 4]] | to nuon --list-of-records --indent 2",
141 result: Some(Value::test_string("[\n {a: 1, b: 2},\n {a: 3, b: 4}\n]")),
142 },
143 ]
144 }
145}
146
147#[cfg(test)]
148mod test {
149 use super::*;
150 use nu_cmd_lang::eval_pipeline_without_terminal_expression;
151
152 use crate::{Get, Metadata};
153
154 #[test]
155 fn test_examples() -> nu_test_support::Result {
156 use super::ToNuon;
157 nu_test_support::test().examples(ToNuon)
158 }
159
160 #[test]
161 fn test_content_type_metadata() {
162 let mut engine_state = Box::new(EngineState::new());
163 let delta = {
164 let mut working_set = StateWorkingSet::new(&engine_state);
167
168 working_set.add_decl(Box::new(ToNuon {}));
169 working_set.add_decl(Box::new(Metadata {}));
170 working_set.add_decl(Box::new(Get {}));
171
172 working_set.render()
173 };
174
175 engine_state
176 .merge_delta(delta)
177 .expect("Error merging delta");
178
179 let cmd = "{a: 1 b: 2} | to nuon | metadata | get content_type | $in";
180 let result = eval_pipeline_without_terminal_expression(
181 cmd,
182 std::env::temp_dir().as_ref(),
183 &mut engine_state,
184 );
185 assert_eq!(
186 Value::test_string("application/x-nuon"),
187 result.expect("There should be a result")
188 );
189 }
190}