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(
engine_state,
&value,
nuon::ToStyle::Raw,
nuon::ToStyle::Default,
Some(Span::unknown()),
false,
)

View File

@ -35,6 +35,14 @@ impl Command for FromNuon {
"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)])
.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]}"))
}
]
}

View File

@ -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,
)

View File

@ -9,7 +9,7 @@ fn filter_with_return_in_closure() {
return true
};
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}]
| headers
| to nuon --raw
| to nuon
"
));

View File

@ -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
}
}

View File

@ -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,
)

View File

@ -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<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_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()),
}
}

View File

@ -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!(