From 52138c0c7ae073e1fb9f3242ac47b6b508984eee Mon Sep 17 00:00:00 2001 From: sholderbach Date: Sat, 20 Apr 2024 19:58:08 +0200 Subject: [PATCH 1/2] Reject incombatible flag combinations in `to nuon` Follow up to #12591 Ensure an error is shown. `.expect` is fine here as the precondition of reaching each branch is explicitly that this flag has been found. Only extra care has to be taken when changing the flag name. But that is the general risk of this stringly typed API. --- crates/nu-command/src/formats/to/nuon.rs | 40 +++++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index e747ac58f6..5c6ce09c8c 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -42,14 +42,38 @@ impl Command for ToNuon { call: &Call, input: PipelineData, ) -> Result { - 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 raw_flag = call.has_flag(engine_state, stack, "raw")?; + let tabs_flag = call.get_flag(engine_state, stack, "tabs")?; + let indent_flag = call.get_flag(engine_state, stack, "indent")?; + let style = match (raw_flag, tabs_flag, indent_flag) { + (true, None, None) => nuon::ToStyle::Raw, + (false, None, None) => nuon::ToStyle::Raw, + (false, None, Some(indent)) => nuon::ToStyle::Spaces(indent), + (false, Some(n_tabs), None) => nuon::ToStyle::Tabs(n_tabs), + (true, None, Some(_)) => { + return Err(ShellError::IncompatibleParameters { + left_message: "can't use `--indent` at the same time".into(), + left_span: call.get_named_arg("indent").expect("has flag").span, + right_message: "because of `--raw`".into(), + right_span: call.get_named_arg("raw").expect("has flag").span, + }); + } + (true, Some(_), _) => { + return Err(ShellError::IncompatibleParameters { + left_message: "can't use `--tabs` at the same time".into(), + left_span: call.get_named_arg("tabs").expect("has flag").span, + right_message: "because of `--raw`".into(), + right_span: call.get_named_arg("raw").expect("has flag").span, + }); + } + (false, Some(_), Some(_)) => { + return Err(ShellError::IncompatibleParameters { + left_message: "can't use `--indent` at the same time".into(), + left_span: call.get_named_arg("indent").expect("has flag").span, + right_message: "because of `--tabs`".into(), + right_span: call.get_named_arg("tabs").expect("has flag").span, + }); + } }; let span = call.head; From f75e6bb4a69ef41fd244f9078dbf1740f47f84d6 Mon Sep 17 00:00:00 2001 From: sholderbach Date: Sat, 20 Apr 2024 20:12:08 +0200 Subject: [PATCH 2/2] Remove example demonstrating old behavior This was introduced as #8366 introduced a logic to have a priority of those flags (and have the `--raw` flag purely to match the flags on `to json`) --- crates/nu-command/src/formats/to/nuon.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 5c6ce09c8c..0ee4b6a754 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -108,11 +108,6 @@ impl Command for ToNuon { example: "[1 2 3] | to nuon --indent 2", result: Some(Value::test_string("[\n 1,\n 2,\n 3\n]")), }, - 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]")) - }, 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",