feat: make to nuon raw option remove all white space

This commit is contained in:
hackeryarn 2025-04-26 10:26:15 -05:00
parent 0389815137
commit 51741ed8b2
No known key found for this signature in database
GPG Key ID: 5F62AEDC40AE8C1C
10 changed files with 47 additions and 28 deletions

View File

@ -218,7 +218,7 @@ fn generate_key(engine_state: &EngineState, item: &ValueCounter) -> Result<Strin
nuon::to_nuon( nuon::to_nuon(
engine_state, engine_state,
&value, &value,
nuon::ToStyle::Raw, nuon::ToStyle::Default,
Some(Span::unknown()), Some(Span::unknown()),
false, false,
) )

View File

@ -35,6 +35,14 @@ impl Command for FromNuon {
"b" => Value::test_list(vec![Value::test_int(1), Value::test_int(2)]), "b" => 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)]),
})),
},
] ]
} }

View File

@ -13,7 +13,7 @@ impl Command for ToNuon {
.input_output_types(vec![(Type::Any, Type::String)]) .input_output_types(vec![(Type::Any, Type::String)])
.switch( .switch(
"raw", "raw",
"remove all of the whitespace (default behaviour and overwrites -i and -t)", "remove all of the whitespace (overwrites -i and -t)",
Some('r'), Some('r'),
) )
.named( .named(
@ -60,7 +60,7 @@ impl Command for ToNuon {
} else if let Some(i) = call.get_flag(engine_state, stack, "indent")? { } else if let Some(i) = call.get_flag(engine_state, stack, "indent")? {
nuon::ToStyle::Spaces(i) nuon::ToStyle::Spaces(i)
} else { } else {
nuon::ToStyle::Raw nuon::ToStyle::Default
}; };
let span = call.head; let span = call.head;
@ -90,12 +90,17 @@ impl Command for ToNuon {
Example { Example {
description: "Overwrite any set option with --raw", description: "Overwrite any set option with --raw",
example: "[1 2 3] | to nuon --indent 2 --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 { Example {
description: "A more complex record with multiple data types", description: "A more complex record with multiple data types",
example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --indent 2", 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}")) 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]}"))
} }
] ]
} }

View File

@ -337,7 +337,7 @@ fn into_sqlite_big_insert() {
let nuon = nuon::to_nuon( let nuon = nuon::to_nuon(
&engine_state, &engine_state,
&value, &value,
nuon::ToStyle::Raw, nuon::ToStyle::Default,
Some(Span::unknown()), Some(Span::unknown()),
serialize_types, serialize_types,
) )

View File

@ -9,7 +9,7 @@ fn filter_with_return_in_closure() {
return true return true
}; };
return false; return false;
} | to nuon --raw } | to nuon
" "
)); ));

View File

@ -36,7 +36,7 @@ fn headers_handles_missing_values() {
" "
[{x: a, y: b}, {x: 1, y: 2}, {x: 1, z: 3}] [{x: a, y: b}, {x: 1, y: 2}, {x: 1, z: 3}]
| headers | headers
| to nuon --raw | to nuon
" "
)); ));

View File

