From 3e5f81ae1450c963e4a88dab438a874c76473399 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Sat, 9 Dec 2023 16:46:21 -0800 Subject: [PATCH] Convert remainder of ShellError variants to named fields (#11276) # Description Removed variants that are no longer in use: * `NoFile*` * `UnexpectedAbbrComponent` Converted: * `OutsideSpannedLabeledError` * `EvalBlockWithInput` * `Break` * `Continue` * `Return` * `NotAConstant` * `NotAConstCommand` * `NotAConstHelp` * `InvalidGlobPattern` * `ErrorExpandingGlob` Fixes #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 --- crates/nu-cli/src/eval_file.rs | 2 +- crates/nu-cmd-base/src/arg_glob.rs | 10 +- .../nu-cmd-lang/src/core_commands/break_.rs | 2 +- .../src/core_commands/continue_.rs | 2 +- crates/nu-cmd-lang/src/core_commands/for_.rs | 8 +- crates/nu-cmd-lang/src/core_commands/loop_.rs | 4 +- .../nu-cmd-lang/src/core_commands/return_.rs | 13 +- crates/nu-cmd-lang/src/core_commands/try_.rs | 6 +- .../nu-cmd-lang/src/core_commands/while_.rs | 4 +- crates/nu-command/src/filesystem/ucp.rs | 8 +- crates/nu-command/src/filters/each.rs | 12 +- crates/nu-command/src/filters/items.rs | 2 +- crates/nu-command/src/filters/utils.rs | 5 +- crates/nu-command/src/formats/from/json.rs | 12 +- crates/nu-command/src/formats/from/nuon.rs | 384 +++++++++--------- crates/nu-command/src/platform/du.rs | 5 +- crates/nu-command/src/removed/format.rs | 10 +- crates/nu-command/src/removed/let_env.rs | 10 +- crates/nu-command/src/system/run_external.rs | 8 +- crates/nu-engine/src/eval.rs | 2 +- crates/nu-protocol/src/eval_const.rs | 26 +- crates/nu-protocol/src/shell_error.rs | 91 +++-- 22 files changed, 328 insertions(+), 298 deletions(-) diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index 4442f9e84..6166920dd 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -139,7 +139,7 @@ pub fn evaluate_file( false, ); let pipeline_data = match pipeline_data { - Err(ShellError::Return(_, _)) => { + Err(ShellError::Return { .. }) => { // allows early exists before `main` is run. return Ok(()); } diff --git a/crates/nu-cmd-base/src/arg_glob.rs b/crates/nu-cmd-base/src/arg_glob.rs index 3727e98d3..83df87fa4 100644 --- a/crates/nu-cmd-base/src/arg_glob.rs +++ b/crates/nu-cmd-base/src/arg_glob.rs @@ -64,12 +64,10 @@ fn arg_glob_opt( // user wasn't referring to a specific thing in filesystem, try to glob it. match glob_with_parent(&pattern.item, options, cwd) { Ok(p) => Ok(p), - Err(pat_err) => { - Err(ShellError::InvalidGlobPattern( - pat_err.msg.into(), - pattern.span, // improve specificity - )) - } + Err(pat_err) => Err(ShellError::InvalidGlobPattern { + msg: pat_err.msg.into(), + span: pattern.span, + }), } } diff --git a/crates/nu-cmd-lang/src/core_commands/break_.rs b/crates/nu-cmd-lang/src/core_commands/break_.rs index 0fb4e1213..d257dbfd0 100644 --- a/crates/nu-cmd-lang/src/core_commands/break_.rs +++ b/crates/nu-cmd-lang/src/core_commands/break_.rs @@ -27,7 +27,7 @@ impl Command for Break { call: &Call, _input: PipelineData, ) -> Result { - Err(ShellError::Break(call.head)) + Err(ShellError::Break { span: call.head }) } fn examples(&self) -> Vec { diff --git a/crates/nu-cmd-lang/src/core_commands/continue_.rs b/crates/nu-cmd-lang/src/core_commands/continue_.rs index 308033be1..b6feddf0d 100644 --- a/crates/nu-cmd-lang/src/core_commands/continue_.rs +++ b/crates/nu-cmd-lang/src/core_commands/continue_.rs @@ -27,7 +27,7 @@ impl Command for Continue { call: &Call, _input: PipelineData, ) -> Result { - Err(ShellError::Continue(call.head)) + Err(ShellError::Continue { span: call.head }) } fn examples(&self) -> Vec { diff --git a/crates/nu-cmd-lang/src/core_commands/for_.rs b/crates/nu-cmd-lang/src/core_commands/for_.rs index 039eb3073..0590046d8 100644 --- a/crates/nu-cmd-lang/src/core_commands/for_.rs +++ b/crates/nu-cmd-lang/src/core_commands/for_.rs @@ -113,10 +113,10 @@ impl Command for For { redirect_stdout, redirect_stderr, ) { - Err(ShellError::Break(_)) => { + Err(ShellError::Break { .. }) => { break; } - Err(ShellError::Continue(_)) => { + Err(ShellError::Continue { .. }) => { continue; } Err(err) => { @@ -159,10 +159,10 @@ impl Command for For { redirect_stdout, redirect_stderr, ) { - Err(ShellError::Break(_)) => { + Err(ShellError::Break { .. }) => { break; } - Err(ShellError::Continue(_)) => { + Err(ShellError::Continue { .. }) => { continue; } Err(err) => { diff --git a/crates/nu-cmd-lang/src/core_commands/loop_.rs b/crates/nu-cmd-lang/src/core_commands/loop_.rs index 39db174e0..b7b3d7c54 100644 --- a/crates/nu-cmd-lang/src/core_commands/loop_.rs +++ b/crates/nu-cmd-lang/src/core_commands/loop_.rs @@ -48,10 +48,10 @@ impl Command for Loop { call.redirect_stdout, call.redirect_stderr, ) { - Err(ShellError::Break(_)) => { + Err(ShellError::Break { .. }) => { break; } - Err(ShellError::Continue(_)) => { + Err(ShellError::Continue { .. }) => { continue; } Err(err) => { diff --git a/crates/nu-cmd-lang/src/core_commands/return_.rs b/crates/nu-cmd-lang/src/core_commands/return_.rs index b59500565..5c49e299e 100644 --- a/crates/nu-cmd-lang/src/core_commands/return_.rs +++ b/crates/nu-cmd-lang/src/core_commands/return_.rs @@ -42,12 +42,15 @@ impl Command for Return { ) -> Result { let return_value: Option = call.opt(engine_state, stack, 0)?; if let Some(value) = return_value { - Err(ShellError::Return(call.head, Box::new(value))) + Err(ShellError::Return { + span: call.head, + value: Box::new(value), + }) } else { - Err(ShellError::Return( - call.head, - Box::new(Value::nothing(call.head)), - )) + Err(ShellError::Return { + span: call.head, + value: Box::new(Value::nothing(call.head)), + }) } } diff --git a/crates/nu-cmd-lang/src/core_commands/try_.rs b/crates/nu-cmd-lang/src/core_commands/try_.rs index 989038423..b9dd0e7dc 100644 --- a/crates/nu-cmd-lang/src/core_commands/try_.rs +++ b/crates/nu-cmd-lang/src/core_commands/try_.rs @@ -128,9 +128,9 @@ fn handle_catch( /// `Err` when flow control to bubble up with `?` fn intercept_block_control(error: ShellError) -> Result { match error { - nu_protocol::ShellError::Break(_) => Err(error), - nu_protocol::ShellError::Continue(_) => Err(error), - nu_protocol::ShellError::Return(_, _) => Err(error), + nu_protocol::ShellError::Break { .. } => Err(error), + nu_protocol::ShellError::Continue { .. } => Err(error), + nu_protocol::ShellError::Return { .. } => Err(error), _ => Ok(error), } } diff --git a/crates/nu-cmd-lang/src/core_commands/while_.rs b/crates/nu-cmd-lang/src/core_commands/while_.rs index 27cc401e4..af62092b5 100644 --- a/crates/nu-cmd-lang/src/core_commands/while_.rs +++ b/crates/nu-cmd-lang/src/core_commands/while_.rs @@ -62,10 +62,10 @@ impl Command for While { call.redirect_stdout, call.redirect_stderr, ) { - Err(ShellError::Break(_)) => { + Err(ShellError::Break { .. }) => { break; } - Err(ShellError::Continue(_)) => { + Err(ShellError::Continue { .. }) => { continue; } Err(err) => { diff --git a/crates/nu-command/src/filesystem/ucp.rs b/crates/nu-command/src/filesystem/ucp.rs index 6dce39647..76fcc2163 100644 --- a/crates/nu-command/src/filesystem/ucp.rs +++ b/crates/nu-command/src/filesystem/ucp.rs @@ -193,10 +193,10 @@ impl Command for UCp { app_vals.push(path) } Err(e) => { - return Err(ShellError::ErrorExpandingGlob( - format!("error {} in path {}", e.error(), e.path().display()), - p.span, - )); + return Err(ShellError::ErrorExpandingGlob { + msg: format!("error {} in path {}", e.error(), e.path().display()), + span: p.span, + }); } } } diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index bb951a555..9da693446 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -157,8 +157,8 @@ with 'transpose' first."# redirect_stderr, ) { Ok(v) => Some(v.into_value(span)), - Err(ShellError::Continue(v)) => Some(Value::nothing(v)), - Err(ShellError::Break(_)) => None, + Err(ShellError::Continue { span }) => Some(Value::nothing(span)), + Err(ShellError::Break { .. }) => None, Err(error) => { let error = chain_error_with_input(error, x_is_error, input_span); Some(Value::error(error, input_span)) @@ -180,8 +180,8 @@ with 'transpose' first."# let x = match x { Ok(x) => x, - Err(ShellError::Continue(v)) => return Some(Value::nothing(v)), - Err(ShellError::Break(_)) => return None, + Err(ShellError::Continue { span }) => return Some(Value::nothing(span)), + Err(ShellError::Break { .. }) => return None, Err(err) => return Some(Value::error(err, span)), }; @@ -203,8 +203,8 @@ with 'transpose' first."# redirect_stderr, ) { Ok(v) => Some(v.into_value(span)), - Err(ShellError::Continue(v)) => Some(Value::nothing(v)), - Err(ShellError::Break(_)) => None, + Err(ShellError::Continue { span }) => Some(Value::nothing(span)), + Err(ShellError::Break { .. }) => None, Err(error) => { let error = chain_error_with_input(error, x_is_error, input_span); Some(Value::error(error, input_span)) diff --git a/crates/nu-command/src/filters/items.rs b/crates/nu-command/src/filters/items.rs index dd2948d82..5d3ed4802 100644 --- a/crates/nu-command/src/filters/items.rs +++ b/crates/nu-command/src/filters/items.rs @@ -83,7 +83,7 @@ impl Command for Items { redirect_stderr, ) { Ok(v) => Some(v.into_value(span)), - Err(ShellError::Break(_)) => None, + Err(ShellError::Break { .. }) => None, Err(error) => { let error = chain_error_with_input(error, false, input_span); Some(Value::error(error, span)) diff --git a/crates/nu-command/src/filters/utils.rs b/crates/nu-command/src/filters/utils.rs index be969e751..3ecae0109 100644 --- a/crates/nu-command/src/filters/utils.rs +++ b/crates/nu-command/src/filters/utils.rs @@ -11,7 +11,10 @@ pub fn chain_error_with_input( span: Span, ) -> ShellError { if !input_is_error { - return ShellError::EvalBlockWithInput(span, vec![error_source]); + return ShellError::EvalBlockWithInput { + span, + sources: vec![error_source], + }; } error_source } diff --git a/crates/nu-command/src/formats/from/json.rs b/crates/nu-command/src/formats/from/json.rs index 64ffe485e..61488f9d8 100644 --- a/crates/nu-command/src/formats/from/json.rs +++ b/crates/nu-command/src/formats/from/json.rs @@ -155,12 +155,12 @@ fn convert_string_to_value(string_input: String, span: Span) -> Result Err(ShellError::CantConvert { diff --git a/crates/nu-command/src/formats/from/nuon.rs b/crates/nu-command/src/formats/from/nuon.rs index 9f4fb2bb1..b47721bef 100644 --- a/crates/nu-command/src/formats/from/nuon.rs +++ b/crates/nu-command/src/formats/from/nuon.rs @@ -65,12 +65,12 @@ impl Command for FromNuon { msg: "could not load nuon text".into(), span: Some(head), help: None, - inner: vec![ShellError::OutsideSpannedLabeledError( - string_input, - "error when loading".into(), - "excess values when loading".into(), - element.span(), - )], + inner: vec![ShellError::OutsideSpannedLabeledError { + src: string_input, + error: "error when loading".into(), + msg: "excess values when loading".into(), + span: element.span(), + }], }); } else { return Err(ShellError::GenericError { @@ -105,12 +105,12 @@ impl Command for FromNuon { msg: "could not load nuon text".into(), span: Some(head), help: None, - inner: vec![ShellError::OutsideSpannedLabeledError( - string_input, - "error when loading".into(), - "detected a pipeline in nuon file".into(), - expr.span(), - )], + inner: vec![ShellError::OutsideSpannedLabeledError { + src: string_input, + error: "error when loading".into(), + msg: "detected a pipeline in nuon file".into(), + span: expr.span(), + }], }); } @@ -145,12 +145,12 @@ impl Command for FromNuon { msg: "could not parse nuon text".into(), span: Some(head), help: None, - inner: vec![ShellError::OutsideSpannedLabeledError( - string_input, - "error when parsing".into(), - err.to_string(), - err.span(), - )], + inner: vec![ShellError::OutsideSpannedLabeledError { + src: string_input, + error: "error when parsing".into(), + msg: err.to_string(), + span: err.span(), + }], }); } @@ -175,99 +175,99 @@ fn convert_to_value( original_text: &str, ) -> Result { match expr.expr { - Expr::BinaryOp(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "binary operators not supported in nuon".into(), - expr.span, - )), - Expr::UnaryNot(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "unary operators not supported in nuon".into(), - expr.span, - )), - Expr::Block(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "blocks not supported in nuon".into(), - expr.span, - )), - Expr::Closure(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "closures not supported in nuon".into(), - expr.span, - )), + Expr::BinaryOp(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "binary operators not supported in nuon".into(), + span: expr.span, + }), + Expr::UnaryNot(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "unary operators not supported in nuon".into(), + span: expr.span, + }), + Expr::Block(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "blocks not supported in nuon".into(), + span: expr.span, + }), + Expr::Closure(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "closures not supported in nuon".into(), + span: expr.span, + }), Expr::Binary(val) => Ok(Value::binary(val, span)), Expr::Bool(val) => Ok(Value::bool(val, span)), - Expr::Call(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "calls not supported in nuon".into(), - expr.span, - )), - Expr::CellPath(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "subexpressions and cellpaths not supported in nuon".into(), - expr.span, - )), + Expr::Call(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "calls not supported in nuon".into(), + span: expr.span, + }), + Expr::CellPath(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "subexpressions and cellpaths not supported in nuon".into(), + span: expr.span, + }), Expr::DateTime(dt) => Ok(Value::date(dt, span)), - Expr::ExternalCall(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "calls not supported in nuon".into(), - expr.span, - )), + Expr::ExternalCall(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "calls not supported in nuon".into(), + span: expr.span, + }), Expr::Filepath(val) => Ok(Value::string(val, span)), Expr::Directory(val) => Ok(Value::string(val, span)), Expr::Float(val) => Ok(Value::float(val, span)), Expr::FullCellPath(full_cell_path) => { if !full_cell_path.tail.is_empty() { - Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "subexpressions and cellpaths not supported in nuon".into(), - expr.span, - )) + Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "subexpressions and cellpaths not supported in nuon".into(), + span: expr.span, + }) } else { convert_to_value(full_cell_path.head, span, original_text) } } - Expr::Garbage => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "extra tokens in input file".into(), - expr.span, - )), - Expr::MatchPattern(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "extra tokens in input file".into(), - expr.span, - )), + Expr::Garbage => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "extra tokens in input file".into(), + span: expr.span, + }), + Expr::MatchPattern(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "extra tokens in input file".into(), + span: expr.span, + }), Expr::GlobPattern(val) => Ok(Value::string(val, span)), - Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "imports not supported in nuon".into(), - expr.span, - )), - Expr::Overlay(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "overlays not supported in nuon".into(), - expr.span, - )), + Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "imports not supported in nuon".into(), + span: expr.span, + }), + Expr::Overlay(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "overlays not supported in nuon".into(), + span: expr.span, + }), Expr::Int(val) => Ok(Value::int(val, span)), - Expr::Keyword(kw, ..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - format!("{} not supported in nuon", String::from_utf8_lossy(&kw)), - expr.span, - )), + Expr::Keyword(kw, ..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: format!("{} not supported in nuon", String::from_utf8_lossy(&kw)), + span: expr.span, + }), Expr::List(vals) => { let mut output = vec![]; for val in vals { @@ -276,19 +276,19 @@ fn convert_to_value( Ok(Value::list(output, span)) } - Expr::MatchBlock(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "match blocks not supported in nuon".into(), - expr.span, - )), + Expr::MatchBlock(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "match blocks not supported in nuon".into(), + span: expr.span, + }), Expr::Nothing => Ok(Value::nothing(span)), - Expr::Operator(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "operators not supported in nuon".into(), - expr.span, - )), + Expr::Operator(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "operators not supported in nuon".into(), + span: expr.span, + }), Expr::Range(from, next, to, operator) => { let from = if let Some(f) = from { convert_to_value(*f, span, original_text)? @@ -322,12 +322,12 @@ fn convert_to_value( let key_str = match key.expr { Expr::String(key_str) => key_str, _ => { - return Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "only strings can be keys".into(), - key.span, - )) + return Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "only strings can be keys".into(), + span: key.span, + }) } }; @@ -336,49 +336,49 @@ fn convert_to_value( record.push(key_str, value); } RecordItem::Spread(_, inner) => { - return Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "spread operator not supported in nuon".into(), - inner.span, - )); + return Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "spread operator not supported in nuon".into(), + span: inner.span, + }); } } } Ok(Value::record(record, span)) } - Expr::RowCondition(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "row conditions not supported in nuon".into(), - expr.span, - )), - Expr::Signature(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "signatures not supported in nuon".into(), - expr.span, - )), - Expr::Spread(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "spread operator not supported in nuon".into(), - expr.span, - )), + Expr::RowCondition(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "row conditions not supported in nuon".into(), + span: expr.span, + }), + Expr::Signature(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "signatures not supported in nuon".into(), + span: expr.span, + }), + Expr::Spread(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "spread operator not supported in nuon".into(), + span: expr.span, + }), Expr::String(s) => Ok(Value::string(s, span)), - Expr::StringInterpolation(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "string interpolation not supported in nuon".into(), - expr.span, - )), - Expr::Subexpression(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "subexpressions not supported in nuon".into(), - expr.span, - )), + Expr::StringInterpolation(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "string interpolation not supported in nuon".into(), + span: expr.span, + }), + Expr::Subexpression(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "subexpressions not supported in nuon".into(), + span: expr.span, + }), Expr::Table(mut headers, cells) => { let mut cols = vec![]; @@ -388,12 +388,12 @@ fn convert_to_value( let key_str = match &mut key.expr { Expr::String(key_str) => key_str, _ => { - return Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "only strings can be keys".into(), - expr.span, - )) + return Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "only strings can be keys".into(), + span: expr.span, + }) } }; @@ -416,12 +416,12 @@ fn convert_to_value( } if cols.len() != vals.len() { - return Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "table has mismatched columns".into(), - expr.span, - )); + return Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "table has mismatched columns".into(), + span: expr.span, + }); } output.push(Value::record( @@ -439,12 +439,12 @@ fn convert_to_value( let size = match val.expr { Expr::Int(val) => val, _ => { - return Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "non-integer unit value".into(), - expr.span, - )) + return Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "non-integer unit value".into(), + span: expr.span, + }) } }; @@ -484,37 +484,37 @@ fn convert_to_value( Unit::Hour => Ok(Value::duration(size * 1000 * 1000 * 1000 * 60 * 60, span)), Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) { Some(val) => Ok(Value::duration(val, span)), - None => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "day duration too large".into(), - "day duration too large".into(), - expr.span, - )), + None => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "day duration too large".into(), + msg: "day duration too large".into(), + span: expr.span, + }), }, Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) { Some(val) => Ok(Value::duration(val, span)), - None => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "week duration too large".into(), - "week duration too large".into(), - expr.span, - )), + None => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "week duration too large".into(), + msg: "week duration too large".into(), + span: expr.span, + }), }, } } - Expr::Var(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "variables not supported in nuon".into(), - expr.span, - )), - Expr::VarDecl(..) => Err(ShellError::OutsideSpannedLabeledError( - original_text.to_string(), - "Error when loading".into(), - "variable declarations not supported in nuon".into(), - expr.span, - )), + Expr::Var(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "variables not supported in nuon".into(), + span: expr.span, + }), + Expr::VarDecl(..) => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.to_string(), + error: "Error when loading".into(), + msg: "variable declarations not supported in nuon".into(), + span: expr.span, + }), } } diff --git a/crates/nu-command/src/platform/du.rs b/crates/nu-command/src/platform/du.rs index 3519b4ba7..52d0836c8 100644 --- a/crates/nu-command/src/platform/du.rs +++ b/crates/nu-command/src/platform/du.rs @@ -108,7 +108,10 @@ impl Command for Du { let exclude = args.exclude.map_or(Ok(None), move |x| { Pattern::new(&x.item) .map(Some) - .map_err(|e| ShellError::InvalidGlobPattern(e.msg.to_string(), x.span)) + .map_err(|e| ShellError::InvalidGlobPattern { + msg: e.msg.into(), + span: x.span, + }) })?; let include_files = args.all; diff --git a/crates/nu-command/src/removed/format.rs b/crates/nu-command/src/removed/format.rs index de5349eae..07a86c36c 100644 --- a/crates/nu-command/src/removed/format.rs +++ b/crates/nu-command/src/removed/format.rs @@ -37,10 +37,10 @@ impl Command for SubCommand { call: &Call, _input: PipelineData, ) -> Result { - Err(nu_protocol::ShellError::RemovedCommand( - self.name().to_string(), - "format date".to_owned(), - call.head, - )) + Err(nu_protocol::ShellError::RemovedCommand { + removed: self.name().to_string(), + replacement: "format date".to_owned(), + span: call.head, + }) } } diff --git a/crates/nu-command/src/removed/let_env.rs b/crates/nu-command/src/removed/let_env.rs index 51de55a94..a5c186a8c 100644 --- a/crates/nu-command/src/removed/let_env.rs +++ b/crates/nu-command/src/removed/let_env.rs @@ -34,10 +34,10 @@ impl Command for LetEnv { call: &Call, _: PipelineData, ) -> Result { - Err(nu_protocol::ShellError::RemovedCommand( - self.name().to_string(), - "$env. = ...".to_owned(), - call.head, - )) + Err(nu_protocol::ShellError::RemovedCommand { + removed: self.name().to_string(), + replacement: "$env. = ...".to_owned(), + span: call.head, + }) } } diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 2de850281..dab7ed30d 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -305,11 +305,11 @@ impl ExternalCommand { Some(s) => s.clone(), None => "".to_string(), }; - return Err(ShellError::RemovedCommand( - command_name_lower, + return Err(ShellError::RemovedCommand { + removed: command_name_lower, replacement, - self.name.span, - )); + span: self.name.span, + }); } let suggestion = suggest_command(&self.name.item, engine_state); diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 5317abda1..b4a3b632e 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -593,7 +593,7 @@ pub fn eval_block_with_early_return( redirect_stdout, redirect_stderr, ) { - Err(ShellError::Return(_, value)) => Ok(PipelineData::Value(*value, None)), + Err(ShellError::Return { span: _, value }) => Ok(PipelineData::Value(*value, None)), x => x, } } diff --git a/crates/nu-protocol/src/eval_const.rs b/crates/nu-protocol/src/eval_const.rs index 6eff45ce9..e954518aa 100644 --- a/crates/nu-protocol/src/eval_const.rs +++ b/crates/nu-protocol/src/eval_const.rs @@ -212,13 +212,13 @@ fn eval_const_call( let decl = working_set.get_decl(call.decl_id); if !decl.is_const() { - return Err(ShellError::NotAConstCommand(call.head)); + return Err(ShellError::NotAConstCommand { span: call.head }); } if !decl.is_known_external() && call.named_iter().any(|(flag, _, _)| flag.item == "help") { // It would require re-implementing get_full_help() for const evaluation. Assuming that // getting help messages at parse-time is rare enough, we can simply disallow it. - return Err(ShellError::NotAConstHelp(call.head)); + return Err(ShellError::NotAConstHelp { span: call.head }); } decl.run_const(working_set, call, input) @@ -233,7 +233,7 @@ pub fn eval_const_subexpression( for pipeline in block.pipelines.iter() { for element in pipeline.elements.iter() { let PipelineElement::Expression(_, expr) = element else { - return Err(ShellError::NotAConstant(span)); + return Err(ShellError::NotAConstant { span }); }; input = eval_constant_with_input(working_set, expr, input)? @@ -288,7 +288,7 @@ impl Eval for EvalConst { _: String, span: Span, ) -> Result { - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn eval_var( @@ -299,7 +299,7 @@ impl Eval for EvalConst { ) -> Result { match working_set.get_variable(var_id).const_val.as_ref() { Some(val) => Ok(val.clone()), - None => Err(ShellError::NotAConstant(span)), + None => Err(ShellError::NotAConstant { span }), } } @@ -322,7 +322,7 @@ impl Eval for EvalConst { span: Span, ) -> Result { // TODO: It may be more helpful to give not_a_const_command error - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn eval_subexpression( @@ -346,7 +346,7 @@ impl Eval for EvalConst { _: bool, expr_span: Span, ) -> Result { - Err(ShellError::NotAConstant(expr_span)) + Err(ShellError::NotAConstant { span: expr_span }) } fn eval_assignment( @@ -358,7 +358,7 @@ impl Eval for EvalConst { _op_span: Span, expr_span: Span, ) -> Result { - Err(ShellError::NotAConstant(expr_span)) + Err(ShellError::NotAConstant { span: expr_span }) } fn eval_row_condition_or_closure( @@ -367,7 +367,7 @@ impl Eval for EvalConst { _: usize, span: Span, ) -> Result { - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn eval_string_interpolation( @@ -376,11 +376,11 @@ impl Eval for EvalConst { _: &[Expression], span: Span, ) -> Result { - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn eval_overlay(_: &StateWorkingSet, span: Span) -> Result { - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn eval_glob_pattern( @@ -389,10 +389,10 @@ impl Eval for EvalConst { _: String, span: Span, ) -> Result { - Err(ShellError::NotAConstant(span)) + Err(ShellError::NotAConstant { span }) } fn unreachable(expr: &Expression) -> Result { - Err(ShellError::NotAConstant(expr.span)) + Err(ShellError::NotAConstant { span: expr.span }) } } diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 5e6f8004d..29aca7802 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -944,14 +944,6 @@ pub enum ShellError { span: Span, }, - // These three are unused. Remove? - #[error("No file to be removed")] - NoFileToBeRemoved(), - #[error("No file to be moved")] - NoFileToBeMoved(), - #[error("No file to be copied")] - NoFileToBeCopied(), - /// Error while trying to read a file /// /// ## Resolution @@ -1084,22 +1076,30 @@ pub enum ShellError { }, /// This is a generic error type used for different situations. - #[error("{1}")] + #[error("{error}")] #[diagnostic()] - OutsideSpannedLabeledError(#[source_code] String, String, String, #[label("{2}")] Span), + OutsideSpannedLabeledError { + #[source_code] + src: String, + error: String, + msg: String, + #[label("{msg}")] + span: Span, + }, /// Attempted to use a command that has been removed from Nushell. /// /// ## Resolution /// /// Check the help for the new suggested command and update your script accordingly. - #[error("Removed command: {0}")] + #[error("Removed command: {removed}")] #[diagnostic(code(nu::shell::removed_command))] - RemovedCommand( - String, - String, - #[label = "'{0}' has been removed from Nushell. Please use '{1}' instead."] Span, - ), + RemovedCommand { + removed: String, + replacement: String, + #[label("'{removed}' has been removed from Nushell. Please use '{replacement}' instead.")] + span: Span, + }, /// Non-Unicode input received. /// @@ -1110,32 +1110,38 @@ pub enum ShellError { #[diagnostic(code(nu::shell::non_unicode_input))] NonUnicodeInput, - /// Unexpected abbr component. - /// - /// ## Resolution - /// - /// Check the path abbreviation to ensure that it is valid. - #[error("Unexpected abbr component `{0}`.")] - #[diagnostic(code(nu::shell::unexpected_path_abbreviateion))] - UnexpectedAbbrComponent(String), - // It should be only used by commands accepts block, and accept inputs from pipeline. /// Failed to eval block with specific pipeline input. #[error("Eval block failed with pipeline input")] #[diagnostic(code(nu::shell::eval_block_with_input))] - EvalBlockWithInput(#[label("source value")] Span, #[related] Vec), + EvalBlockWithInput { + #[label("source value")] + span: Span, + #[related] + sources: Vec, + }, /// Break event, which may become an error if used outside of a loop #[error("Break used outside of loop")] - Break(#[label = "used outside of loop"] Span), + Break { + #[label("used outside of loop")] + span: Span, + }, /// Continue event, which may become an error if used outside of a loop #[error("Continue used outside of loop")] - Continue(#[label = "used outside of loop"] Span), + Continue { + #[label("used outside of loop")] + span: Span, + }, /// Return event, which may become an error if used outside of a function #[error("Return used outside of function")] - Return(#[label = "used outside of function"] Span, Box), + Return { + #[label("used outside of function")] + span: Span, + value: Box, + }, /// The code being executed called itself too many times. /// @@ -1206,7 +1212,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe code(nu::shell::not_a_constant), help("Only a subset of expressions are allowed constants during parsing. Try using the 'const' command or typing the value literally.") )] - NotAConstant(#[label = "Value is not a parse-time constant"] Span), + NotAConstant { + #[label("Value is not a parse-time constant")] + span: Span, + }, /// Tried running a command that is not const-compatible /// @@ -1219,7 +1228,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe code(nu::shell::not_a_const_command), help("Only a subset of builtin commands, and custom commands built only from those commands, can run at parse time.") )] - NotAConstCommand(#[label = "This command cannot run at parse time."] Span), + NotAConstCommand { + #[label("This command cannot run at parse time.")] + span: Span, + }, /// Tried getting a help message at parse time. /// @@ -1231,7 +1243,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe code(nu::shell::not_a_const_help), help("Help messages are currently not supported to be constants.") )] - NotAConstHelp(#[label = "Cannot get help message at parse time."] Span), + NotAConstHelp { + #[label("This command cannot run at parse time.")] + span: Span, + }, /// Invalid glob pattern /// @@ -1243,7 +1258,11 @@ This is an internal Nushell error, please file an issue https://github.com/nushe code(nu::shell::invalid_glob_pattern), help("Refer to xxx for help on nushell glob patterns.") )] - InvalidGlobPattern(String, #[label = "{0}"] Span), + InvalidGlobPattern { + msg: String, + #[label("{msg}")] + span: Span, + }, /// Error expanding glob pattern /// @@ -1256,7 +1275,11 @@ This is an internal Nushell error, please file an issue https://github.com/nushe help("Correct glob pattern or file access issue") )] //todo: add error detail - ErrorExpandingGlob(String, #[label = "{0}"] Span), + ErrorExpandingGlob { + msg: String, + #[label("{msg}")] + span: Span, + }, /// Tried spreading a non-list inside a list. ///