From 7a3cbf43e8299a7dcd6c1035a1c8539f4f523dbf Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Tue, 7 Nov 2023 14:25:32 -0800 Subject: [PATCH] Convert ShellError::UnsupportedInput to named fields (#10971) # Description This is easy to do with rust-analyzer, but I didn't want to just pump these all out without feedback. Part of #10700 # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting N/A --------- Co-authored-by: Stefan Holderbach --- .../src/dataframe/expressions/datepart.rs | 12 +-- .../src/dataframe/series/indexes/arg_true.rs | 12 +-- .../values/nu_dataframe/conversion.rs | 48 ++++++------ .../src/dataframe/values/nu_expression/mod.rs | 12 +-- crates/nu-cmd-extra/src/extra/bits/not.rs | 12 +-- .../src/extra/bits/rotate_left.rs | 12 +-- .../src/extra/bits/rotate_right.rs | 12 +-- .../nu-cmd-extra/src/extra/bits/shift_left.rs | 12 +-- .../src/extra/bits/shift_right.rs | 12 +-- .../nu-cmd-extra/src/extra/filters/rotate.rs | 13 ++-- .../src/extra/formats/from/url.rs | 12 +-- crates/nu-cmd-extra/src/extra/math/arccos.rs | 13 ++-- crates/nu-cmd-extra/src/extra/math/arccosh.rs | 12 +-- crates/nu-cmd-extra/src/extra/math/arcsin.rs | 13 ++-- crates/nu-cmd-extra/src/extra/math/arctanh.rs | 13 ++-- crates/nu-cmd-extra/src/extra/math/ln.rs | 12 +-- .../src/extra/strings/encode_decode/hex.rs | 16 +--- crates/nu-command/src/bytes/at.rs | 13 ++-- .../nu-command/src/charting/hashable_value.rs | 12 +-- crates/nu-command/src/charting/histogram.rs | 9 +-- crates/nu-command/src/env/load_env.rs | 12 +-- crates/nu-command/src/filters/find.rs | 13 ++-- crates/nu-command/src/filters/flatten.rs | 14 +--- crates/nu-command/src/filters/join.rs | 24 +++--- crates/nu-command/src/filters/rename.rs | 11 +-- crates/nu-command/src/formats/from/ods.rs | 36 +++++---- crates/nu-command/src/formats/from/xlsx.rs | 36 +++++---- crates/nu-command/src/formats/from/yaml.rs | 39 +++++----- crates/nu-command/src/formats/to/delimited.rs | 12 +-- crates/nu-command/src/formats/to/nuon.rs | 72 +++++++++--------- crates/nu-command/src/formats/to/toml.rs | 12 +-- crates/nu-command/src/math/log.rs | 24 +++--- crates/nu-command/src/math/median.rs | 40 +++++----- crates/nu-command/src/math/mode.rs | 12 +-- crates/nu-command/src/math/reducers.rs | 73 +++++++++---------- crates/nu-command/src/math/sqrt.rs | 12 +-- crates/nu-command/src/math/utils.rs | 26 +++---- crates/nu-command/src/math/variance.rs | 13 ++-- crates/nu-command/src/network/http/client.rs | 9 +-- .../nu-command/src/network/url/build_query.rs | 24 +++--- crates/nu-command/src/network/url/join.rs | 12 +-- crates/nu-command/src/network/url/parse.rs | 24 +++--- crates/nu-command/src/path/join.rs | 21 ++---- .../nu-command/src/platform/input/input_.rs | 12 +-- .../src/platform/input/input_listen.rs | 24 +++--- .../src/strings/encode_decode/base64.rs | 13 ++-- .../src/strings/encode_decode/decode.rs | 12 +-- .../src/strings/encode_decode/encode.rs | 12 +-- .../src/strings/encode_decode/encoding.rs | 12 +-- .../nu-command/src/strings/str_/substring.rs | 13 ++-- .../nu-command/src/strings/str_/trim/trim_.rs | 21 +++--- crates/nu-protocol/src/shell_error.rs | 14 ++-- crates/nu-protocol/src/value/mod.rs | 51 ++++++------- crates/nu_plugin_formats/src/from/ics.rs | 12 +-- crates/nu_plugin_formats/src/from/ini.rs | 12 +-- crates/nu_plugin_formats/src/from/vcf.rs | 12 +-- 56 files changed, 506 insertions(+), 567 deletions(-) diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs index 1372ad7350..39b981d02c 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs @@ -124,12 +124,12 @@ impl Command for ExprDatePart { "microsecond" => expr_dt.microsecond(), "nanosecond" => expr_dt.nanosecond(), _ => { - return Err(ShellError::UnsupportedInput( - format!("{} is not a valid datepart, expected one of year, month, day, hour, minute, second, millisecond, microsecond, nanosecond", part.item), - "value originates from here".to_string(), - call.head, - part.span, - )); + return Err(ShellError::UnsupportedInput { + msg: format!("{} is not a valid datepart, expected one of year, month, day, hour, minute, second, millisecond, microsecond, nanosecond", part.item), + input: "value originates from here".to_string(), + msg_span: call.head, + input_span: part.span, + }); } }.into(); diff --git a/crates/nu-cmd-dataframe/src/dataframe/series/indexes/arg_true.rs b/crates/nu-cmd-dataframe/src/dataframe/series/indexes/arg_true.rs index 3a97c152a0..e81800cf87 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/series/indexes/arg_true.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/series/indexes/arg_true.rs @@ -98,12 +98,12 @@ fn command( let value = NuDataFrame::dataframe_into_value(res, call.head); Ok(PipelineData::Value(value, None)) } - _ => Err(ShellError::UnsupportedInput( - "Expected the dataframe to have a column".to_string(), - "".to_string(), - call.head, - call.head, - )), + _ => Err(ShellError::UnsupportedInput { + msg: "Expected the dataframe to have a column".to_string(), + input: "".to_string(), + msg_span: call.head, + input_span: call.head, + }), } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/conversion.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/conversion.rs index d52ea1925f..028d73a146 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/conversion.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/conversion.rs @@ -855,13 +855,13 @@ fn series_to_values( Some(val) => val, None => { return Value::error( - ShellError::UnsupportedInput( - "The given local datetime representation is invalid." + ShellError::UnsupportedInput { + msg: "The given local datetime representation is invalid." .to_string(), - format!("timestamp is {a:?}"), - span, - Span::unknown(), - ), + input: format!("timestamp is {a:?}"), + msg_span: span, + input_span: Span::unknown(), + }, span, ) } @@ -871,13 +871,13 @@ fn series_to_values( Some(val) => val, None => { return Value::error( - ShellError::UnsupportedInput( - "The given local datetime representation is invalid." + ShellError::UnsupportedInput { + msg: "The given local datetime representation is invalid." .to_string(), - format!("timestamp is {a:?}"), - span, - Span::unknown(), - ), + input: format!("timestamp is {a:?}"), + msg_span: span, + input_span: Span::unknown(), + }, span, ) } @@ -923,13 +923,13 @@ fn series_to_values( Some(val) => val, None => { return Value::error( - ShellError::UnsupportedInput( - "The given local datetime representation is invalid." + ShellError::UnsupportedInput { + msg: "The given local datetime representation is invalid." .to_string(), - format!("timestamp is {a:?}"), - span, - Span::unknown(), - ), + input: format!("timestamp is {a:?}"), + msg_span: span, + input_span: Span::unknown(), + }, span, ) } @@ -939,13 +939,13 @@ fn series_to_values( Some(val) => val, None => { return Value::error( - ShellError::UnsupportedInput( - "The given local datetime representation is invalid." + ShellError::UnsupportedInput { + msg: "The given local datetime representation is invalid." .to_string(), - format!("timestamp is {a:?}"), - span, - Span::unknown(), - ), + input: format!("timestamp is {a:?}"), + msg_span: span, + input_span: Span::unknown(), + }, span, ) } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/mod.rs index dc2f7b48ae..238ba0a1ab 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/mod.rs @@ -427,11 +427,11 @@ pub fn expr_to_value(expr: &Expr, span: Span) -> Result { } // the parameter polars_plan::dsl::selector::Selector is not publicly exposed. // I am not sure what we can meaningfully do with this at this time. - Expr::Selector(_) => Err(ShellError::UnsupportedInput( - "Expressions of type Selector to Nu Values is not yet supported".to_string(), - format!("Expression is {expr:?}"), - span, - Span::unknown(), - )), + Expr::Selector(_) => Err(ShellError::UnsupportedInput { + msg: "Expressions of type Selector to Nu Values is not yet supported".to_string(), + input: format!("Expression is {expr:?}"), + msg_span: span, + input_span: Span::unknown(), + }), } } diff --git a/crates/nu-cmd-extra/src/extra/bits/not.rs b/crates/nu-cmd-extra/src/extra/bits/not.rs index 61025d27bf..82d822e9ae 100644 --- a/crates/nu-cmd-extra/src/extra/bits/not.rs +++ b/crates/nu-cmd-extra/src/extra/bits/not.rs @@ -60,12 +60,12 @@ impl Command for BitsNot { let bytes_len = get_number_bytes(number_bytes.as_ref()); if let NumberBytes::Invalid = bytes_len { if let Some(val) = number_bytes { - return Err(ShellError::UnsupportedInput( - "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), - "value originates from here".to_string(), - head, - val.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), + input: "value originates from here".to_string(), + msg_span: head, + input_span: val.span, + }); } } diff --git a/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs b/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs index 5eec68d46b..e76447dbb0 100644 --- a/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs +++ b/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs @@ -63,12 +63,12 @@ impl Command for BitsRol { let bytes_len = get_number_bytes(number_bytes.as_ref()); if let NumberBytes::Invalid = bytes_len { if let Some(val) = number_bytes { - return Err(ShellError::UnsupportedInput( - "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), - "value originates from here".to_string(), - head, - val.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), + input: "value originates from here".to_string(), + msg_span: head, + input_span: val.span, + }); } } // This doesn't match explicit nulls diff --git a/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs b/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs index 18942db674..8b73efea52 100644 --- a/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs +++ b/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs @@ -63,12 +63,12 @@ impl Command for BitsRor { let bytes_len = get_number_bytes(number_bytes.as_ref()); if let NumberBytes::Invalid = bytes_len { if let Some(val) = number_bytes { - return Err(ShellError::UnsupportedInput( - "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), - "value originates from here".to_string(), - head, - val.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), + input: "value originates from here".to_string(), + msg_span: head, + input_span: val.span, + }); } } // This doesn't match explicit nulls diff --git a/crates/nu-cmd-extra/src/extra/bits/shift_left.rs b/crates/nu-cmd-extra/src/extra/bits/shift_left.rs index b2a46d687d..362cbdc217 100644 --- a/crates/nu-cmd-extra/src/extra/bits/shift_left.rs +++ b/crates/nu-cmd-extra/src/extra/bits/shift_left.rs @@ -63,12 +63,12 @@ impl Command for BitsShl { let bytes_len = get_number_bytes(number_bytes.as_ref()); if let NumberBytes::Invalid = bytes_len { if let Some(val) = number_bytes { - return Err(ShellError::UnsupportedInput( - "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), - "value originates from here".to_string(), - head, - val.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), + input: "value originates from here".to_string(), + msg_span: head, + input_span: val.span, + }); } } // This doesn't match explicit nulls diff --git a/crates/nu-cmd-extra/src/extra/bits/shift_right.rs b/crates/nu-cmd-extra/src/extra/bits/shift_right.rs index f3bf66cf71..5d8931e04c 100644 --- a/crates/nu-cmd-extra/src/extra/bits/shift_right.rs +++ b/crates/nu-cmd-extra/src/extra/bits/shift_right.rs @@ -63,12 +63,12 @@ impl Command for BitsShr { let bytes_len = get_number_bytes(number_bytes.as_ref()); if let NumberBytes::Invalid = bytes_len { if let Some(val) = number_bytes { - return Err(ShellError::UnsupportedInput( - "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), - "value originates from here".to_string(), - head, - val.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(), + input: "value originates from here".to_string(), + msg_span: head, + input_span: val.span, + }); } } // This doesn't match explicit nulls diff --git a/crates/nu-cmd-extra/src/extra/filters/rotate.rs b/crates/nu-cmd-extra/src/extra/filters/rotate.rs index 44aaca2b41..2340d71b12 100644 --- a/crates/nu-cmd-extra/src/extra/filters/rotate.rs +++ b/crates/nu-cmd-extra/src/extra/filters/rotate.rs @@ -200,13 +200,12 @@ pub fn rotate( } } } else { - return Err(ShellError::UnsupportedInput( - "list input is empty".to_string(), - "value originates from here".into(), - call.head, - // TODO: Maybe make all Pipelines have spans, so that this doesn't need to be unwrapped. - span.unwrap_or(call.head), - )); + return Err(ShellError::UnsupportedInput { + msg: "list input is empty".to_string(), + input: "value originates from here".into(), + msg_span: call.head, + input_span: span.unwrap_or(call.head), + }); } let total_columns = &old_column_names.len(); diff --git a/crates/nu-cmd-extra/src/extra/formats/from/url.rs b/crates/nu-cmd-extra/src/extra/formats/from/url.rs index 1717dddcd4..fca1e0209c 100644 --- a/crates/nu-cmd-extra/src/extra/formats/from/url.rs +++ b/crates/nu-cmd-extra/src/extra/formats/from/url.rs @@ -61,12 +61,12 @@ fn from_url(input: PipelineData, head: Span) -> Result Ok(PipelineData::Value(Value::record(record, head), metadata)) } - _ => Err(ShellError::UnsupportedInput( - "String not compatible with URL encoding".to_string(), - "value originates from here".into(), - head, - span, - )), + _ => Err(ShellError::UnsupportedInput { + msg: "String not compatible with URL encoding".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }), } } diff --git a/crates/nu-cmd-extra/src/extra/math/arccos.rs b/crates/nu-cmd-extra/src/extra/math/arccos.rs index b1bdcf363f..ec0dfa89ef 100644 --- a/crates/nu-cmd-extra/src/extra/math/arccos.rs +++ b/crates/nu-cmd-extra/src/extra/math/arccos.rs @@ -84,12 +84,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { Value::float(val, span) } else { Value::error( - ShellError::UnsupportedInput( - "'arccos' undefined for values outside the closed interval [-1, 1].".into(), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: "'arccos' undefined for values outside the closed interval [-1, 1]." + .into(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ) } diff --git a/crates/nu-cmd-extra/src/extra/math/arccosh.rs b/crates/nu-cmd-extra/src/extra/math/arccosh.rs index d5f2244535..f91f606d34 100644 --- a/crates/nu-cmd-extra/src/extra/math/arccosh.rs +++ b/crates/nu-cmd-extra/src/extra/math/arccosh.rs @@ -74,12 +74,12 @@ fn operate(value: Value, head: Span) -> Value { Value::float(val, span) } else { Value::error( - ShellError::UnsupportedInput( - "'arccosh' undefined for values below 1.".into(), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: "'arccosh' undefined for values below 1.".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ) } diff --git a/crates/nu-cmd-extra/src/extra/math/arcsin.rs b/crates/nu-cmd-extra/src/extra/math/arcsin.rs index a9f943ded3..766417e8e6 100644 --- a/crates/nu-cmd-extra/src/extra/math/arcsin.rs +++ b/crates/nu-cmd-extra/src/extra/math/arcsin.rs @@ -85,12 +85,13 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { Value::float(val, span) } else { Value::error( - ShellError::UnsupportedInput( - "'arcsin' undefined for values outside the closed interval [-1, 1].".into(), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: "'arcsin' undefined for values outside the closed interval [-1, 1]." + .into(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ) } diff --git a/crates/nu-cmd-extra/src/extra/math/arctanh.rs b/crates/nu-cmd-extra/src/extra/math/arctanh.rs index 0bb2f4d4a9..69ad6b86a9 100644 --- a/crates/nu-cmd-extra/src/extra/math/arctanh.rs +++ b/crates/nu-cmd-extra/src/extra/math/arctanh.rs @@ -74,12 +74,13 @@ fn operate(value: Value, head: Span) -> Value { Value::float(val, span) } else { Value::error( - ShellError::UnsupportedInput( - "'arctanh' undefined for values outside the open interval (-1, 1).".into(), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: "'arctanh' undefined for values outside the open interval (-1, 1)." + .into(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, head, ) } diff --git a/crates/nu-cmd-extra/src/extra/math/ln.rs b/crates/nu-cmd-extra/src/extra/math/ln.rs index 24d261cdc0..01fba009f4 100644 --- a/crates/nu-cmd-extra/src/extra/math/ln.rs +++ b/crates/nu-cmd-extra/src/extra/math/ln.rs @@ -74,12 +74,12 @@ fn operate(value: Value, head: Span) -> Value { Value::float(val, span) } else { Value::error( - ShellError::UnsupportedInput( - "'ln' undefined for values outside the open interval (0, Inf).".into(), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: "'ln' undefined for values outside the open interval (0, Inf).".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ) } diff --git a/crates/nu-cmd-extra/src/extra/strings/encode_decode/hex.rs b/crates/nu-cmd-extra/src/extra/strings/encode_decode/hex.rs index fad4521fce..7707b55fbe 100644 --- a/crates/nu-cmd-extra/src/extra/strings/encode_decode/hex.rs +++ b/crates/nu-cmd-extra/src/extra/strings/encode_decode/hex.rs @@ -108,26 +108,14 @@ fn action( Value::Binary { val, .. } => match hex_config.action_type { ActionType::Encode => Value::string(hex_encode(val.as_ref()), command_span), ActionType::Decode => Value::error( - ShellError::UnsupportedInput( - "Binary data can only be encoded".to_string(), - "value originates from here".into(), - command_span, - // This line requires the Value::Error {} match above. - input.span(), - ), + ShellError::UnsupportedInput { msg: "Binary data can only be encoded".to_string(), input: "value originates from here".into(), msg_span: command_span, input_span: input.span() }, command_span, ), }, Value::String { val, .. } => { match hex_config.action_type { ActionType::Encode => Value::error( - ShellError::UnsupportedInput( - "String value can only be decoded".to_string(), - "value originates from here".into(), - command_span, - // This line requires the Value::Error {} match above. - input.span(), - ), + ShellError::UnsupportedInput { msg: "String value can only be decoded".to_string(), input: "value originates from here".into(), msg_span: command_span, input_span: input.span() }, command_span, ), diff --git a/crates/nu-command/src/bytes/at.rs b/crates/nu-command/src/bytes/at.rs index f692e2f52a..18490e7300 100644 --- a/crates/nu-command/src/bytes/at.rs +++ b/crates/nu-command/src/bytes/at.rs @@ -176,13 +176,12 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { Value::Error { .. } => input.clone(), other => Value::error( - ShellError::UnsupportedInput( - "Only binary values are supported".into(), - format!("input type: {:?}", other.get_type()), - head, - // This line requires the Value::Error match above. - other.span(), - ), + ShellError::UnsupportedInput { + msg: "Only binary values are supported".into(), + input: format!("input type: {:?}", other.get_type()), + msg_span: head, + input_span: other.span(), + }, head, ), } diff --git a/crates/nu-command/src/charting/hashable_value.rs b/crates/nu-command/src/charting/hashable_value.rs index 5d23146539..96b41a3371 100644 --- a/crates/nu-command/src/charting/hashable_value.rs +++ b/crates/nu-command/src/charting/hashable_value.rs @@ -102,12 +102,12 @@ impl HashableValue { // Explicitly propagate errors instead of dropping them. Value::Error { error, .. } => Err(*error), - _ => Err(ShellError::UnsupportedInput( - "input value is not hashable".into(), - format!("input type: {:?}", value.get_type()), - span, - value.span(), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "input value is not hashable".into(), + input: format!("input type: {:?}", value.get_type()), + msg_span: span, + input_span: value.span(), + }), } } diff --git a/crates/nu-command/src/charting/histogram.rs b/crates/nu-command/src/charting/histogram.rs index fc58053a39..9f7baacfdd 100755 --- a/crates/nu-command/src/charting/histogram.rs +++ b/crates/nu-command/src/charting/histogram.rs @@ -162,14 +162,9 @@ fn run_histogram( let t = v.get_type(); let span = v.span(); inputs.push(HashableValue::from_value(v, head_span).map_err(|_| { - ShellError::UnsupportedInput( - "Since --column-name was not provided, only lists of hashable values are supported.".to_string(), - format!( + ShellError::UnsupportedInput { msg: "Since --column-name was not provided, only lists of hashable values are supported.".to_string(), input: format!( "input type: {t:?}" - ), - head_span, - span, - ) + ), msg_span: head_span, input_span: span } })?) } } diff --git a/crates/nu-command/src/env/load_env.rs b/crates/nu-command/src/env/load_env.rs index 12687b3877..4e4394047e 100644 --- a/crates/nu-command/src/env/load_env.rs +++ b/crates/nu-command/src/env/load_env.rs @@ -81,12 +81,12 @@ impl Command for LoadEnv { } Ok(PipelineData::empty()) } - _ => Err(ShellError::UnsupportedInput( - "'load-env' expects a single record".into(), - "value originated from here".into(), - span, - input.span().unwrap_or(span), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "'load-env' expects a single record".into(), + input: "value originated from here".into(), + msg_span: span, + input_span: input.span().unwrap_or(span), + }), }, } } diff --git a/crates/nu-command/src/filters/find.rs b/crates/nu-command/src/filters/find.rs index 93c58b5dd7..65d45d902e 100644 --- a/crates/nu-command/src/filters/find.rs +++ b/crates/nu-command/src/filters/find.rs @@ -469,13 +469,12 @@ fn find_with_rest_and_highlight( // Propagate errors by explicitly matching them before the final case. Value::Error { error, .. } => return Err(*error), other => { - return Err(ShellError::UnsupportedInput( - "unsupported type from raw stream".into(), - format!("input: {:?}", other.get_type()), - span, - // This line requires the Value::Error match above. - other.span(), - )); + return Err(ShellError::UnsupportedInput { + msg: "unsupported type from raw stream".into(), + input: format!("input: {:?}", other.get_type()), + msg_span: span, + input_span: other.span(), + }); } } } diff --git a/crates/nu-command/src/filters/flatten.rs b/crates/nu-command/src/filters/flatten.rs index dbe30213be..7650e741cf 100644 --- a/crates/nu-command/src/filters/flatten.rs +++ b/crates/nu-command/src/filters/flatten.rs @@ -208,12 +208,7 @@ fn flat_value(columns: &[CellPath], item: &Value, name_tag: Span, all: bool) -> } Value::List { vals, .. } if all && vals.iter().all(|f| f.as_record().is_ok()) => { if need_flatten && inner_table.is_some() { - return vec![Value::error( ShellError::UnsupportedInput( - "can only flatten one inner list at a time. tried flattening more than one column with inner lists... but is flattened already".to_string(), - "value originates from here".into(), - s, - span - ), span) + return vec![Value::error( ShellError::UnsupportedInput { msg: "can only flatten one inner list at a time. tried flattening more than one column with inner lists... but is flattened already".to_string(), input: "value originates from here".into(), msg_span: s, input_span: span }, span) ]; } // it's a table (a list of record, we can flatten inner record) @@ -244,12 +239,7 @@ fn flat_value(columns: &[CellPath], item: &Value, name_tag: Span, all: bool) -> } Value::List { vals: values, .. } => { if need_flatten && inner_table.is_some() { - return vec![Value::error( ShellError::UnsupportedInput( - "can only flatten one inner list at a time. tried flattening more than one column with inner lists... but is flattened already".to_string(), - "value originates from here".into(), - s, - span - ), span) + return vec![Value::error( ShellError::UnsupportedInput { msg: "can only flatten one inner list at a time. tried flattening more than one column with inner lists... but is flattened already".to_string(), input: "value originates from here".into(), msg_span: s, input_span: span }, span) ]; } diff --git a/crates/nu-command/src/filters/join.rs b/crates/nu-command/src/filters/join.rs index b69472152d..a95839d2e2 100644 --- a/crates/nu-command/src/filters/join.rs +++ b/crates/nu-command/src/filters/join.rs @@ -89,18 +89,18 @@ impl Command for Join { let result = join(rows_1, rows_2, l_on, r_on, join_type, span); Ok(PipelineData::Value(result, None)) } - _ => Err(ShellError::UnsupportedInput( - "(PipelineData, table, string, string)".into(), - format!( + _ => Err(ShellError::UnsupportedInput { + msg: "(PipelineData
, table, string, string)".into(), + input: format!( "({:?}, {:?}, {:?} {:?})", collected_input, table_2.get_type(), l_on.get_type(), r_on.get_type(), ), - span, - span, - )), + msg_span: span, + input_span: span, + }), } } @@ -126,12 +126,12 @@ fn join_type(call: &Call) -> Result { (false, true, false, false) => Ok(JoinType::Left), (false, false, true, false) => Ok(JoinType::Right), (false, false, false, true) => Ok(JoinType::Outer), - _ => Err(ShellError::UnsupportedInput( - "Choose one of: --inner, --left, --right, --outer".into(), - "".into(), - call.head, - call.head, - )), + _ => Err(ShellError::UnsupportedInput { + msg: "Choose one of: --inner, --left, --right, --outer".into(), + input: "".into(), + msg_span: call.head, + input_span: call.head, + }), } } diff --git a/crates/nu-command/src/filters/rename.rs b/crates/nu-command/src/filters/rename.rs index 0719e7f084..ac41aae077 100644 --- a/crates/nu-command/src/filters/rename.rs +++ b/crates/nu-command/src/filters/rename.rs @@ -204,16 +204,9 @@ fn rename( "already checked column to rename still exists", ); return Value::error( - ShellError::UnsupportedInput( - format!( + ShellError::UnsupportedInput { msg: format!( "The column '{not_exists_column}' does not exist in the input", - ), - "value originated from here".into(), - // Arrow 1 points at the specified column name, - head_span, - // Arrow 2 points at the input value. - span, - ), + ), input: "value originated from here".into(), msg_span: head_span, input_span: span }, span, ); } diff --git a/crates/nu-command/src/formats/from/ods.rs b/crates/nu-command/src/formats/from/ods.rs index 5850733e76..41230aec42 100644 --- a/crates/nu-command/src/formats/from/ods.rs +++ b/crates/nu-command/src/formats/from/ods.rs @@ -95,12 +95,12 @@ fn collect_binary(input: PipelineData, span: Span) -> Result, ShellError } Some(Value::Error { error, .. }) => return Err(*error), Some(x) => { - return Err(ShellError::UnsupportedInput( - "Expected binary from pipeline".to_string(), - "value originates from here".into(), - span, - x.span(), - )) + return Err(ShellError::UnsupportedInput { + msg: "Expected binary from pipeline".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: x.span(), + }) } None => break, } @@ -117,13 +117,11 @@ fn from_ods( let span = input.span(); let bytes = collect_binary(input, head)?; let buf: Cursor> = Cursor::new(bytes); - let mut ods = Ods::<_>::new(buf).map_err(|_| { - ShellError::UnsupportedInput( - "Could not load ODS file".to_string(), - "value originates from here".into(), - head, - span.unwrap_or(head), - ) + let mut ods = Ods::<_>::new(buf).map_err(|_| ShellError::UnsupportedInput { + msg: "Could not load ODS file".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span.unwrap_or(head), })?; let mut dict = IndexMap::new(); @@ -160,12 +158,12 @@ fn from_ods( dict.insert(sheet_name, Value::list(sheet_output, head)); } else { - return Err(ShellError::UnsupportedInput( - "Could not load sheet".to_string(), - "value originates from here".into(), - head, - span.unwrap_or(head), - )); + return Err(ShellError::UnsupportedInput { + msg: "Could not load sheet".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span.unwrap_or(head), + }); } } diff --git a/crates/nu-command/src/formats/from/xlsx.rs b/crates/nu-command/src/formats/from/xlsx.rs index 4a5057ea01..7d15c6e772 100644 --- a/crates/nu-command/src/formats/from/xlsx.rs +++ b/crates/nu-command/src/formats/from/xlsx.rs @@ -94,12 +94,12 @@ fn collect_binary(input: PipelineData, span: Span) -> Result, ShellError bytes.extend_from_slice(&b); } Some(x) => { - return Err(ShellError::UnsupportedInput( - "Expected binary from pipeline".to_string(), - "value originates from here".into(), - span, - x.span(), - )) + return Err(ShellError::UnsupportedInput { + msg: "Expected binary from pipeline".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: x.span(), + }) } None => break, } @@ -116,13 +116,11 @@ fn from_xlsx( let span = input.span(); let bytes = collect_binary(input, head)?; let buf: Cursor> = Cursor::new(bytes); - let mut xlsx = Xlsx::<_>::new(buf).map_err(|_| { - ShellError::UnsupportedInput( - "Could not load XLSX file".to_string(), - "value originates from here".into(), - head, - span.unwrap_or(head), - ) + let mut xlsx = Xlsx::<_>::new(buf).map_err(|_| ShellError::UnsupportedInput { + msg: "Could not load XLSX file".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span.unwrap_or(head), })?; let mut dict = IndexMap::new(); @@ -159,12 +157,12 @@ fn from_xlsx( dict.insert(sheet_name, Value::list(sheet_output, head)); } else { - return Err(ShellError::UnsupportedInput( - "Could not load sheet".to_string(), - "value originates from here".into(), - head, - span.unwrap_or(head), - )); + return Err(ShellError::UnsupportedInput { + msg: "Could not load sheet".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span.unwrap_or(head), + }); } } diff --git a/crates/nu-command/src/formats/from/yaml.rs b/crates/nu-command/src/formats/from/yaml.rs index 7fb28f68fe..1134336588 100644 --- a/crates/nu-command/src/formats/from/yaml.rs +++ b/crates/nu-command/src/formats/from/yaml.rs @@ -81,12 +81,12 @@ fn convert_yaml_value_to_nu_value( span: Span, val_span: Span, ) -> Result { - let err_not_compatible_number = ShellError::UnsupportedInput( - "Expected a nu-compatible number in YAML input".to_string(), - "value originates from here".into(), - span, - val_span, - ); + let err_not_compatible_number = ShellError::UnsupportedInput { + msg: "Expected a nu-compatible number in YAML input".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: val_span, + }; Ok(match v { serde_yaml::Value::Bool(b) => Value::bool(*b, span), serde_yaml::Value::Number(n) if n.is_i64() => { @@ -109,12 +109,12 @@ fn convert_yaml_value_to_nu_value( for (k, v) in t { // A ShellError that we re-use multiple times in the Mapping scenario - let err_unexpected_map = ShellError::UnsupportedInput( - format!("Unexpected YAML:\nKey: {k:?}\nValue: {v:?}"), - "value originates from here".into(), - span, - val_span, - ); + let err_unexpected_map = ShellError::UnsupportedInput { + msg: format!("Unexpected YAML:\nKey: {k:?}\nValue: {v:?}"), + input: "value originates from here".into(), + msg_span: span, + input_span: val_span, + }; match (k, v) { (serde_yaml::Value::Number(k), _) => { collected.insert( @@ -198,14 +198,13 @@ pub fn from_yaml_string_to_value( let mut documents = vec![]; for document in serde_yaml::Deserializer::from_str(&s) { - let v: serde_yaml::Value = serde_yaml::Value::deserialize(document).map_err(|x| { - ShellError::UnsupportedInput( - format!("Could not load YAML: {x}"), - "value originates from here".into(), - span, - val_span, - ) - })?; + let v: serde_yaml::Value = + serde_yaml::Value::deserialize(document).map_err(|x| ShellError::UnsupportedInput { + msg: format!("Could not load YAML: {x}"), + input: "value originates from here".into(), + msg_span: span, + input_span: val_span, + })?; documents.push(convert_yaml_value_to_nu_value(&v, span, val_span)?); } diff --git a/crates/nu-command/src/formats/to/delimited.rs b/crates/nu-command/src/formats/to/delimited.rs index 6036a82a8d..89b2d9a1d2 100644 --- a/crates/nu-command/src/formats/to/delimited.rs +++ b/crates/nu-command/src/formats/to/delimited.rs @@ -126,12 +126,12 @@ fn to_string_tagged_value( } fn make_unsupported_input_error(value: &Value, head: Span, span: Span) -> ShellError { - ShellError::UnsupportedInput( - "Unexpected type".to_string(), - format!("input type: {:?}", value.get_type()), - head, - span, - ) + ShellError::UnsupportedInput { + msg: "Unexpected type".to_string(), + input: format!("input type: {:?}", value.get_type()), + msg_span: head, + input_span: span, + } } pub fn find_non_record(values: &[Value]) -> Option<&Value> { diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index bd09df211b..e9429cc9f1 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -131,28 +131,28 @@ pub fn value_to_string( let mut s = String::with_capacity(2 * val.len()); for byte in val { if write!(s, "{byte:02X}").is_err() { - return Err(ShellError::UnsupportedInput( - "could not convert binary to string".into(), - "value originates from here".into(), - span, - v.span(), - )); + return Err(ShellError::UnsupportedInput { + msg: "could not convert binary to string".into(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }); } } Ok(format!("0x[{s}]")) } - Value::Block { .. } => Err(ShellError::UnsupportedInput( - "blocks are currently not nuon-compatible".into(), - "value originates from here".into(), - span, - v.span(), - )), - Value::Closure { .. } => Err(ShellError::UnsupportedInput( - "closures are currently not nuon-compatible".into(), - "value originates from here".into(), - span, - v.span(), - )), + Value::Block { .. } => Err(ShellError::UnsupportedInput { + msg: "blocks are currently not nuon-compatible".into(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }), + Value::Closure { .. } => Err(ShellError::UnsupportedInput { + msg: "closures are currently not nuon-compatible".into(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }), Value::Bool { val, .. } => { if *val { Ok("true".to_string()) @@ -160,18 +160,18 @@ pub fn value_to_string( Ok("false".to_string()) } } - Value::CellPath { .. } => Err(ShellError::UnsupportedInput( - "cell-paths are currently not nuon-compatible".to_string(), - "value originates from here".into(), - span, - v.span(), - )), - Value::CustomValue { .. } => Err(ShellError::UnsupportedInput( - "custom values are currently not nuon-compatible".to_string(), - "value originates from here".into(), - span, - v.span(), - )), + Value::CellPath { .. } => Err(ShellError::UnsupportedInput { + msg: "cell-paths are currently not nuon-compatible".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }), + Value::CustomValue { .. } => Err(ShellError::UnsupportedInput { + msg: "custom values are currently not nuon-compatible".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }), Value::Date { val, .. } => Ok(val.to_rfc3339()), // FIXME: make durations use the shortest lossless representation. Value::Duration { val, .. } => Ok(format!("{}ns", *val)), @@ -241,12 +241,12 @@ pub fn value_to_string( )) } } - Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput( - "match patterns are currently not nuon-compatible".to_string(), - "value originates from here".into(), - span, - v.span(), - )), + Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput { + msg: "match patterns are currently not nuon-compatible".to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: v.span(), + }), Value::Nothing { .. } => Ok("null".to_string()), Value::Range { val, .. } => Ok(format!( "{}..{}{}", diff --git a/crates/nu-command/src/formats/to/toml.rs b/crates/nu-command/src/formats/to/toml.rs index a67f384e4e..b61337f15e 100644 --- a/crates/nu-command/src/formats/to/toml.rs +++ b/crates/nu-command/src/formats/to/toml.rs @@ -137,12 +137,12 @@ fn value_to_toml_value( Value::Record { .. } => helper(engine_state, v), // Propagate existing errors Value::Error { error, .. } => Err(*error.clone()), - _ => Err(ShellError::UnsupportedInput( - format!("{:?} is not valid top-level TOML", v.get_type()), - "value originates from here".into(), - head, - v.span(), - )), + _ => Err(ShellError::UnsupportedInput { + msg: format!("{:?} is not valid top-level TOML", v.get_type()), + input: "value originates from here".into(), + msg_span: head, + input_span: v.span(), + }), } } diff --git a/crates/nu-command/src/math/log.rs b/crates/nu-command/src/math/log.rs index e3577c9b05..fe71606dff 100644 --- a/crates/nu-command/src/math/log.rs +++ b/crates/nu-command/src/math/log.rs @@ -50,12 +50,12 @@ impl Command for SubCommand { let base: Spanned = call.req(engine_state, stack, 0)?; if base.item <= 0.0f64 { - return Err(ShellError::UnsupportedInput( - "Base has to be greater 0".into(), - "value originates from here".into(), - head, - base.span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Base has to be greater 0".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: base.span, + }); } // This doesn't match explicit nulls if matches!(input, PipelineData::Empty) { @@ -103,13 +103,13 @@ fn operate(value: Value, head: Span, base: f64) -> Value { if val <= 0.0 { return Value::error( - ShellError::UnsupportedInput( - "'math log' undefined for values outside the open interval (0, Inf)." + ShellError::UnsupportedInput { + msg: "'math log' undefined for values outside the open interval (0, Inf)." .into(), - "value originates from here".into(), - head, - span, - ), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ); } diff --git a/crates/nu-command/src/math/median.rs b/crates/nu-command/src/math/median.rs index 097b4a3044..4957c36c63 100644 --- a/crates/nu-command/src/math/median.rs +++ b/crates/nu-command/src/math/median.rs @@ -107,14 +107,14 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result { let idx = (values.len() as f64 / 2.0).floor() as usize; - let out = sorted.get(idx).ok_or_else(|| { - ShellError::UnsupportedInput( - "Empty input".to_string(), - "value originates from here".into(), - head, - span, - ) - })?; + let out = sorted + .get(idx) + .ok_or_else(|| ShellError::UnsupportedInput { + msg: "Empty input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + })?; Ok(out.clone()) } Pick::MedianAverage => { @@ -123,25 +123,21 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result Result Err(*error.clone()), - other => Err(ShellError::UnsupportedInput( - "Unable to give a result with this input".to_string(), - "value originates from here".into(), - head, - other.span(), - )), + other => Err(ShellError::UnsupportedInput { + msg: "Unable to give a result with this input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: other.span(), + }), }) .collect::, ShellError>>()?; diff --git a/crates/nu-command/src/math/reducers.rs b/crates/nu-command/src/math/reducers.rs index e879146737..1f96a778d7 100644 --- a/crates/nu-command/src/math/reducers.rs +++ b/crates/nu-command/src/math/reducers.rs @@ -23,13 +23,11 @@ pub fn reducer_for(command: Reduce) -> ReducerFunction { pub fn max(data: Vec, span: Span, head: Span) -> Result { let mut biggest = data .first() - .ok_or_else(|| { - ShellError::UnsupportedInput( - "Empty input".to_string(), - "value originates from here".into(), - head, - span, - ) + .ok_or_else(|| ShellError::UnsupportedInput { + msg: "Empty input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, })? .clone(); @@ -54,13 +52,11 @@ pub fn max(data: Vec, span: Span, head: Span) -> Result, span: Span, head: Span) -> Result { let mut smallest = data .first() - .ok_or_else(|| { - ShellError::UnsupportedInput( - "Empty input".to_string(), - "value originates from here".into(), - head, - span, - ) + .ok_or_else(|| ShellError::UnsupportedInput { + msg: "Empty input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, })? .clone(); @@ -96,12 +92,12 @@ pub fn sum(data: Vec, span: Span, head: Span) -> Result Err(ShellError::UnsupportedInput( - "Empty input".to_string(), - "value originates from here".into(), - head, - span, - )), + None => Err(ShellError::UnsupportedInput { + msg: "Empty input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }), }?; for value in &data { @@ -114,12 +110,13 @@ pub fn sum(data: Vec, span: Span, head: Span) -> Result return Err(*error.clone()), other => { - return Err(ShellError::UnsupportedInput( - "Attempted to compute the sum of a value that cannot be summed".to_string(), - "value originates from here".into(), - head, - other.span(), - )); + return Err(ShellError::UnsupportedInput { + msg: "Attempted to compute the sum of a value that cannot be summed" + .to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: other.span(), + }); } } } @@ -137,12 +134,12 @@ pub fn product(data: Vec, span: Span, head: Span) -> Result Ok(Value::nothing(head)), } } - None => Err(ShellError::UnsupportedInput( - "Empty input".to_string(), - "value originates from here".into(), - head, - span, - )), + None => Err(ShellError::UnsupportedInput { + msg: "Empty input".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }), }?; for value in &data { @@ -152,13 +149,13 @@ pub fn product(data: Vec, span: Span, head: Span) -> Result return Err(*error.clone()), other => { - return Err(ShellError::UnsupportedInput( - "Attempted to compute the product of a value that cannot be multiplied" + return Err(ShellError::UnsupportedInput { + msg: "Attempted to compute the product of a value that cannot be multiplied" .to_string(), - "value originates from here".into(), - head, - other.span(), - )); + input: "value originates from here".into(), + msg_span: head, + input_span: other.span(), + }); } } } diff --git a/crates/nu-command/src/math/sqrt.rs b/crates/nu-command/src/math/sqrt.rs index f4cb43eb24..95427c3b32 100644 --- a/crates/nu-command/src/math/sqrt.rs +++ b/crates/nu-command/src/math/sqrt.rs @@ -93,12 +93,12 @@ fn operate(value: Value, head: Span) -> Value { fn error_negative_sqrt(head: Span, span: Span) -> Value { Value::error( - ShellError::UnsupportedInput( - String::from("Can't square root a negative number"), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: String::from("Can't square root a negative number"), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ) } diff --git a/crates/nu-command/src/math/utils.rs b/crates/nu-command/src/math/utils.rs index 0423689aa6..ee1dc8228c 100644 --- a/crates/nu-command/src/math/utils.rs +++ b/crates/nu-command/src/math/utils.rs @@ -49,12 +49,12 @@ fn helper_for_tables( } } if column_totals.keys().len() == 0 { - return Err(ShellError::UnsupportedInput( - "Unable to give a result with this input".to_string(), - "value originates from here".into(), - name, - val_span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Unable to give a result with this input".to_string(), + input: "value originates from here".into(), + msg_span: name, + input_span: val_span, + }); } Ok(Value::record(column_totals.into_iter().collect(), name)) @@ -107,13 +107,13 @@ pub fn calculate( } PipelineData::Value(val, ..) => mf(&[val], span, name), PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: name }), - val => Err(ShellError::UnsupportedInput( - "Only ints, floats, lists, records, or ranges are supported".into(), - "value originates from here".into(), - name, - // This requires both the ListStream and Empty match arms to be above it. - val.span() + val => Err(ShellError::UnsupportedInput { + msg: "Only ints, floats, lists, records, or ranges are supported".into(), + input: "value originates from here".into(), + msg_span: name, + input_span: val + .span() .expect("non-Empty non-ListStream PipelineData had no span"), - )), + }), } } diff --git a/crates/nu-command/src/math/variance.rs b/crates/nu-command/src/math/variance.rs index f28f7c60d1..09b13934e9 100644 --- a/crates/nu-command/src/math/variance.rs +++ b/crates/nu-command/src/math/variance.rs @@ -65,12 +65,13 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result { let v = match &value { Value::Int { .. } | Value::Float { .. } => Ok(value), Value::Error { error, .. } => Err(*error.clone()), - _ => Err(ShellError::UnsupportedInput( - "Attempted to compute the sum of squares of a non-int, non-float value".to_string(), - "value originates from here".into(), - span, - value.span(), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "Attempted to compute the sum of squares of a non-int, non-float value" + .to_string(), + input: "value originates from here".into(), + msg_span: span, + input_span: value.span(), + }), }?; let v_squared = &v.mul(span, v, span)?; sum_x2 = sum_x2.add(span, v_squared, span)?; diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 279b13e9d0..edfbad3040 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -56,13 +56,8 @@ pub fn http_parse_url( let url = match url::Url::parse(&requested_url) { Ok(u) => u, Err(_e) => { - return Err(ShellError::UnsupportedInput( - "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com" - .to_string(), - format!("value: '{requested_url:?}'"), - call.head, - span, - )); + return Err(ShellError::UnsupportedInput { msg: "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com" + .to_string(), input: format!("value: '{requested_url:?}'"), msg_span: call.head, input_span: span }); } }; diff --git a/crates/nu-command/src/network/url/build_query.rs b/crates/nu-command/src/network/url/build_query.rs index 386d34e36d..075b39f733 100644 --- a/crates/nu-command/src/network/url/build_query.rs +++ b/crates/nu-command/src/network/url/build_query.rs @@ -75,12 +75,12 @@ fn to_url(input: PipelineData, head: Span) -> Result { row_vec.push((k.clone(), s.to_string())); } _ => { - return Err(ShellError::UnsupportedInput( - "Expected a record with string values".to_string(), - "value originates from here".into(), - head, - span, - )); + return Err(ShellError::UnsupportedInput { + msg: "Expected a record with string values".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }); } } } @@ -97,12 +97,12 @@ fn to_url(input: PipelineData, head: Span) -> Result { } // Propagate existing errors Value::Error { error, .. } => Err(*error), - other => Err(ShellError::UnsupportedInput( - "Expected a table from pipeline".to_string(), - "value originates from here".into(), - head, - other.span(), - )), + other => Err(ShellError::UnsupportedInput { + msg: "Expected a table from pipeline".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: other.span(), + }), } }) .collect(); diff --git a/crates/nu-command/src/network/url/join.rs b/crates/nu-command/src/network/url/join.rs index 557b3c3943..488d0aa8ee 100644 --- a/crates/nu-command/src/network/url/join.rs +++ b/crates/nu-command/src/network/url/join.rs @@ -104,12 +104,12 @@ impl Command for SubCommand { url_components?.to_url(span) } Value::Error { error, .. } => Err(*error), - other => Err(ShellError::UnsupportedInput( - "Expected a record from pipeline".to_string(), - "value originates from here".into(), - head, - other.span(), - )), + other => Err(ShellError::UnsupportedInput { + msg: "Expected a record from pipeline".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: other.span(), + }), } }) .collect(); diff --git a/crates/nu-command/src/network/url/parse.rs b/crates/nu-command/src/network/url/parse.rs index 3ef24883c3..1b3712c9c7 100644 --- a/crates/nu-command/src/network/url/parse.rs +++ b/crates/nu-command/src/network/url/parse.rs @@ -113,21 +113,21 @@ fn parse(value: Value, head: Span, engine_state: &EngineState) -> Result Err(ShellError::UnsupportedInput( - "String not compatible with url-encoding".to_string(), - "value originates from here".into(), - head, - span, - )), + _ => Err(ShellError::UnsupportedInput { + msg: "String not compatible with url-encoding".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }), } } - Err(_e) => Err(ShellError::UnsupportedInput( - "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com" + Err(_e) => Err(ShellError::UnsupportedInput { + msg: "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com" .to_string(), - "value originates from here".into(), - head, - span, - )), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }), } } diff --git a/crates/nu-command/src/path/join.rs b/crates/nu-command/src/path/join.rs index c870af1edc..5196fe6746 100644 --- a/crates/nu-command/src/path/join.rs +++ b/crates/nu-command/src/path/join.rs @@ -180,12 +180,12 @@ fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result Err(ShellError::PipelineEmpty { dst_span: head }), - _ => Err(ShellError::UnsupportedInput( - "Input value cannot be joined".to_string(), - "value originates from here".into(), - head, - input.span().unwrap_or(call.head), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "Input value cannot be joined".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: input.span().unwrap_or(call.head), + }), } } @@ -249,14 +249,9 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Result ShellError { - ShellError::UnsupportedInput( - format!("{} is not a valid event type", val), - "value originates from here".into(), - head, - val_span, - ) + ShellError::UnsupportedInput { + msg: format!("{} is not a valid event type", val), + input: "value originates from here".into(), + msg_span: head, + input_span: val_span, + } } fn bad_list_error(head: Span, value: &Value) -> ShellError { - ShellError::UnsupportedInput( - "--types expects a list of strings".to_string(), - "value originates from here".into(), - head, - value.span(), - ) + ShellError::UnsupportedInput { + msg: "--types expects a list of strings".to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: value.span(), + } } /// Enable capturing of all events allowed by this filter. diff --git a/crates/nu-command/src/strings/encode_decode/base64.rs b/crates/nu-command/src/strings/encode_decode/base64.rs index cb017236cc..8d845a1e00 100644 --- a/crates/nu-command/src/strings/encode_decode/base64.rs +++ b/crates/nu-command/src/strings/encode_decode/base64.rs @@ -127,13 +127,12 @@ fn action( Value::string(std::str::from_utf8(&enc_vec).unwrap_or(""), command_span) } ActionType::Decode => Value::error( - ShellError::UnsupportedInput( - "Binary data can only be encoded".to_string(), - "value originates from here".into(), - command_span, - // This line requires the Value::Error {} match above. - input.span(), - ), + ShellError::UnsupportedInput { + msg: "Binary data can only be encoded".to_string(), + input: "value originates from here".into(), + msg_span: command_span, + input_span: input.span(), + }, command_span, ), }, diff --git a/crates/nu-command/src/strings/encode_decode/decode.rs b/crates/nu-command/src/strings/encode_decode/decode.rs index 46a69b25d5..12014da4d3 100644 --- a/crates/nu-command/src/strings/encode_decode/decode.rs +++ b/crates/nu-command/src/strings/encode_decode/decode.rs @@ -99,12 +99,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"# } // This should be more precise, but due to difficulties in getting spans // from PipelineData::ListData, this is as it is. - _ => Err(ShellError::UnsupportedInput( - "non-binary input".into(), - "value originates from here".into(), - head, - input.span().unwrap_or(head), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "non-binary input".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: input.span().unwrap_or(head), + }), } } } diff --git a/crates/nu-command/src/strings/encode_decode/encode.rs b/crates/nu-command/src/strings/encode_decode/encode.rs index 86bfc5ff11..2ec83dbfac 100644 --- a/crates/nu-command/src/strings/encode_decode/encode.rs +++ b/crates/nu-command/src/strings/encode_decode/encode.rs @@ -113,12 +113,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"# } // This should be more precise, but due to difficulties in getting spans // from PipelineData::ListStream, this is as it is. - _ => Err(ShellError::UnsupportedInput( - "non-string input".into(), - "value originates from here".into(), - head, - input.span().unwrap_or(head), - )), + _ => Err(ShellError::UnsupportedInput { + msg: "non-string input".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: input.span().unwrap_or(head), + }), } } } diff --git a/crates/nu-command/src/strings/encode_decode/encoding.rs b/crates/nu-command/src/strings/encode_decode/encoding.rs index 1917184b32..25bdfeab2d 100644 --- a/crates/nu-command/src/strings/encode_decode/encoding.rs +++ b/crates/nu-command/src/strings/encode_decode/encoding.rs @@ -12,12 +12,12 @@ pub fn detect_encoding_name( //Guess(TLD=None(usually used in HTML), Allow_UTF8=True) let (encoding, is_certain) = detector.guess_assess(None, true); if !is_certain { - return Err(ShellError::UnsupportedInput( - "Input contains unknown encoding, try giving a encoding name".into(), - "value originates from here".into(), - head, - input, - )); + return Err(ShellError::UnsupportedInput { + msg: "Input contains unknown encoding, try giving a encoding name".into(), + input: "value originates from here".into(), + msg_span: head, + input_span: input, + }); } Ok(encoding) } diff --git a/crates/nu-command/src/strings/str_/substring.rs b/crates/nu-command/src/strings/str_/substring.rs index edfdf53146..74652ed660 100644 --- a/crates/nu-command/src/strings/str_/substring.rs +++ b/crates/nu-command/src/strings/str_/substring.rs @@ -191,13 +191,12 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { // Propagate errors by explicitly matching them before the final case. Value::Error { .. } => input.clone(), other => Value::error( - ShellError::UnsupportedInput( - "Only string values are supported".into(), - format!("input type: {:?}", other.get_type()), - head, - // This line requires the Value::Error match above. - other.span(), - ), + ShellError::UnsupportedInput { + msg: "Only string values are supported".into(), + input: format!("input type: {:?}", other.get_type()), + msg_span: head, + input_span: other.span(), + }, head, ), } diff --git a/crates/nu-command/src/strings/str_/trim/trim_.rs b/crates/nu-command/src/strings/str_/trim/trim_.rs index 6f7b2591dc..3434ef2161 100644 --- a/crates/nu-command/src/strings/str_/trim/trim_.rs +++ b/crates/nu-command/src/strings/str_/trim/trim_.rs @@ -198,18 +198,15 @@ fn action(input: &Value, arg: &Arguments, head: Span) -> Value { } _ => input.clone(), }, - ActionMode::Local => { - Value::error( - ShellError::UnsupportedInput( - "Only string values are supported".into(), - format!("input type: {:?}", other.get_type()), - head, - // This line requires the Value::Error match above. - other.span(), - ), - head, - ) - } + ActionMode::Local => Value::error( + ShellError::UnsupportedInput { + msg: "Only string values are supported".into(), + input: format!("input type: {:?}", other.get_type()), + msg_span: head, + input_span: other.span(), + }, + head, + ), } } } diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index e581e68c7b..551691edcf 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -627,12 +627,14 @@ pub enum ShellError { /// This error is fairly generic. Refer to the specific error message for further details. #[error("Unsupported input")] #[diagnostic(code(nu::shell::unsupported_input))] - UnsupportedInput( - String, - String, - #[label("{0}")] Span, // call head (the name of the command itself) - #[label("input type: {1}")] Span, - ), + UnsupportedInput { + msg: String, + input: String, + #[label("{msg}")] + msg_span: Span, + #[label("{input}")] + input_span: Span, + }, /// Failed to parse an input into a datetime value. /// diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 72cf6d20b2..7b2956cd40 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -1659,12 +1659,12 @@ impl Value { // SIGH... Value::Error { error, .. } => return Err(*error.clone()), _ => { - return Err(ShellError::UnsupportedInput( - "expected table or record".into(), - format!("input type: {:?}", val.get_type()), - head_span, - *span, - )) + return Err(ShellError::UnsupportedInput { + msg: "expected table or record".into(), + input: format!("input type: {:?}", val.get_type()), + msg_span: head_span, + input_span: *span, + }) } } } @@ -1697,12 +1697,12 @@ impl Value { *self = record } other => { - return Err(ShellError::UnsupportedInput( - "table or record".into(), - format!("input type: {:?}", other.get_type()), - head_span, - *span, - )) + return Err(ShellError::UnsupportedInput { + msg: "table or record".into(), + input: format!("input type: {:?}", other.get_type()), + msg_span: head_span, + input_span: *span, + }) } }, PathMember::Int { @@ -3220,27 +3220,24 @@ impl Value { if let Some(regex) = cache.get(rhs) { regex.is_match(lhs) } else { - let regex = Regex::new(rhs).map_err(|e| { - ShellError::UnsupportedInput( - format!("{e}"), - "value originated from here".into(), - span, - rhs_span, - ) - })?; + let regex = + Regex::new(rhs).map_err(|e| ShellError::UnsupportedInput { + msg: format!("{e}"), + input: "value originated from here".into(), + msg_span: span, + input_span: rhs_span, + })?; let ret = regex.is_match(lhs); cache.put(rhs.clone(), regex); ret } } Err(_) => { - let regex = Regex::new(rhs).map_err(|e| { - ShellError::UnsupportedInput( - format!("{e}"), - "value originated from here".into(), - span, - rhs_span, - ) + let regex = Regex::new(rhs).map_err(|e| ShellError::UnsupportedInput { + msg: format!("{e}"), + input: "value originated from here".into(), + msg_span: span, + input_span: rhs_span, })?; regex.is_match(lhs) } diff --git a/crates/nu_plugin_formats/src/from/ics.rs b/crates/nu_plugin_formats/src/from/ics.rs index 52f3863497..7509deddab 100644 --- a/crates/nu_plugin_formats/src/from/ics.rs +++ b/crates/nu_plugin_formats/src/from/ics.rs @@ -36,12 +36,12 @@ pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result output.push(calendar_to_value(c, head)), Err(e) => output.push(Value::error( - ShellError::UnsupportedInput( - format!("input cannot be parsed as .ics ({e})"), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: format!("input cannot be parsed as .ics ({e})"), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, )), } diff --git a/crates/nu_plugin_formats/src/from/ini.rs b/crates/nu_plugin_formats/src/from/ini.rs index ba16f23a7f..74a885b998 100644 --- a/crates/nu_plugin_formats/src/from/ini.rs +++ b/crates/nu_plugin_formats/src/from/ini.rs @@ -40,12 +40,12 @@ pub fn from_ini_call(call: &EvaluatedCall, input: &Value) -> Result Err(ShellError::UnsupportedInput( - format!("Could not load ini: {err}"), - "value originates from here".into(), - head, - span, - ) + Err(err) => Err(ShellError::UnsupportedInput { + msg: format!("Could not load ini: {err}"), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + } .into()), } } diff --git a/crates/nu_plugin_formats/src/from/vcf.rs b/crates/nu_plugin_formats/src/from/vcf.rs index 0c5c1a5fe5..c44aebb9d3 100644 --- a/crates/nu_plugin_formats/src/from/vcf.rs +++ b/crates/nu_plugin_formats/src/from/vcf.rs @@ -32,12 +32,12 @@ pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result contact_to_value(c, head), Err(e) => Value::error( - ShellError::UnsupportedInput( - format!("input cannot be parsed as .vcf ({e})"), - "value originates from here".into(), - head, - span, - ), + ShellError::UnsupportedInput { + msg: format!("input cannot be parsed as .vcf ({e})"), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + }, span, ), });