add "to nuon" enumeration of possible styles (#12591)

# Description
in order to change the style of the _serialized_ NUON data,
`nuon::to_nuon` takes three mutually exclusive arguments, `raw: bool`,
`tabs: Option<usize>` and `indent: Option<usize>` 🤔
this begs to use an enumeration with all possible alternatives, right?

this PR changes the signature of `nuon::to_nuon` to use `nuon::ToStyle`
which has three variants
- `Raw`: no newlines
- `Tabs(n: usize)`: newlines and `n` tabulations as indent
- `Spaces(n: usize)`: newlines and `n` spaces as indent

# User-Facing Changes
the signature of `nuon::to_nuon` changes from
```rust
to_nuon(
    input: &Value,
    raw: bool,
    tabs: Option<usize>,
    indent: Option<usize>,
    span: Option<Span>,
) -> Result<String, ShellError>
```
to
```rust
to_nuon(
    input: &Value,
    style: ToStyle,
    span: Option<Span>
) -> Result<String, ShellError>
```

# Tests + Formatting

# After Submitting
This commit is contained in:
Antoine Stevan
2024-04-20 11:40:52 +02:00
committed by GitHub
parent 187b87c61c
commit be5ed3290c
5 changed files with 62 additions and 62 deletions

View File

@ -215,7 +215,7 @@ fn sort_attributes(val: Value) -> Value {
fn generate_key(item: &ValueCounter) -> Result<String, ShellError> {
let value = sort_attributes(item.val_to_compare.clone()); //otherwise, keys could be different for Records
nuon::to_nuon(&value, true, None, None, Some(Span::unknown()))
nuon::to_nuon(&value, nuon::ToStyle::Raw, Some(Span::unknown()))
}
fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Vec<Value> {

View File

@ -42,14 +42,20 @@ impl Command for ToNuon {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let raw = call.has_flag(engine_state, stack, "raw")?;
let tabs: Option<usize> = call.get_flag(engine_state, stack, "tabs")?;
let indent: Option<usize> = call.get_flag(engine_state, stack, "indent")?;
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(&value, raw, tabs, indent, Some(span)) {
match nuon::to_nuon(&value, style, Some(span)) {
Ok(serde_nuon_string) => {
Ok(Value::string(serde_nuon_string, span).into_pipeline_data())
}

View File

@ -330,7 +330,7 @@ fn into_sqlite_big_insert() {
)
.unwrap();
let nuon = nuon::to_nuon(&value, true, None, None, Some(Span::unknown())).unwrap()
let nuon = nuon::to_nuon(&value, nuon::ToStyle::Raw, Some(Span::unknown())).unwrap()
+ &line_ending();
nuon_file.write_all(nuon.as_bytes()).unwrap();