@ -125,7 +125,7 @@ def process_call [
# Use this information to implement your plugin logic # Use this information to implement your plugin logic
# Print the call to stderr, in raw nuon and as a table # 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 $plugin_call | table -e | print -e
# Get the span from the call # Get the span from the call

View File

@ -31,7 +31,7 @@ mod tests {
assert_eq!(val, m); assert_eq!(val, m);
} }
assert_eq!( assert_eq!(
to_nuon(&engine_state, &val, ToStyle::Raw, None, false).unwrap(), to_nuon(&engine_state, &val, ToStyle::Default, None, false).unwrap(),
input input
); );
} }
@ -186,7 +186,7 @@ mod tests {
block_id: BlockId::new(0), block_id: BlockId::new(0),
captures: vec![] captures: vec![]
}), }),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -211,7 +211,7 @@ mod tests {
to_nuon( to_nuon(
&engine_state, &engine_state,
&from_nuon("0x[1f ff]", None).unwrap(), &from_nuon("0x[1f ff]", None).unwrap(),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -261,7 +261,7 @@ mod tests {
to_nuon( to_nuon(
&engine_state, &engine_state,
&Value::test_float(1.0), &Value::test_float(1.0),
ToStyle::Raw, ToStyle::Default,
None, None,
false false
) )
@ -278,7 +278,7 @@ mod tests {
to_nuon( to_nuon(
&engine_state, &engine_state,
&Value::test_float(f64::INFINITY), &Value::test_float(f64::INFINITY),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -295,7 +295,7 @@ mod tests {
to_nuon( to_nuon(
&engine_state, &engine_state,
&Value::test_float(f64::NEG_INFINITY), &Value::test_float(f64::NEG_INFINITY),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -312,7 +312,7 @@ mod tests {
to_nuon( to_nuon(
&engine_state, &engine_state,
&Value::test_float(-f64::NAN), &Value::test_float(-f64::NAN),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -340,7 +340,7 @@ mod tests {
"c d" => Value::test_int(6) "c d" => Value::test_int(6)
)) ))
]), ]),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -357,7 +357,7 @@ mod tests {
let res = to_nuon( let res = to_nuon(
&engine_state, &engine_state,
&Value::test_string(""), &Value::test_string(""),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
); );
@ -423,7 +423,7 @@ mod tests {
"c d" => Value::test_int(6) "c d" => Value::test_int(6)
)) ))
]), ]),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )
@ -438,7 +438,7 @@ mod tests {
"ro name" => Value::test_string("sam"), "ro name" => Value::test_string("sam"),
"rank" => Value::test_int(10) "rank" => Value::test_int(10)
)), )),
ToStyle::Raw, ToStyle::Default,
None, None,
false, false,
) )

View File

@ -6,9 +6,13 @@ use nu_utils::{escape_quote_string, needs_quoting};
/// control the way Nushell [`Value`] is converted to NUON data /// control the way Nushell [`Value`] is converted to NUON data
pub enum ToStyle { pub enum ToStyle {
/// no indentation at all /// no extra indentation
/// ///
/// `{ a: 1, b: 2 }` will be converted to `{a: 1, b: 2}` /// `{ 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, Raw,
#[allow(clippy::tabs_in_doc_comments)] #[allow(clippy::tabs_in_doc_comments)]
/// tabulation-based indentation /// tabulation-based indentation
@ -52,7 +56,8 @@ pub fn to_nuon(
let span = span.unwrap_or(Span::unknown()); let span = span.unwrap_or(Span::unknown());
let indentation = match style { let indentation = match style {
ToStyle::Raw => None, ToStyle::Default => None,
ToStyle::Raw => Some("".to_string()),
ToStyle::Tabs(t) => Some("\t".repeat(t)), ToStyle::Tabs(t) => Some("\t".repeat(t)),
ToStyle::Spaces(s) => Some(" ".repeat(s)), ToStyle::Spaces(s) => Some(" ".repeat(s)),
}; };
@ -77,7 +82,7 @@ fn value_to_string(
indent: Option<&str>, indent: Option<&str>,
serialize_types: bool, serialize_types: bool,
) -> Result<String, ShellError> { ) -> Result<String, ShellError> {
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 = get_true_indentation(depth, indent);
let idt_po = get_true_indentation(depth + 1, indent); let idt_po = get_true_indentation(depth + 1, indent);
let idt_pt = get_true_indentation(depth + 2, indent); let idt_pt = get_true_indentation(depth + 2, indent);
@ -211,7 +216,7 @@ fn value_to_string(
col col
}; };
collection.push(format!( collection.push(format!(
"{idt_po}{col}: {}", "{idt_po}{col}:{kv_sep}{}",
value_to_string_without_quotes( value_to_string_without_quotes(
engine_state, engine_state,
val, 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 { match indent {
Some(_) => ("\n".to_string(), "".to_string()), Some("") => ("".to_string(), "".to_string(), "".to_string()),
None => ("".to_string(), " ".to_string()), Some(_) => ("\n".to_string(), "".to_string(), " ".to_string()),
None => ("".to_string(), " ".to_string(), " ".to_string()),
} }
} }

View File

@ -10,7 +10,7 @@ fn parses_ini() {
let actual = nu_with_plugins!( let actual = nu_with_plugins!(
cwd: TEST_CWD, cwd: TEST_CWD,
plugin: ("nu_plugin_formats"), plugin: ("nu_plugin_formats"),
"open sample.ini | to nuon -r" "open sample.ini | to nuon"
); );
assert_eq!( assert_eq!(