From 51741ed8b2c401a9f5c22602c52fcc023153ef03 Mon Sep 17 00:00:00 2001 From: hackeryarn Date: Sat, 26 Apr 2025 10:26:15 -0500 Subject: [PATCH 1/2] feat: make to nuon raw option remove all white space --- crates/nu-command/src/filters/uniq.rs | 2 +- crates/nu-command/src/formats/from/nuon.rs | 8 +++++++ crates/nu-command/src/formats/to/nuon.rs | 11 +++++++--- .../tests/commands/database/into_sqlite.rs | 2 +- crates/nu-command/tests/commands/filter.rs | 2 +- crates/nu-command/tests/commands/headers.rs | 2 +- .../nu_plugin_nu_example.nu | 4 ++-- crates/nuon/src/lib.rs | 22 +++++++++---------- crates/nuon/src/to.rs | 20 +++++++++++------ tests/plugins/formats/ini.rs | 2 +- 10 files changed, 47 insertions(+), 28 deletions(-) diff --git a/crates/nu-command/src/filters/uniq.rs b/crates/nu-command/src/filters/uniq.rs index 8042963d75..179353ed1f 100644 --- a/crates/nu-command/src/filters/uniq.rs +++ b/crates/nu-command/src/filters/uniq.rs @@ -218,7 +218,7 @@ fn generate_key(engine_state: &EngineState, item: &ValueCounter) -> Result Value::test_list(vec![Value::test_int(1), Value::test_int(2)]), })), }, + Example { + example: "'{a:1,b:[1,2]}' | from nuon", + description: "Converts raw nuon formatted string to table", + result: Some(Value::test_record(record! { + "a" => Value::test_int(1), + "b" => Value::test_list(vec![Value::test_int(1), Value::test_int(2)]), + })), + }, ] } diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 5e5e505bd8..a5e1464065 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -13,7 +13,7 @@ impl Command for ToNuon { .input_output_types(vec![(Type::Any, Type::String)]) .switch( "raw", - "remove all of the whitespace (default behaviour and overwrites -i and -t)", + "remove all of the whitespace (overwrites -i and -t)", Some('r'), ) .named( @@ -60,7 +60,7 @@ impl Command for ToNuon { } else if let Some(i) = call.get_flag(engine_state, stack, "indent")? { nuon::ToStyle::Spaces(i) } else { - nuon::ToStyle::Raw + nuon::ToStyle::Default }; let span = call.head; @@ -90,12 +90,17 @@ impl Command for ToNuon { 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]")) + 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}")) + }, + Example { + description: "A more complex record with --raw", + example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --raw", + result: Some(Value::test_string("{date:2000-01-01T00:00:00+00:00,data:[1,[2,3],4.56]}")) } ] } diff --git a/crates/nu-command/tests/commands/database/into_sqlite.rs b/crates/nu-command/tests/commands/database/into_sqlite.rs index 93be9f27db..b236faa898 100644 --- a/crates/nu-command/tests/commands/database/into_sqlite.rs +++ b/crates/nu-command/tests/commands/database/into_sqlite.rs @@ -337,7 +337,7 @@ fn into_sqlite_big_insert() { let nuon = nuon::to_nuon( &engine_state, &value, - nuon::ToStyle::Raw, + nuon::ToStyle::Default, Some(Span::unknown()), serialize_types, ) diff --git a/crates/nu-command/tests/commands/filter.rs b/crates/nu-command/tests/commands/filter.rs index a1e8204eaa..c8f7508502 100644 --- a/crates/nu-command/tests/commands/filter.rs +++ b/crates/nu-command/tests/commands/filter.rs @@ -9,7 +9,7 @@ fn filter_with_return_in_closure() { return true }; return false; - } | to nuon --raw + } | to nuon " )); diff --git a/crates/nu-command/tests/commands/headers.rs b/crates/nu-command/tests/commands/headers.rs index 51001f8cd0..aa4ab32964 100644 --- a/crates/nu-command/tests/commands/headers.rs +++ b/crates/nu-command/tests/commands/headers.rs @@ -36,7 +36,7 @@ fn headers_handles_missing_values() { " [{x: a, y: b}, {x: 1, y: 2}, {x: 1, z: 3}] | headers - | to nuon --raw + | to nuon " )); diff --git a/crates/nu_plugin_nu_example/nu_plugin_nu_example.nu b/crates/nu_plugin_nu_example/nu_plugin_nu_example.nu index 9c936007b6..c4b981771e 100755 --- a/crates/nu_plugin_nu_example/nu_plugin_nu_example.nu +++ b/crates/nu_plugin_nu_example/nu_plugin_nu_example.nu @@ -125,7 +125,7 @@ def process_call [ # Use this information to implement your plugin logic # Print the call to stderr, in raw nuon and as a table - $plugin_call | to nuon --raw | print -e + $plugin_call | to nuon | print -e $plugin_call | table -e | print -e # Get the span from the call @@ -264,4 +264,4 @@ def start_plugin [] { }) | each { from json | handle_input } | ignore -} \ No newline at end of file +} diff --git a/crates/nuon/src/lib.rs b/crates/nuon/src/lib.rs index 99525959ab..48166091e4 100644 --- a/crates/nuon/src/lib.rs +++ b/crates/nuon/src/lib.rs @@ -31,7 +31,7 @@ mod tests { assert_eq!(val, m); } assert_eq!( - to_nuon(&engine_state, &val, ToStyle::Raw, None, false).unwrap(), + to_nuon(&engine_state, &val, ToStyle::Default, None, false).unwrap(), input ); } @@ -186,7 +186,7 @@ mod tests { block_id: BlockId::new(0), captures: vec![] }), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -211,7 +211,7 @@ mod tests { to_nuon( &engine_state, &from_nuon("0x[1f ff]", None).unwrap(), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -261,7 +261,7 @@ mod tests { to_nuon( &engine_state, &Value::test_float(1.0), - ToStyle::Raw, + ToStyle::Default, None, false ) @@ -278,7 +278,7 @@ mod tests { to_nuon( &engine_state, &Value::test_float(f64::INFINITY), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -295,7 +295,7 @@ mod tests { to_nuon( &engine_state, &Value::test_float(f64::NEG_INFINITY), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -312,7 +312,7 @@ mod tests { to_nuon( &engine_state, &Value::test_float(-f64::NAN), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -340,7 +340,7 @@ mod tests { "c d" => Value::test_int(6) )) ]), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -357,7 +357,7 @@ mod tests { let res = to_nuon( &engine_state, &Value::test_string(""), - ToStyle::Raw, + ToStyle::Default, None, false, ); @@ -423,7 +423,7 @@ mod tests { "c d" => Value::test_int(6) )) ]), - ToStyle::Raw, + ToStyle::Default, None, false, ) @@ -438,7 +438,7 @@ mod tests { "ro name" => Value::test_string("sam"), "rank" => Value::test_int(10) )), - ToStyle::Raw, + ToStyle::Default, None, false, ) diff --git a/crates/nuon/src/to.rs b/crates/nuon/src/to.rs index fae454cd9a..6cdf14756d 100644 --- a/crates/nuon/src/to.rs +++ b/crates/nuon/src/to.rs @@ -6,9 +6,13 @@ use nu_utils::{escape_quote_string, needs_quoting}; /// control the way Nushell [`Value`] is converted to NUON data pub enum ToStyle { - /// no indentation at all + /// no extra indentation /// /// `{ a: 1, b: 2 }` will be converted to `{a: 1, b: 2}` + Default, + /// no white space at all + /// + /// `{ a: 1, b: 2 }` will be converted to `{a:1,b:2}` Raw, #[allow(clippy::tabs_in_doc_comments)] /// tabulation-based indentation @@ -52,7 +56,8 @@ pub fn to_nuon( let span = span.unwrap_or(Span::unknown()); let indentation = match style { - ToStyle::Raw => None, + ToStyle::Default => None, + ToStyle::Raw => Some("".to_string()), ToStyle::Tabs(t) => Some("\t".repeat(t)), ToStyle::Spaces(s) => Some(" ".repeat(s)), }; @@ -77,7 +82,7 @@ fn value_to_string( indent: Option<&str>, serialize_types: bool, ) -> Result { - let (nl, sep) = get_true_separators(indent); + let (nl, sep, kv_sep) = get_true_separators(indent); let idt = get_true_indentation(depth, indent); let idt_po = get_true_indentation(depth + 1, indent); let idt_pt = get_true_indentation(depth + 2, indent); @@ -211,7 +216,7 @@ fn value_to_string( col }; collection.push(format!( - "{idt_po}{col}: {}", + "{idt_po}{col}:{kv_sep}{}", value_to_string_without_quotes( engine_state, val, @@ -241,10 +246,11 @@ fn get_true_indentation(depth: usize, indent: Option<&str>) -> String { } } -fn get_true_separators(indent: Option<&str>) -> (String, String) { +fn get_true_separators(indent: Option<&str>) -> (String, String, String) { match indent { - Some(_) => ("\n".to_string(), "".to_string()), - None => ("".to_string(), " ".to_string()), + Some("") => ("".to_string(), "".to_string(), "".to_string()), + Some(_) => ("\n".to_string(), "".to_string(), " ".to_string()), + None => ("".to_string(), " ".to_string(), " ".to_string()), } } diff --git a/tests/plugins/formats/ini.rs b/tests/plugins/formats/ini.rs index eee592dac6..e0cf0f2a57 100644 --- a/tests/plugins/formats/ini.rs +++ b/tests/plugins/formats/ini.rs @@ -10,7 +10,7 @@ fn parses_ini() { let actual = nu_with_plugins!( cwd: TEST_CWD, plugin: ("nu_plugin_formats"), - "open sample.ini | to nuon -r" + "open sample.ini | to nuon" ); assert_eq!( From beedf6fa6a82fb6fe185f6a064c4323689fdb89a Mon Sep 17 00:00:00 2001 From: hackeryarn Date: Sun, 27 Apr 2025 09:31:37 -0500 Subject: [PATCH 2/2] remove raw flag from ndnuon --- crates/nu-std/std/assert/mod.nu | 10 +++++----- crates/nu-std/std/formats/mod.nu | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/nu-std/std/assert/mod.nu b/crates/nu-std/std/assert/mod.nu index 297e66ba52..4bfc410e46 100644 --- a/crates/nu-std/std/assert/mod.nu +++ b/crates/nu-std/std/assert/mod.nu @@ -96,8 +96,8 @@ export def equal [left: any, right: any, message?: string] { } text: ( "These are not equal.\n" - + $" Left : '($left | to nuon --raw)'\n" - + $" Right : '($right | to nuon --raw)'" + + $" Left : '($left | to nuon)'\n" + + $" Right : '($right | to nuon)'" ) } } @@ -114,7 +114,7 @@ export def "not equal" [left: any, right: any, message?: string] { start: (metadata $left).span.start end: (metadata $right).span.end } - text: $"These are both '($left | to nuon --raw)'." + text: $"These are both '($left | to nuon)'." } } @@ -210,7 +210,7 @@ export def length [left: list, right: int, message?: string] { } text: ( "This does not have the correct length:\n" - + $" value : ($left | to nuon --raw)\n" + + $" value : ($left | to nuon)\n" + $" length : ($left | core length)\n" + $" expected : ($right)" ) @@ -231,7 +231,7 @@ export def "str contains" [left: string, right: string, message?: string] { } text: ( $"This does not contain '($right)'.\n" - + $" value: ($left | to nuon --raw)" + + $" value: ($left | to nuon)" ) } } diff --git a/crates/nu-std/std/formats/mod.nu b/crates/nu-std/std/formats/mod.nu index 429b867741..43e2a313ca 100644 --- a/crates/nu-std/std/formats/mod.nu +++ b/crates/nu-std/std/formats/mod.nu @@ -36,5 +36,5 @@ export def "from ndnuon" []: [string -> any] { # Convert structured data to newline-delimited NUON (NDNUON) export def "to ndnuon" []: [any -> string] { - each { to nuon --raw | str replace --all "\n" '\n' } | to text + each { to nuon | str replace --all "\n" '\n' } | to text }