diff --git a/crates/nu-cli/src/commands/commandline.rs b/crates/nu-cli/src/commands/commandline.rs index 5945fc86fe..646b5cd127 100644 --- a/crates/nu-cli/src/commands/commandline.rs +++ b/crates/nu-cli/src/commands/commandline.rs @@ -84,7 +84,7 @@ impl Command for Commandline { return Err(ShellError::CantConvert { to_type: "int".to_string(), from_type: "string".to_string(), - span: cmd.span()?, + span: cmd.span(), help: Some(format!( r#"string "{cmd_str}" does not represent a valid integer"# )), diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index e28de2f084..748b92d4bc 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -185,7 +185,7 @@ pub(crate) fn print_table_or_error( // Change the engine_state config to use the passed in configuration engine_state.set_config(config); - if let PipelineData::Value(Value::Error { error }, ..) = &pipeline_data { + if let PipelineData::Value(Value::Error { error, .. }, ..) = &pipeline_data { let working_set = StateWorkingSet::new(engine_state); report_error(&working_set, &**error); std::process::exit(1); @@ -232,7 +232,7 @@ pub(crate) fn print_table_or_error( fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) { for item in pipeline_data { - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { let working_set = StateWorkingSet::new(engine_state); report_error(&working_set, &*error); diff --git a/crates/nu-cli/src/nu_highlight.rs b/crates/nu-cli/src/nu_highlight.rs index 1e8df9f719..a8557ba0a7 100644 --- a/crates/nu-cli/src/nu_highlight.rs +++ b/crates/nu-cli/src/nu_highlight.rs @@ -55,6 +55,7 @@ impl Command for NuHighlight { } Err(err) => Value::Error { error: Box::new(err), + span: head, }, }, ctrlc, diff --git a/crates/nu-cli/src/reedline_config.rs b/crates/nu-cli/src/reedline_config.rs index fad763503b..7916e99e8c 100644 --- a/crates/nu-cli/src/reedline_config.rs +++ b/crates/nu-cli/src/reedline_config.rs @@ -141,14 +141,14 @@ fn add_menu( _ => Err(ShellError::UnsupportedConfigValue( "columnar, list or description".to_string(), menu.menu_type.into_abbreviated_string(config), - menu.menu_type.span()?, + menu.menu_type.span(), )), } } else { Err(ShellError::UnsupportedConfigValue( "only record type".to_string(), menu.menu_type.into_abbreviated_string(config), - menu.menu_type.span()?, + menu.menu_type.span(), )) } } @@ -264,7 +264,7 @@ pub(crate) fn add_columnar_menu( _ => Err(ShellError::UnsupportedConfigValue( "block or omitted value".to_string(), menu.source.into_abbreviated_string(config), - menu.source.span()?, + menu.source.span(), )), } } @@ -347,7 +347,7 @@ pub(crate) fn add_list_menu( _ => Err(ShellError::UnsupportedConfigValue( "block or omitted value".to_string(), menu.source.into_abbreviated_string(config), - menu.source.span()?, + menu.source.span(), )), } } @@ -466,7 +466,7 @@ pub(crate) fn add_description_menu( _ => Err(ShellError::UnsupportedConfigValue( "closure or omitted value".to_string(), menu.source.into_abbreviated_string(config), - menu.source.span()?, + menu.source.span(), )), } } @@ -603,7 +603,7 @@ fn add_keybinding( v => Err(ShellError::UnsupportedConfigValue( "string or list of strings".to_string(), v.into_abbreviated_string(config), - v.span()?, + v.span(), )), } } @@ -633,7 +633,7 @@ fn add_parsed_keybinding( return Err(ShellError::UnsupportedConfigValue( "CONTROL, SHIFT, ALT or NONE".to_string(), keybinding.modifier.into_abbreviated_string(config), - keybinding.modifier.span()?, + keybinding.modifier.span(), )) } }; @@ -657,7 +657,7 @@ fn add_parsed_keybinding( return Err(ShellError::UnsupportedConfigValue( "char_".to_string(), c.to_string(), - keybinding.keycode.span()?, + keybinding.keycode.span(), )); }; @@ -684,7 +684,7 @@ fn add_parsed_keybinding( .ok_or(ShellError::UnsupportedConfigValue( "(f1|f2|...|f20)".to_string(), format!("unknown function key: {c}"), - keybinding.keycode.span()?, + keybinding.keycode.span(), ))?; KeyCode::F(fn_num) } @@ -694,7 +694,7 @@ fn add_parsed_keybinding( return Err(ShellError::UnsupportedConfigValue( "crossterm KeyCode".to_string(), keybinding.keycode.into_abbreviated_string(config), - keybinding.keycode.span()?, + keybinding.keycode.span(), )) } }; @@ -751,7 +751,7 @@ fn parse_event(value: &Value, config: &Config) -> Result, None => Err(ShellError::UnsupportedConfigValue( "List containing valid events".to_string(), "Nothing value (null)".to_string(), - value.span()?, + value.span(), )), Some(event) => Ok(event), }, @@ -764,7 +764,7 @@ fn parse_event(value: &Value, config: &Config) -> Result, v => Err(ShellError::UnsupportedConfigValue( "list of events".to_string(), v.into_abbreviated_string(config), - v.span()?, + v.span(), )), }, }, @@ -776,7 +776,7 @@ fn parse_event(value: &Value, config: &Config) -> Result, None => Err(ShellError::UnsupportedConfigValue( "List containing valid events".to_string(), "Nothing value (null)".to_string(), - value.span()?, + value.span(), )), Some(event) => Ok(event), }, @@ -790,7 +790,7 @@ fn parse_event(value: &Value, config: &Config) -> Result, v => Err(ShellError::UnsupportedConfigValue( "record or list of records, null to unbind key".to_string(), v.into_abbreviated_string(config), - v.span()?, + v.span(), )), } } @@ -965,7 +965,7 @@ fn edit_from_record( } fn extract_char(value: &Value, config: &Config) -> Result { - let span = value.span()?; + let span = value.span(); value .into_string("", config) .chars() diff --git a/crates/nu-cmd-base/src/input_handler.rs b/crates/nu-cmd-base/src/input_handler.rs index 73b831af88..70e063f411 100644 --- a/crates/nu-cmd-base/src/input_handler.rs +++ b/crates/nu-cmd-base/src/input_handler.rs @@ -78,6 +78,7 @@ where if let Err(error) = r { return Value::Error { error: Box::new(error), + span, }; } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/filter_with.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/filter_with.rs index 88c40e336c..e302ee24b5 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/filter_with.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/filter_with.rs @@ -92,7 +92,7 @@ fn command_eager( df: NuDataFrame, ) -> Result { let mask_value: Value = call.req(engine_state, stack, 0)?; - let mask_span = mask_value.span()?; + let mask_span = mask_value.span(); if NuExpression::can_downcast(&mask_value) { let expression = NuExpression::try_from_value(mask_value)?; diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/rename.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/rename.rs index 67cd8649f3..9729b6d43f 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/rename.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/rename.rs @@ -160,7 +160,7 @@ fn command_lazy( let value: Value = call.req(engine_state, stack, 1)?; return Err(ShellError::IncompatibleParametersSingle { msg: "New name list has different size to column list".into(), - span: value.span()?, + span: value.span(), }); } diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/summary.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/summary.rs index 7f30ed1b2d..f92cb0a0b2 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/summary.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/summary.rs @@ -134,16 +134,17 @@ fn command( )) } } - _ => match value.span() { - Ok(span) => Err(ShellError::GenericError( + Value::Error { error, .. } => Err(*error.clone()), + _ => { + let span = value.span(); + Err(ShellError::GenericError( "Incorrect value for quantile".to_string(), "value should be a float".to_string(), Some(span), None, Vec::new(), - )), - Err(e) => Err(e), - }, + )) + } }) .collect::, ShellError>>() }); diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/take.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/take.rs index 4d2a4681fa..99b314c3b4 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/take.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/take.rs @@ -93,7 +93,7 @@ fn command( input: PipelineData, ) -> Result { let index_value: Value = call.req(engine_state, stack, 0)?; - let index_span = index_value.span()?; + let index_span = index_value.span(); let index = NuDataFrame::try_from_value(index_value)?.as_series(index_span)?; let casted = match index.dtype() { diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/with_column.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/with_column.rs index 17b195ec5f..53e57501e2 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/with_column.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/with_column.rs @@ -114,7 +114,7 @@ impl Command for WithColumn { Err(ShellError::CantConvert { to_type: "lazy or eager dataframe".into(), from_type: value.get_type().to_string(), - span: value.span()?, + span: value.span(), help: None, }) } @@ -128,7 +128,7 @@ fn command_eager( mut df: NuDataFrame, ) -> Result { let new_column: Value = call.req(engine_state, stack, 0)?; - let column_span = new_column.span()?; + let column_span = new_column.span(); if NuExpression::can_downcast(&new_column) { let vals: Vec = call.rest(engine_state, stack, 0)?; diff --git a/crates/nu-cmd-dataframe/src/dataframe/lazy/fill_nan.rs b/crates/nu-cmd-dataframe/src/dataframe/lazy/fill_nan.rs index 9406fe3443..bc5f9ec182 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/lazy/fill_nan.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/lazy/fill_nan.rs @@ -93,7 +93,7 @@ impl Command for LazyFillNA { None, )) } else { - let val_span = value.span()?; + let val_span = value.span(); let frame = NuDataFrame::try_from_value(value)?; let columns = frame.columns(val_span)?; let dataframe = columns diff --git a/crates/nu-cmd-dataframe/src/dataframe/lazy/groupby.rs b/crates/nu-cmd-dataframe/src/dataframe/lazy/groupby.rs index 99d4897819..9a1a2bb887 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/lazy/groupby.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/lazy/groupby.rs @@ -126,7 +126,7 @@ impl Command for ToLazyGroupBy { let value: Value = call.req(engine_state, stack, 0)?; return Err(ShellError::IncompatibleParametersSingle { msg: "Expected only Col expressions".into(), - span: value.span()?, + span: value.span(), }); } diff --git a/crates/nu-cmd-dataframe/src/dataframe/lazy/join.rs b/crates/nu-cmd-dataframe/src/dataframe/lazy/join.rs index 58f4d6be86..e51cc84ad7 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/lazy/join.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/lazy/join.rs @@ -199,7 +199,7 @@ impl Command for LazyJoin { let right_on: Value = call.req(engine_state, stack, 2)?; return Err(ShellError::IncompatibleParametersSingle { msg: "The right column list has a different size to the left column list".into(), - span: right_on.span()?, + span: right_on.span(), }); } @@ -209,7 +209,7 @@ impl Command for LazyJoin { let value: Value = call.req(engine_state, stack, *index)?; return Err(ShellError::IncompatibleParametersSingle { msg: "Expected only a string, col expressions or list of strings".into(), - span: value.span()?, + span: value.span(), }); } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/lazy/sort_by_expr.rs b/crates/nu-cmd-dataframe/src/dataframe/lazy/sort_by_expr.rs index 5a3ca4e150..302117b15c 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/lazy/sort_by_expr.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/lazy/sort_by_expr.rs @@ -118,7 +118,7 @@ impl Command for LazySortBy { let span = call .get_flag::(engine_state, stack, "reverse")? .expect("already checked and it exists") - .span()?; + .span(); return Err(ShellError::GenericError( "Incorrect list size".into(), "Size doesn't match expression list".into(), diff --git a/crates/nu-cmd-dataframe/src/dataframe/series/indexes/set_with_idx.rs b/crates/nu-cmd-dataframe/src/dataframe/series/indexes/set_with_idx.rs index 575111534e..8731996b58 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/series/indexes/set_with_idx.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/series/indexes/set_with_idx.rs @@ -82,7 +82,7 @@ fn command( let indices_value: Value = call .get_flag(engine_state, stack, "indices")? .expect("required named value"); - let indices_span = indices_value.span()?; + let indices_span = indices_value.span(); let indices = NuDataFrame::try_from_value(indices_value)?.as_series(indices_span)?; let casted = match indices.dtype() { @@ -204,7 +204,7 @@ fn command( "this value cannot be set in a series of type '{}'", series.dtype() ), - Some(value.span()?), + Some(value.span()), None, Vec::new(), )), diff --git a/crates/nu-cmd-dataframe/src/dataframe/series/masks/is_in.rs b/crates/nu-cmd-dataframe/src/dataframe/series/masks/is_in.rs index 687b5e78d2..f9a0dab394 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/series/masks/is_in.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/series/masks/is_in.rs @@ -74,7 +74,7 @@ fn command( let df = NuDataFrame::try_from_pipeline(input, call.head)?; let other_value: Value = call.req(engine_state, stack, 0)?; - let other_span = other_value.span()?; + let other_span = other_value.span(); let other_df = NuDataFrame::try_from_value(other_value)?; let other = other_df.as_series(other_span)?; diff --git a/crates/nu-cmd-dataframe/src/dataframe/series/masks/set.rs b/crates/nu-cmd-dataframe/src/dataframe/series/masks/set.rs index 2d2ed72061..d948667222 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/series/masks/set.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/series/masks/set.rs @@ -81,7 +81,7 @@ fn command( let mask_value: Value = call .get_flag(engine_state, stack, "mask")? .expect("required named value"); - let mask_span = mask_value.span()?; + let mask_span = mask_value.span(); let mask = NuDataFrame::try_from_value(mask_value)?.as_series(mask_span)?; let bool_mask = match mask.dtype() { @@ -185,7 +185,7 @@ fn command( "this value cannot be set in a series of type '{}'", series.dtype() ), - Some(value.span()?), + Some(value.span()), None, Vec::new(), )), diff --git a/crates/nu-cmd-dataframe/src/dataframe/series/string/concatenate.rs b/crates/nu-cmd-dataframe/src/dataframe/series/string/concatenate.rs index 1152bcad63..db9e371f0d 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/series/string/concatenate.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/series/string/concatenate.rs @@ -74,7 +74,7 @@ fn command( let df = NuDataFrame::try_from_pipeline(input, call.head)?; let other: Value = call.req(engine_state, stack, 0)?; - let other_span = other.span()?; + let other_span = other.span(); let other_df = NuDataFrame::try_from_value(other)?; let other_series = other_df.as_series(other_span)?; diff --git a/crates/nu-cmd-dataframe/src/dataframe/utils.rs b/crates/nu-cmd-dataframe/src/dataframe/utils.rs index 982797cef2..c7dc75eede 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/utils.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/utils.rs @@ -9,7 +9,7 @@ pub fn extract_strings(value: Value) -> Result, ShellError> { (Err(_), Ok(cols)) => Ok(cols), _ => Err(ShellError::IncompatibleParametersSingle { msg: "Expected a string or list of strings".into(), - span: value.span()?, + span: value.span(), }), } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs index 88ba19d993..fccd9c81dc 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/between_values.rs @@ -17,7 +17,7 @@ pub(super) fn between_dataframes( right: &Value, rhs: &NuDataFrame, ) -> Result { - let operation_span = span(&[left.span()?, right.span()?]); + let operation_span = span(&[left.span(), right.span()]); match operator.item { Operator::Math(Math::Plus) => match lhs.append_df(rhs, Axis::Row, operation_span) { Ok(df) => Ok(df.into_value(operation_span)), @@ -26,9 +26,9 @@ pub(super) fn between_dataframes( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), } } @@ -40,7 +40,7 @@ pub(super) fn compute_between_series( right: &Value, rhs: &Series, ) -> Result { - let operation_span = span(&[left.span()?, right.span()?]); + let operation_span = span(&[left.span(), right.span()]); match operator.item { Operator::Math(Math::Plus) => { let mut res = lhs + rhs; @@ -71,7 +71,7 @@ pub(super) fn compute_between_series( Err(e) => Err(ShellError::GenericError( "Division error".into(), e.to_string(), - Some(right.span()?), + Some(right.span()), None, Vec::new(), )), @@ -79,32 +79,32 @@ pub(super) fn compute_between_series( } Operator::Comparison(Comparison::Equal) => { let name = format!("eq_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Comparison(Comparison::NotEqual) => { let name = format!("neq_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Comparison(Comparison::LessThan) => { let name = format!("lt_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Comparison(Comparison::LessThanOrEqual) => { let name = format!("lte_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Comparison(Comparison::GreaterThan) => { let name = format!("gt_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Comparison(Comparison::GreaterThanOrEqual) => { let name = format!("gte_{}_{}", lhs.name(), rhs.name()); - let res = compare_series(lhs, rhs, name.as_str(), right.span().ok(), Series::equal)?; + let res = compare_series(lhs, rhs, name.as_str(), right.span(), Series::equal)?; NuDataFrame::series_to_value(res, operation_span) } Operator::Boolean(Boolean::And) => match lhs.dtype() { @@ -122,7 +122,7 @@ pub(super) fn compute_between_series( _ => Err(ShellError::GenericError( "Incompatible types".into(), "unable to cast to boolean".into(), - Some(right.span()?), + Some(right.span()), None, Vec::new(), )), @@ -151,7 +151,7 @@ pub(super) fn compute_between_series( _ => Err(ShellError::GenericError( "Incompatible types".into(), "unable to cast to boolean".into(), - Some(right.span()?), + Some(right.span()), None, Vec::new(), )), @@ -168,9 +168,9 @@ pub(super) fn compute_between_series( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), } } @@ -179,7 +179,7 @@ fn compare_series<'s, F>( lhs: &'s Series, rhs: &'s Series, name: &'s str, - span: Option, + span: Span, f: F, ) -> Result where @@ -190,7 +190,7 @@ where ShellError::GenericError( "Equality error".into(), e.to_string(), - span, + Some(span), None, Vec::new(), ) @@ -211,13 +211,13 @@ pub(super) fn compute_series_single_value( return Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }); } - let lhs_span = left.span()?; + let lhs_span = left.span(); let lhs = lhs.as_series(lhs_span)?; match operator.item { @@ -232,9 +232,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Math(Math::Minus) => match &right { @@ -247,9 +247,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Math(Math::Multiply) => match &right { @@ -262,9 +262,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Math(Math::Divide) => match &right { @@ -285,9 +285,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::Equal) => match &right { @@ -305,9 +305,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::NotEqual) => match &right { @@ -326,9 +326,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::LessThan) => match &right { @@ -342,9 +342,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::LessThanOrEqual) => match &right { @@ -358,9 +358,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::GreaterThan) => match &right { @@ -374,9 +374,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::GreaterThanOrEqual) => match &right { @@ -390,9 +390,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, // TODO: update this to do a regex match instead of a simple contains? @@ -401,9 +401,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::StartsWith) => match &right { @@ -414,9 +414,9 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, Operator::Comparison(Comparison::EndsWith) => match &right { @@ -427,17 +427,17 @@ pub(super) fn compute_series_single_value( _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), }, _ => Err(ShellError::OperatorMismatch { op_span: operator.span, lhs_ty: left.get_type().to_string(), - lhs_span: left.span()?, + lhs_span: left.span(), rhs_ty: right.get_type().to_string(), - rhs_span: right.span()?, + rhs_span: right.span(), }), } } 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 e4cc880ce6..be536bdd81 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 @@ -882,6 +882,7 @@ fn series_to_values( span, Span::unknown(), )), + span, } } }; @@ -897,6 +898,7 @@ fn series_to_values( span, Span::unknown(), )), + span, } } }; @@ -950,6 +952,7 @@ fn series_to_values( span, Span::unknown(), )), + span, } } }; @@ -965,6 +968,7 @@ fn series_to_values( span, Span::unknown(), )), + span, } } }; diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/mod.rs index 286e6e7fe6..4e64015e02 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/mod.rs @@ -235,7 +235,7 @@ impl NuDataFrame { if Self::can_downcast(&value) { Ok(Self::get_df(value)?) } else if NuLazyFrame::can_downcast(&value) { - let span = value.span()?; + let span = value.span(); let lazy = NuLazyFrame::try_from_value(value)?; let df = lazy.collect(span)?; Ok(df) @@ -243,7 +243,7 @@ impl NuDataFrame { Err(ShellError::CantConvert { to_type: "lazy or eager dataframe".into(), from_type: value.get_type().to_string(), - span: value.span()?, + span: value.span(), help: None, }) } @@ -266,7 +266,7 @@ impl NuDataFrame { x => Err(ShellError::CantConvert { to_type: "dataframe".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/custom_value.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/custom_value.rs index 3eeb214788..85a0fb9652 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/custom_value.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_expression/custom_value.rs @@ -70,17 +70,17 @@ fn compute_with_value( match rhs.as_ref() { polars::prelude::Expr::Literal(..) => { - with_operator(operator, left, rhs, lhs_span, right.span()?, op) + with_operator(operator, left, rhs, lhs_span, right.span(), op) } _ => Err(ShellError::TypeMismatch { err_message: "Only literal expressions or number".into(), - span: right.span()?, + span: right.span(), }), } } _ => { let rhs = NuExpression::try_from_value(right.clone())?; - with_operator(operator, left, &rhs, lhs_span, right.span()?, op) + with_operator(operator, left, &rhs, lhs_span, right.span(), op) } } } 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 da01ca72d7..2b51186b82 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 @@ -79,7 +79,7 @@ impl NuExpression { x => Err(ShellError::CantConvert { to_type: "lazy expression".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } @@ -157,7 +157,7 @@ impl ExtractedExpr { x => Err(ShellError::CantConvert { to_type: "expression".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazyframe/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazyframe/mod.rs index ff637bb605..60aac358f7 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazyframe/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazyframe/mod.rs @@ -135,7 +135,7 @@ impl NuLazyFrame { Err(ShellError::CantConvert { to_type: "lazy or eager dataframe".into(), from_type: value.get_type().to_string(), - span: value.span()?, + span: value.span(), help: None, }) } @@ -164,7 +164,7 @@ impl NuLazyFrame { x => Err(ShellError::CantConvert { to_type: "lazy frame".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazygroupby/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazygroupby/mod.rs index 9d2efd15a6..103319f5ec 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazygroupby/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_lazygroupby/mod.rs @@ -104,7 +104,7 @@ impl NuLazyGroupBy { x => Err(ShellError::CantConvert { to_type: "lazy groupby".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/nu_when/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/values/nu_when/mod.rs index a1fe747e5e..087309b6b6 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/nu_when/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/nu_when/mod.rs @@ -71,7 +71,7 @@ impl NuWhen { x => Err(ShellError::CantConvert { to_type: "when expression".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu-cmd-dataframe/src/dataframe/values/utils.rs b/crates/nu-cmd-dataframe/src/dataframe/values/utils.rs index 912adb237c..5460ee6592 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/values/utils.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/values/utils.rs @@ -21,7 +21,7 @@ pub(crate) fn convert_columns( Vec::new(), ) }) - .and_then(|v| v.span())?; + .map(|v| v.span())?; let res = columns .into_iter() @@ -61,7 +61,7 @@ pub(crate) fn convert_columns_string( Vec::new(), ) }) - .and_then(|v| v.span())?; + .map(|v| v.span())?; let res = columns .into_iter() diff --git a/crates/nu-cmd-extra/src/extra/bits/and.rs b/crates/nu-cmd-extra/src/extra/bits/and.rs index 5a3e245bdf..fa67887631 100644 --- a/crates/nu-cmd-extra/src/extra/bits/and.rs +++ b/crates/nu-cmd-extra/src/extra/bits/and.rs @@ -90,8 +90,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value { exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/bits/into.rs b/crates/nu-cmd-extra/src/extra/bits/into.rs index 9db8312613..86176fb170 100644 --- a/crates/nu-cmd-extra/src/extra/bits/into.rs +++ b/crates/nu-cmd-extra/src/extra/bits/into.rs @@ -256,8 +256,9 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value { exp_input_type: "integer, filesize, string, date, duration, binary or bool".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-cmd-extra/src/extra/bits/not.rs b/crates/nu-cmd-extra/src/extra/bits/not.rs index 4d21a26986..e932208c3d 100644 --- a/crates/nu-cmd-extra/src/extra/bits/not.rs +++ b/crates/nu-cmd-extra/src/extra/bits/not.rs @@ -160,8 +160,9 @@ fn operate(value: Value, head: Span, signed: bool, number_size: NumberBytes) -> exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, }, } diff --git a/crates/nu-cmd-extra/src/extra/bits/or.rs b/crates/nu-cmd-extra/src/extra/bits/or.rs index 037d9cd78b..cbf0585f14 100644 --- a/crates/nu-cmd-extra/src/extra/bits/or.rs +++ b/crates/nu-cmd-extra/src/extra/bits/or.rs @@ -90,8 +90,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value { exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } 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 6e9fe7480b..89288a39b8 100644 --- a/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs +++ b/crates/nu-cmd-extra/src/extra/bits/rotate_left.rs @@ -117,6 +117,7 @@ where None, Vec::new(), )), + span, }, } } @@ -146,8 +147,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } 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 f7edded5ff..70e3a3adea 100644 --- a/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs +++ b/crates/nu-cmd-extra/src/extra/bits/rotate_right.rs @@ -121,6 +121,7 @@ where None, Vec::new(), )), + span, }, } } @@ -150,8 +151,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } 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 983513f5d6..beff36cd30 100644 --- a/crates/nu-cmd-extra/src/extra/bits/shift_left.rs +++ b/crates/nu-cmd-extra/src/extra/bits/shift_left.rs @@ -129,6 +129,7 @@ where None, Vec::new(), )), + span, }, } } @@ -140,6 +141,7 @@ where None, Vec::new(), )), + span, }, } } @@ -169,8 +171,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } 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 dcb0a563e8..e8af586e70 100644 --- a/crates/nu-cmd-extra/src/extra/bits/shift_right.rs +++ b/crates/nu-cmd-extra/src/extra/bits/shift_right.rs @@ -119,6 +119,7 @@ where None, Vec::new(), )), + span, }, } } @@ -130,6 +131,7 @@ where None, Vec::new(), )), + span, }, } } @@ -159,8 +161,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/bits/xor.rs b/crates/nu-cmd-extra/src/extra/bits/xor.rs index 7819af4336..73805c447c 100644 --- a/crates/nu-cmd-extra/src/extra/bits/xor.rs +++ b/crates/nu-cmd-extra/src/extra/bits/xor.rs @@ -89,8 +89,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value { exp_input_type: "integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/conversions/fmt.rs b/crates/nu-cmd-extra/src/extra/conversions/fmt.rs index ae8531ffca..d90aadb9d8 100644 --- a/crates/nu-cmd-extra/src/extra/conversions/fmt.rs +++ b/crates/nu-cmd-extra/src/extra/conversions/fmt.rs @@ -91,8 +91,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { exp_input_type: "float , integer or filesize".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-cmd-extra/src/extra/filters/roll/mod.rs b/crates/nu-cmd-extra/src/extra/filters/roll/mod.rs index 46fe8d8938..4ae803e7ce 100644 --- a/crates/nu-cmd-extra/src/extra/filters/roll/mod.rs +++ b/crates/nu-cmd-extra/src/extra/filters/roll/mod.rs @@ -38,7 +38,7 @@ fn vertical_rotate_value( } _ => Err(ShellError::TypeMismatch { err_message: "list".to_string(), - span: value.span()?, + span: value.span(), }), } } @@ -85,7 +85,7 @@ fn horizontal_rotate_value( } _ => Err(ShellError::TypeMismatch { err_message: "record".to_string(), - span: value.span()?, + span: value.span(), }), } } diff --git a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs index 305260760a..458261b219 100644 --- a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs +++ b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs @@ -251,7 +251,10 @@ fn process_cell( redirect_stderr, ) { Ok(pd) => pd.into_value(span), - Err(e) => Value::Error { error: Box::new(e) }, + Err(e) => Value::Error { + error: Box::new(e), + span, + }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arccos.rs b/crates/nu-cmd-extra/src/extra/math/arccos.rs index 3798cea754..17e9cc35b1 100644 --- a/crates/nu-cmd-extra/src/extra/math/arccos.rs +++ b/crates/nu-cmd-extra/src/extra/math/arccos.rs @@ -89,6 +89,7 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { head, span, )), + span, } } } @@ -98,8 +99,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arccosh.rs b/crates/nu-cmd-extra/src/extra/math/arccosh.rs index 70acfcb6ae..19cb08cf9b 100644 --- a/crates/nu-cmd-extra/src/extra/math/arccosh.rs +++ b/crates/nu-cmd-extra/src/extra/math/arccosh.rs @@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value { head, span, )), + span, } } } @@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arcsin.rs b/crates/nu-cmd-extra/src/extra/math/arcsin.rs index 774a4e76b6..8253e509c2 100644 --- a/crates/nu-cmd-extra/src/extra/math/arcsin.rs +++ b/crates/nu-cmd-extra/src/extra/math/arcsin.rs @@ -90,6 +90,7 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { head, span, )), + span, } } } @@ -99,8 +100,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arcsinh.rs b/crates/nu-cmd-extra/src/extra/math/arcsinh.rs index 72665a4cf7..7e627c0c23 100644 --- a/crates/nu-cmd-extra/src/extra/math/arcsinh.rs +++ b/crates/nu-cmd-extra/src/extra/math/arcsinh.rs @@ -77,8 +77,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arctan.rs b/crates/nu-cmd-extra/src/extra/math/arctan.rs index 40d7f8927a..955764885e 100644 --- a/crates/nu-cmd-extra/src/extra/math/arctan.rs +++ b/crates/nu-cmd-extra/src/extra/math/arctan.rs @@ -88,8 +88,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/arctanh.rs b/crates/nu-cmd-extra/src/extra/math/arctanh.rs index 9082cd0518..dc0984b19c 100644 --- a/crates/nu-cmd-extra/src/extra/math/arctanh.rs +++ b/crates/nu-cmd-extra/src/extra/math/arctanh.rs @@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value { head, span, )), + span: head, } } } @@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/cos.rs b/crates/nu-cmd-extra/src/extra/math/cos.rs index a7d899e474..a211a834db 100644 --- a/crates/nu-cmd-extra/src/extra/math/cos.rs +++ b/crates/nu-cmd-extra/src/extra/math/cos.rs @@ -97,8 +97,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/cosh.rs b/crates/nu-cmd-extra/src/extra/math/cosh.rs index dcfd854e41..1d7f2fd2d9 100644 --- a/crates/nu-cmd-extra/src/extra/math/cosh.rs +++ b/crates/nu-cmd-extra/src/extra/math/cosh.rs @@ -79,8 +79,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/exp.rs b/crates/nu-cmd-extra/src/extra/math/exp.rs index 5b7aa0cb3a..faa586226f 100644 --- a/crates/nu-cmd-extra/src/extra/math/exp.rs +++ b/crates/nu-cmd-extra/src/extra/math/exp.rs @@ -85,8 +85,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.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 b6ce3ba30c..3880cf09c6 100644 --- a/crates/nu-cmd-extra/src/extra/math/ln.rs +++ b/crates/nu-cmd-extra/src/extra/math/ln.rs @@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value { head, span, )), + span, } } } @@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/sin.rs b/crates/nu-cmd-extra/src/extra/math/sin.rs index 95b7994932..d4f1a4d78c 100644 --- a/crates/nu-cmd-extra/src/extra/math/sin.rs +++ b/crates/nu-cmd-extra/src/extra/math/sin.rs @@ -97,8 +97,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/sinh.rs b/crates/nu-cmd-extra/src/extra/math/sinh.rs index 490ac39104..acfaebc255 100644 --- a/crates/nu-cmd-extra/src/extra/math/sinh.rs +++ b/crates/nu-cmd-extra/src/extra/math/sinh.rs @@ -79,8 +79,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/tan.rs b/crates/nu-cmd-extra/src/extra/math/tan.rs index 7470ef3a73..0d8b87f894 100644 --- a/crates/nu-cmd-extra/src/extra/math/tan.rs +++ b/crates/nu-cmd-extra/src/extra/math/tan.rs @@ -95,8 +95,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/math/tanh.rs b/crates/nu-cmd-extra/src/extra/math/tanh.rs index 0dbf6413fd..15331762a9 100644 --- a/crates/nu-cmd-extra/src/extra/math/tanh.rs +++ b/crates/nu-cmd-extra/src/extra/math/tanh.rs @@ -78,8 +78,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-extra/src/extra/platform/ansi/gradient.rs b/crates/nu-cmd-extra/src/extra/platform/ansi/gradient.rs index 40de006e2e..3237222af9 100644 --- a/crates/nu-cmd-extra/src/extra/platform/ansi/gradient.rs +++ b/crates/nu-cmd-extra/src/extra/platform/ansi/gradient.rs @@ -140,6 +140,7 @@ fn operate( if let Err(error) = r { return Value::Error { error: Box::new(error), + span: head, }; } } @@ -170,6 +171,7 @@ fn action( "please supply foreground and/or background color parameters".into(), span: command_span, }), + span: command_span, } } (None, None, None, Some(bg_end)) => { @@ -295,8 +297,9 @@ fn action( Value::Error { error: Box::new(ShellError::TypeMismatch { err_message: got, - span: other.span().unwrap_or(command_span), + span: other.span(), }), + span: other.span(), } } } diff --git a/crates/nu-cmd-extra/src/extra/platform/ansi/link.rs b/crates/nu-cmd-extra/src/extra/platform/ansi/link.rs index 1d143dae1c..1367405245 100644 --- a/crates/nu-cmd-extra/src/extra/platform/ansi/link.rs +++ b/crates/nu-cmd-extra/src/extra/platform/ansi/link.rs @@ -98,7 +98,7 @@ fn operate( if column_paths.is_empty() { input.map( - move |v| process_value(&v, &text, command_span), + move |v| process_value(&v, &text), engine_state.ctrlc.clone(), ) } else { @@ -116,20 +116,18 @@ fn process_each_path( command_span: Span, ) -> Value { for path in column_paths { - let ret = value.update_cell_path( - &path.members, - Box::new(|v| process_value(v, text, command_span)), - ); + let ret = value.update_cell_path(&path.members, Box::new(|v| process_value(v, text))); if let Err(error) = ret { return Value::Error { error: Box::new(error), + span: command_span, }; } } value } -fn process_value(value: &Value, text: &Option, command_span: Span) -> Value { +fn process_value(value: &Value, text: &Option) -> Value { match value { Value::String { val, span } => { let text = text.as_deref().unwrap_or(val.as_str()); @@ -142,8 +140,9 @@ fn process_value(value: &Value, text: &Option, command_span: Span) -> Va Value::Error { error: Box::new(ShellError::TypeMismatch { err_message: got, - span: other.span().unwrap_or(command_span), + span: other.span(), }), + span: other.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 3485dd95a9..826e40c07d 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 @@ -113,8 +113,9 @@ fn action( "value originates from here".into(), command_span, // This line requires the Value::Error {} match above. - input.expect_span(), + input.span(), )), + span: command_span, }, }, Value::String { val, .. } => { @@ -125,8 +126,9 @@ fn action( "value originates from here".into(), command_span, // This line requires the Value::Error {} match above. - input.expect_span(), + input.span(), )), + span: command_span, }, ActionType::Decode => match hex_decode(val.as_ref()) { @@ -139,6 +141,7 @@ fn action( None, Vec::new(), )), + span: command_span, }, Err(HexDecodingError::InvalidDigit(index, digit)) => Value::Error { error: Box::new(ShellError::GenericError( @@ -148,6 +151,7 @@ fn action( None, Vec::new(), )), + span: command_span, }, }, } @@ -155,8 +159,9 @@ fn action( other => Value::Error { error: Box::new(ShellError::TypeMismatch { err_message: format!("string or binary, not {}", other.get_type()), - span: other.span().unwrap_or(command_span), + span: other.span(), }), + span: other.span(), }, } } diff --git a/crates/nu-cmd-extra/src/extra/strings/format/command.rs b/crates/nu-cmd-extra/src/extra/strings/format/command.rs index d5a50e5a62..13497a2a68 100644 --- a/crates/nu-cmd-extra/src/extra/strings/format/command.rs +++ b/crates/nu-cmd-extra/src/extra/strings/format/command.rs @@ -55,7 +55,7 @@ impl Command for Format { Err(e) => Err(e), Ok(pattern) => { let string_pattern = pattern.as_string()?; - let string_span = pattern.span()?; + let string_span = pattern.span(); // the string span is start as `"`, we don't need the character // to generate proper span for sub expression. let ops = extract_formatting_operations( @@ -233,13 +233,13 @@ fn format( } } } - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), _ => { return Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record".to_string(), wrong_type: val.get_type().to_string(), dst_span: head_span, - src_span: val.expect_span(), + src_span: val.span(), }) } } @@ -252,12 +252,12 @@ fn format( } // Unwrapping this ShellError is a bit unfortunate. // Ideally, its Span would be preserved. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), _ => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record".to_string(), wrong_type: data_as_value.get_type().to_string(), dst_span: head_span, - src_span: data_as_value.expect_span(), + src_span: data_as_value.span(), }), } } diff --git a/crates/nu-cmd-extra/src/extra/strings/str_/case/mod.rs b/crates/nu-cmd-extra/src/extra/strings/str_/case/mod.rs index dac550c4d7..4923795ac3 100644 --- a/crates/nu-cmd-extra/src/extra/strings/str_/case/mod.rs +++ b/crates/nu-cmd-extra/src/extra/strings/str_/case/mod.rs @@ -67,8 +67,9 @@ where exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-cmd-lang/src/core_commands/do_.rs b/crates/nu-cmd-lang/src/core_commands/do_.rs index 65ed6a4f70..48c0751f80 100644 --- a/crates/nu-cmd-lang/src/core_commands/do_.rs +++ b/crates/nu-cmd-lang/src/core_commands/do_.rs @@ -91,7 +91,7 @@ impl Command for Do { } let span = if let Some(rest_item) = rest_items.first() { - rest_item.span()? + rest_item.span() } else { call.head }; diff --git a/crates/nu-cmd-lang/src/core_commands/error_make.rs b/crates/nu-cmd-lang/src/core_commands/error_make.rs index 0ad64a42fe..3682ccb572 100644 --- a/crates/nu-cmd-lang/src/core_commands/error_make.rs +++ b/crates/nu-cmd-lang/src/core_commands/error_make.rs @@ -73,6 +73,7 @@ impl Command for ErrorMake { None, Vec::new(), )), + span: Span::unknown(), }), }, Example { @@ -93,6 +94,7 @@ impl Command for ErrorMake { None, Vec::new(), )), + span: Span::unknown(), }), }, Example { @@ -126,10 +128,7 @@ fn make_error(value: &Value, throw_span: Option) -> Option { let label_end = label.get_data_by_key("end"); let label_text = label.get_data_by_key("text"); - let label_span = match label.span() { - Ok(lspan) => Some(lspan), - Err(_) => None, - }; + let label_span = Some(label.span()); match (label_start, label_end, label_text) { ( diff --git a/crates/nu-cmd-lang/src/core_commands/if_.rs b/crates/nu-cmd-lang/src/core_commands/if_.rs index 129db3b37f..43c97bbe7b 100644 --- a/crates/nu-cmd-lang/src/core_commands/if_.rs +++ b/crates/nu-cmd-lang/src/core_commands/if_.rs @@ -105,7 +105,7 @@ impl Command for If { x => Err(ShellError::CantConvert { to_type: "bool".into(), from_type: x.get_type().to_string(), - span: result.span()?, + span: result.span(), help: None, }), } diff --git a/crates/nu-cmd-lang/src/core_commands/try_.rs b/crates/nu-cmd-lang/src/core_commands/try_.rs index 6d7da363d4..eda1172003 100644 --- a/crates/nu-cmd-lang/src/core_commands/try_.rs +++ b/crates/nu-cmd-lang/src/core_commands/try_.rs @@ -56,7 +56,7 @@ impl Command for Try { let err_record = err_to_record(error, call.head); handle_catch(err_record, catch_block, engine_state, stack) } - Ok(PipelineData::Value(Value::Error { error }, ..)) => { + Ok(PipelineData::Value(Value::Error { error, .. }, ..)) => { let error = intercept_block_control(*error)?; let err_record = err_to_record(error, call.head); handle_catch(err_record, catch_block, engine_state, stack) @@ -141,7 +141,7 @@ fn err_to_record(error: ShellError, head: Span) -> Value { record! { "msg" => Value::string(error.to_string(), head), "debug" => Value::string(format!("{error:?}"), head), - "raw" => Value::error(error), + "raw" => Value::error(error, head), }, head, ) diff --git a/crates/nu-cmd-lang/src/core_commands/while_.rs b/crates/nu-cmd-lang/src/core_commands/while_.rs index e375f0d0a6..4a6f842ff2 100644 --- a/crates/nu-cmd-lang/src/core_commands/while_.rs +++ b/crates/nu-cmd-lang/src/core_commands/while_.rs @@ -86,7 +86,7 @@ impl Command for While { return Err(ShellError::CantConvert { to_type: "bool".into(), from_type: x.get_type().to_string(), - span: result.span()?, + span: result.span(), help: None, }) } diff --git a/crates/nu-command/src/bytes/add.rs b/crates/nu-command/src/bytes/add.rs index a3aba96914..2bbb07f4a9 100644 --- a/crates/nu-command/src/bytes/add.rs +++ b/crates/nu-command/src/bytes/add.rs @@ -137,8 +137,9 @@ fn add(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/at.rs b/crates/nu-command/src/bytes/at.rs index f1b6b01f98..db024f8141 100644 --- a/crates/nu-command/src/bytes/at.rs +++ b/crates/nu-command/src/bytes/at.rs @@ -178,6 +178,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { err_message: "End must be greater than or equal to Start".to_string(), span: head, }), + span: head, }, Ordering::Less => Value::Binary { val: { @@ -210,8 +211,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { format!("input type: {:?}", other.get_type()), head, // This line requires the Value::Error match above. - other.expect_span(), + other.span(), )), + span: head, }, } } diff --git a/crates/nu-command/src/bytes/build_.rs b/crates/nu-command/src/bytes/build_.rs index 0b80a4895f..4726ee1551 100644 --- a/crates/nu-command/src/bytes/build_.rs +++ b/crates/nu-command/src/bytes/build_.rs @@ -53,11 +53,11 @@ impl Command for BytesBuild { match val { Value::Binary { mut val, .. } => output.append(&mut val), // Explicitly propagate errors instead of dropping them. - Value::Error { error } => return Err(*error), + Value::Error { error, .. } => return Err(*error), other => { return Err(ShellError::TypeMismatch { err_message: "only binary data arguments are supported".to_string(), - span: other.expect_span(), + span: other.span(), }) } } diff --git a/crates/nu-command/src/bytes/collect.rs b/crates/nu-command/src/bytes/collect.rs index d68c915b14..96bfa83285 100644 --- a/crates/nu-command/src/bytes/collect.rs +++ b/crates/nu-command/src/bytes/collect.rs @@ -55,13 +55,13 @@ impl Command for BytesCollect { } } // Explicitly propagate errors instead of dropping them. - Value::Error { error } => return Err(*error), + Value::Error { error, .. } => return Err(*error), other => { return Err(ShellError::OnlySupportsThisInputType { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: call.head, - src_span: other.expect_span(), + src_span: other.span(), }); } } diff --git a/crates/nu-command/src/bytes/ends_with.rs b/crates/nu-command/src/bytes/ends_with.rs index 0a12cbbae4..1c18b2558c 100644 --- a/crates/nu-command/src/bytes/ends_with.rs +++ b/crates/nu-command/src/bytes/ends_with.rs @@ -101,8 +101,9 @@ fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/index_of.rs b/crates/nu-command/src/bytes/index_of.rs index bcb02861b5..6a5ac0fdc4 100644 --- a/crates/nu-command/src/bytes/index_of.rs +++ b/crates/nu-command/src/bytes/index_of.rs @@ -143,8 +143,9 @@ fn index_of(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/length.rs b/crates/nu-command/src/bytes/length.rs index bfca8fd6df..115354788f 100644 --- a/crates/nu-command/src/bytes/length.rs +++ b/crates/nu-command/src/bytes/length.rs @@ -86,8 +86,9 @@ fn length(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/remove.rs b/crates/nu-command/src/bytes/remove.rs index bc43760bee..a0dd3a04e2 100644 --- a/crates/nu-command/src/bytes/remove.rs +++ b/crates/nu-command/src/bytes/remove.rs @@ -149,8 +149,9 @@ fn remove(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/replace.rs b/crates/nu-command/src/bytes/replace.rs index a78fcae73f..5fdc61314d 100644 --- a/crates/nu-command/src/bytes/replace.rs +++ b/crates/nu-command/src/bytes/replace.rs @@ -141,8 +141,9 @@ fn replace(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/reverse.rs b/crates/nu-command/src/bytes/reverse.rs index 437d0fb466..97488f9378 100644 --- a/crates/nu-command/src/bytes/reverse.rs +++ b/crates/nu-command/src/bytes/reverse.rs @@ -93,8 +93,9 @@ fn reverse(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/bytes/starts_with.rs b/crates/nu-command/src/bytes/starts_with.rs index dc1d6acc10..e1d7c8a524 100644 --- a/crates/nu-command/src/bytes/starts_with.rs +++ b/crates/nu-command/src/bytes/starts_with.rs @@ -89,8 +89,9 @@ impl Command for BytesStartsWith { exp_input_type: "string and binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, } .into_pipeline_data()); } @@ -158,8 +159,9 @@ fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/charting/hashable_value.rs b/crates/nu-command/src/charting/hashable_value.rs index 61ec9d82ce..49074e1225 100644 --- a/crates/nu-command/src/charting/hashable_value.rs +++ b/crates/nu-command/src/charting/hashable_value.rs @@ -79,12 +79,12 @@ impl HashableValue { Value::Binary { val, span } => Ok(HashableValue::Binary { val, span }), // Explicitly propagate errors instead of dropping them. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), _ => Err(ShellError::UnsupportedInput( "input value is not hashable".into(), format!("input type: {:?}", value.get_type()), span, - value.expect_span(), + value.span(), )), } } @@ -237,6 +237,7 @@ mod test { Value::Nothing { span }, Value::Error { error: Box::new(ShellError::DidYouMean("what?".to_string(), span)), + span, }, Value::CellPath { val: CellPath { diff --git a/crates/nu-command/src/charting/histogram.rs b/crates/nu-command/src/charting/histogram.rs index 35c1864ae1..5bbb4413e5 100755 --- a/crates/nu-command/src/charting/histogram.rs +++ b/crates/nu-command/src/charting/histogram.rs @@ -142,7 +142,7 @@ impl Command for Histogram { calc_method, span, // Note that as_list() filters out Value::Error here. - data_as_value.expect_span(), + data_as_value.span(), ) } } @@ -164,10 +164,10 @@ fn run_histogram( for v in values { match v { // Propagate existing errors. - Value::Error { error } => return Err(*error), + Value::Error { error, .. } => return Err(*error), _ => { let t = v.get_type(); - let span = v.expect_span(); + 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(), @@ -202,7 +202,7 @@ fn run_histogram( } } // Propagate existing errors. - Value::Error { error } => return Err(*error), + Value::Error { error, .. } => return Err(*error), _ => continue, } } diff --git a/crates/nu-command/src/conversions/fill.rs b/crates/nu-command/src/conversions/fill.rs index 94205ebb69..01697e6eac 100644 --- a/crates/nu-command/src/conversions/fill.rs +++ b/crates/nu-command/src/conversions/fill.rs @@ -203,8 +203,9 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { exp_input_type: "int, filesize, float, string".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/conversions/into/binary.rs b/crates/nu-command/src/conversions/into/binary.rs index b8baa0df89..f2b6e632dd 100644 --- a/crates/nu-command/src/conversions/into/binary.rs +++ b/crates/nu-command/src/conversions/into/binary.rs @@ -204,8 +204,9 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value { .into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, }; diff --git a/crates/nu-command/src/conversions/into/bool.rs b/crates/nu-command/src/conversions/into/bool.rs index d86780ea4a..c5b81e4ed5 100644 --- a/crates/nu-command/src/conversions/into/bool.rs +++ b/crates/nu-command/src/conversions/into/bool.rs @@ -161,6 +161,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { Ok(val) => Value::Bool { val, span }, Err(error) => Value::Error { error: Box::new(error), + span, }, }, // Propagate errors by explicitly matching them before the final case. @@ -170,8 +171,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { exp_input_type: "bool, integer, float or string".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } diff --git a/crates/nu-command/src/conversions/into/datetime.rs b/crates/nu-command/src/conversions/into/datetime.rs index 8fce51f56a..b03bfe4a2e 100644 --- a/crates/nu-command/src/conversions/into/datetime.rs +++ b/crates/nu-command/src/conversions/into/datetime.rs @@ -264,8 +264,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { exp_input_type: "string and integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }; } }; @@ -308,6 +309,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { input.debug_value(), *span, )), + span: *span, }, }, Zone::West(i) => match FixedOffset::west_opt((*i as i32) * HOUR) { @@ -323,6 +325,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { input.debug_value(), *span, )), + span: *span, }, }, Zone::Error => Value::Error { @@ -331,6 +334,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { err_message: "Invalid timezone or offset".to_string(), span: *span, }), + span: *span, }, }, }; @@ -346,6 +350,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { Err(reason) => { Value::Error { error: Box::new(ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) }), + span: head, } } }, @@ -369,8 +374,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/conversions/into/decimal.rs b/crates/nu-command/src/conversions/into/decimal.rs index a9b0d982d6..906669d6ca 100644 --- a/crates/nu-command/src/conversions/into/decimal.rs +++ b/crates/nu-command/src/conversions/into/decimal.rs @@ -107,6 +107,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value { span: *span, help: None, }), + span: *span, }, } } @@ -125,8 +126,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value { exp_input_type: "string, integer or bool".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/conversions/into/duration.rs b/crates/nu-command/src/conversions/into/duration.rs index 24fc069004..667e9d02e9 100644 --- a/crates/nu-command/src/conversions/into/duration.rs +++ b/crates/nu-command/src/conversions/into/duration.rs @@ -156,6 +156,7 @@ fn into_duration( if let Err(error) = r { return Value::Error { error: Box::new(error), + span, }; } } @@ -235,6 +236,7 @@ fn action(input: &Value, span: Span) -> Value { Ok(val) => Value::Duration { val, span }, Err(error) => Value::Error { error: Box::new(error), + span, }, }, // Propagate errors by explicitly matching them before the final case. @@ -244,8 +246,9 @@ fn action(input: &Value, span: Span) -> Value { exp_input_type: "string or duration".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } @@ -270,7 +273,7 @@ mod test { #[case("4\u{00B5}s", 4*1000)] // micro sign #[case("4\u{03BC}s", 4*1000)] // mu symbol #[case("5ms", 5 * 1000 * 1000)] - #[case("1sec", 1 * NS_PER_SEC)] + #[case("1sec", NS_PER_SEC)] #[case("7min", 7 * 60 * NS_PER_SEC)] #[case("42hr", 42 * 60 * 60 * NS_PER_SEC)] #[case("123day", 123 * 24 * 60 * 60 * NS_PER_SEC)] diff --git a/crates/nu-command/src/conversions/into/filesize.rs b/crates/nu-command/src/conversions/into/filesize.rs index 38303f0dea..6b852c9147 100644 --- a/crates/nu-command/src/conversions/into/filesize.rs +++ b/crates/nu-command/src/conversions/into/filesize.rs @@ -148,42 +148,40 @@ impl Command for SubCommand { } pub fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { - if let Ok(value_span) = input.span() { - match input { - Value::Filesize { .. } => input.clone(), - Value::Int { val, .. } => Value::Filesize { - val: *val, + let value_span = input.span(); + match input { + Value::Filesize { .. } => input.clone(), + Value::Int { val, .. } => Value::Filesize { + val: *val, + span: value_span, + }, + Value::Float { val, .. } => Value::Filesize { + val: *val as i64, + span: value_span, + }, + Value::String { val, .. } => match int_from_string(val, value_span) { + Ok(val) => Value::Filesize { + val, span: value_span, }, - Value::Float { val, .. } => Value::Filesize { - val: *val as i64, + Err(error) => Value::Error { + error: Box::new(error), span: value_span, }, - Value::String { val, .. } => match int_from_string(val, value_span) { - Ok(val) => Value::Filesize { - val, - span: value_span, - }, - Err(error) => Value::Error { - error: Box::new(error), - }, - }, - Value::Nothing { .. } => Value::Filesize { - val: 0, - span: value_span, - }, - other => Value::Error { - error: Box::new(ShellError::OnlySupportsThisInputType { - exp_input_type: "string and integer".into(), - wrong_type: other.get_type().to_string(), - dst_span: span, - src_span: value_span, - }), - }, - } - } else { - // Propagate existing errors - input.clone() + }, + Value::Nothing { .. } => Value::Filesize { + val: 0, + span: value_span, + }, + other => Value::Error { + error: Box::new(ShellError::OnlySupportsThisInputType { + exp_input_type: "string and integer".into(), + wrong_type: other.get_type().to_string(), + dst_span: span, + src_span: value_span, + }), + span, + }, } } fn int_from_string(a_string: &str, span: Span) -> Result { diff --git a/crates/nu-command/src/conversions/into/int.rs b/crates/nu-command/src/conversions/into/int.rs index 23bc33bb9f..7a6dfb8460 100644 --- a/crates/nu-command/src/conversions/into/int.rs +++ b/crates/nu-command/src/conversions/into/int.rs @@ -250,6 +250,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { span, help: None, }), + span, } } } @@ -263,6 +264,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { Ok(val) => Value::Int { val, span }, Err(error) => Value::Error { error: Box::new(error), + span, }, } } else { @@ -297,6 +299,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { val_span: *val_span, call_span: span, }), + span, } } else { Value::Int { @@ -335,8 +338,9 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { .into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }, } } @@ -353,7 +357,12 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value { { match int_from_string(val, head) { Ok(x) => return Value::int(x, head), - Err(e) => return Value::Error { error: Box::new(e) }, + Err(e) => { + return Value::Error { + error: Box::new(e), + span: head, + } + } } } else if val.starts_with("00") { // It's a padded string @@ -367,6 +376,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value { span: head, help: Some(e.to_string()), }), + span: head, } } } @@ -381,8 +391,9 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value { exp_input_type: "string and integer".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }; } }; @@ -395,6 +406,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value { span: head, help: None, }), + span: head, }, } } @@ -583,7 +595,7 @@ mod test { }, Span::test_data(), ); - if let Value::Error { error } = actual { + if let Value::Error { error, .. } = actual { if let ShellError::IncorrectValue { msg: e, .. } = *error { assert!( e.contains(err_expected), diff --git a/crates/nu-command/src/conversions/into/record.rs b/crates/nu-command/src/conversions/into/record.rs index 20af631996..45ded57d96 100644 --- a/crates/nu-command/src/conversions/into/record.rs +++ b/crates/nu-command/src/conversions/into/record.rs @@ -176,8 +176,9 @@ fn into_record( exp_input_type: "string".into(), wrong_type: other.get_type().to_string(), dst_span: call.head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: call.head, }, }; Ok(res.into_pipeline_data()) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 634fbf2d8f..f5d1a88019 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -249,7 +249,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { span, }, - Value::Error { error } => Value::String { + Value::Error { error, .. } => Value::String { val: into_code(error).unwrap_or_default(), span, }, @@ -265,6 +265,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { span, help: Some("try using the `to nuon` command".into()), }), + span, }, Value::Binary { .. } => Value::Error { error: Box::new(ShellError::CantConvert { @@ -273,6 +274,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { span, help: Some("try using the `decode` command".into()), }), + span, }, x => Value::Error { error: Box::new(ShellError::CantConvert { @@ -281,6 +283,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { span, help: None, }), + span, }, } } diff --git a/crates/nu-command/src/database/commands/into_sqlite.rs b/crates/nu-command/src/database/commands/into_sqlite.rs index 782f340383..bffad8ed75 100644 --- a/crates/nu-command/src/database/commands/into_sqlite.rs +++ b/crates/nu-command/src/database/commands/into_sqlite.rs @@ -213,12 +213,12 @@ fn action( Ok(Value::Nothing { span: *span }) } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error.clone()), + Value::Error { error, .. } => Err(*error.clone()), other => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), } } @@ -261,7 +261,7 @@ fn nu_value_to_string(value: Value, separator: &str) -> String { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), diff --git a/crates/nu-command/src/database/values/sqlite.rs b/crates/nu-command/src/database/values/sqlite.rs index 99c51aa536..18e5be6980 100644 --- a/crates/nu-command/src/database/values/sqlite.rs +++ b/crates/nu-command/src/database/values/sqlite.rs @@ -72,7 +72,7 @@ impl SQLiteDatabase { x => Err(ShellError::CantConvert { to_type: "database".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } @@ -459,6 +459,7 @@ pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value { Err(_) => { return Value::Error { error: Box::new(ShellError::NonUtf8(span)), + span, } } }; diff --git a/crates/nu-command/src/date/humanize.rs b/crates/nu-command/src/date/humanize.rs index 7fbeb53e16..33ce986a07 100644 --- a/crates/nu-command/src/date/humanize.rs +++ b/crates/nu-command/src/date/humanize.rs @@ -91,6 +91,7 @@ fn helper(value: Value, head: Span) -> Value { }, _ => Value::Error { error: Box::new(ShellError::DatetimeParseError(value.debug_value(), head)), + span: head, }, } } diff --git a/crates/nu-command/src/date/to_record.rs b/crates/nu-command/src/date/to_record.rs index 396318d87a..55d2c958b3 100644 --- a/crates/nu-command/src/date/to_record.rs +++ b/crates/nu-command/src/date/to_record.rs @@ -166,6 +166,7 @@ fn helper(val: Value, head: Span) -> Value { Value::Date { val, span: _ } => parse_date_into_table(val, head), _ => Value::Error { error: Box::new(DatetimeParseError(val.debug_value(), head)), + span: head, }, } } diff --git a/crates/nu-command/src/date/to_table.rs b/crates/nu-command/src/date/to_table.rs index 697de40eb6..aaf33f3fb5 100644 --- a/crates/nu-command/src/date/to_table.rs +++ b/crates/nu-command/src/date/to_table.rs @@ -168,6 +168,7 @@ fn helper(val: Value, head: Span) -> Value { Value::Date { val, span: _ } => parse_date_into_table(val, head), _ => Value::Error { error: Box::new(DatetimeParseError(val.debug_value(), head)), + span: head, }, } } diff --git a/crates/nu-command/src/date/to_timezone.rs b/crates/nu-command/src/date/to_timezone.rs index 2e745b93cb..9afcbad5af 100644 --- a/crates/nu-command/src/date/to_timezone.rs +++ b/crates/nu-command/src/date/to_timezone.rs @@ -128,6 +128,7 @@ fn helper(value: Value, head: Span, timezone: &Spanned) -> Value { } _ => Value::Error { error: Box::new(ShellError::DatetimeParseError(value.debug_value(), head)), + span: head, }, } } @@ -140,6 +141,7 @@ fn _to_timezone(dt: DateTime, timezone: &Spanned, span: Spa err_message: String::from("invalid time zone"), span: timezone.span, }), + span: timezone.span, }, } } diff --git a/crates/nu-command/src/date/utils.rs b/crates/nu-command/src/date/utils.rs index e90e34a3f1..3df3fcc596 100644 --- a/crates/nu-command/src/date/utils.rs +++ b/crates/nu-command/src/date/utils.rs @@ -16,11 +16,13 @@ pub(crate) fn parse_date_from_string( LocalResult::Ambiguous(d, _) => Ok(d), LocalResult::None => Err(Value::Error { error: Box::new(ShellError::DatetimeParseError(input.to_string(), span)), + span, }), } } Err(_) => Err(Value::Error { error: Box::new(ShellError::DatetimeParseError(input.to_string(), span)), + span, }), } } diff --git a/crates/nu-command/src/debug/explain.rs b/crates/nu-command/src/debug/explain.rs index 32241d7712..615c20f1ad 100644 --- a/crates/nu-command/src/debug/explain.rs +++ b/crates/nu-command/src/debug/explain.rs @@ -86,7 +86,7 @@ pub fn get_pipeline_elements( }; let index = format!("{pipeline_idx}_{i}"); let value_type = value.get_type(); - let value_span = value.span()?; + let value_span = value.span(); let value_span_start = value_span.start as i64; let value_span_end = value_span.end as i64; let command_name = command_name; @@ -150,7 +150,7 @@ fn get_arguments(engine_state: &EngineState, stack: &mut Stack, call: Call) -> V let arg_type = "expr"; let arg_value_name = debug_string_without_formatting(&evaluated_expression); let arg_value_type = &evaluated_expression.get_type().to_string(); - let evaled_span = evaluated_expression.expect_span(); + let evaled_span = evaluated_expression.span(); let arg_value_name_span_start = evaled_span.start as i64; let arg_value_name_span_end = evaled_span.end as i64; @@ -169,7 +169,7 @@ fn get_arguments(engine_state: &EngineState, stack: &mut Stack, call: Call) -> V let evaluated_expression = get_expression_as_value(engine_state, stack, inner_expr); let arg_value_name = debug_string_without_formatting(&evaluated_expression); let arg_value_type = &evaluated_expression.get_type().to_string(); - let evaled_span = evaluated_expression.expect_span(); + let evaled_span = evaluated_expression.span(); let arg_value_name_span_start = evaled_span.start as i64; let arg_value_name_span_end = evaled_span.end as i64; @@ -187,7 +187,7 @@ fn get_arguments(engine_state: &EngineState, stack: &mut Stack, call: Call) -> V let evaluated_expression = get_expression_as_value(engine_state, stack, inner_expr); let arg_value_name = debug_string_without_formatting(&evaluated_expression); let arg_value_type = &evaluated_expression.get_type().to_string(); - let evaled_span = evaluated_expression.expect_span(); + let evaled_span = evaluated_expression.span(); let arg_value_name_span_start = evaled_span.start as i64; let arg_value_name_span_end = evaled_span.end as i64; @@ -215,6 +215,7 @@ fn get_expression_as_value( Ok(v) => v, Err(error) => Value::Error { error: Box::new(error), + span: inner_expr.span, }, } } @@ -257,7 +258,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), diff --git a/crates/nu-command/src/debug/metadata.rs b/crates/nu-command/src/debug/metadata.rs index 2f0dc2d616..08f813e57c 100644 --- a/crates/nu-command/src/debug/metadata.rs +++ b/crates/nu-command/src/debug/metadata.rs @@ -116,18 +116,17 @@ fn build_metadata_record( ) -> Value { let mut record = Record::new(); - if let Ok(span) = arg.span() { - record.push( - "span", - Value::record( - record! { - "start" => Value::int(span.start as i64,span), - "end" => Value::int(span.end as i64, span), - }, - head, - ), - ); - } + let span = arg.span(); + record.push( + "span", + Value::record( + record! { + "start" => Value::int(span.start as i64,span), + "end" => Value::int(span.end as i64, span), + }, + head, + ), + ); if let Some(x) = metadata.as_deref() { match x { diff --git a/crates/nu-command/src/debug/view_source.rs b/crates/nu-command/src/debug/view_source.rs index d49683551a..3d1af9c50f 100644 --- a/crates/nu-command/src/debug/view_source.rs +++ b/crates/nu-command/src/debug/view_source.rs @@ -34,7 +34,7 @@ impl Command for ViewSource { _input: PipelineData, ) -> Result { let arg: Value = call.req(engine_state, stack, 0)?; - let arg_span = arg.span()?; + let arg_span = arg.span(); match arg { Value::Block { val: block_id, .. } | Value::Closure { val: block_id, .. } => { diff --git a/crates/nu-command/src/filesystem/cp.rs b/crates/nu-command/src/filesystem/cp.rs index 20c05474bd..3fc52a95eb 100644 --- a/crates/nu-command/src/filesystem/cp.rs +++ b/crates/nu-command/src/filesystem/cp.rs @@ -402,6 +402,7 @@ fn interactive_copy( None, Vec::new(), )), + span, } } else if !confirmed { let msg = format!("{:} not copied to {:}", src.display(), dst.display()); @@ -541,6 +542,7 @@ fn copy_symlink( None, vec![], )), + span, } } }; @@ -574,6 +576,7 @@ fn copy_symlink( None, vec![], )), + span, }, } } @@ -617,5 +620,6 @@ fn convert_io_error(error: std::io::Error, src: PathBuf, dst: PathBuf, span: Spa Value::Error { error: Box::new(shell_error), + span, } } diff --git a/crates/nu-command/src/filesystem/glob.rs b/crates/nu-command/src/filesystem/glob.rs index 7d7bc42b8a..b0360b7673 100644 --- a/crates/nu-command/src/filesystem/glob.rs +++ b/crates/nu-command/src/filesystem/glob.rs @@ -148,7 +148,7 @@ impl Command for Glob { }) = call.get_flag(engine_state, stack, "not")? { - let p = convert_patterns(pats.as_slice(), span)?; + let p = convert_patterns(pats.as_slice())?; (p, pat_span) } else { (vec![], span) @@ -226,14 +226,14 @@ impl Command for Glob { } } -fn convert_patterns(columns: &[Value], span: Span) -> Result, ShellError> { +fn convert_patterns(columns: &[Value]) -> Result, ShellError> { let res = columns .iter() .map(|value| match &value { Value::String { val: s, .. } => Ok(s.clone()), _ => Err(ShellError::IncompatibleParametersSingle { msg: "Incorrect column format, Only string as column name".to_string(), - span: value.span().unwrap_or(span), + span: value.span(), }), }) .collect::, _>>()?; diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index fd9a4c147e..2b47e60319 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -257,11 +257,13 @@ impl Command for Ls { Ok(value) => Some(value), Err(err) => Some(Value::Error { error: Box::new(err), + span: call_span, }), } } Err(err) => Some(Value::Error { error: Box::new(err), + span: call_span, }), } } diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index b8b63c987c..6e7e882323 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -202,6 +202,7 @@ impl Command for Mv { if let Err(error) = result { Some(Value::Error { error: Box::new(error), + span: spanned_source.span, }) } else if verbose { let val = match result { diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index 486f6acfd5..cd97d40d98 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -430,6 +430,7 @@ fn rm( let msg = format!("Could not delete {:}: {e:}", f.to_string_lossy()); Value::Error { error: Box::new(ShellError::RemoveNotPossible(msg, span)), + span, } } else if verbose { let msg = if interactive && !confirmed { @@ -452,6 +453,7 @@ fn rm( None, Vec::new(), )), + span, } } } else { @@ -464,6 +466,7 @@ fn rm( None, Vec::new(), )), + span, } } }) diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index 1f64c79755..4a4f77eb27 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -263,7 +263,7 @@ fn value_to_bytes(value: Value) -> Result, ShellError> { Ok(val.into_bytes()) } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Ok(other.as_string()?.into_bytes()), } } @@ -385,13 +385,13 @@ fn stream_to_file( Value::String { val, .. } => val.into_bytes(), Value::Binary { val, .. } => val, // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => return Err(*error), + Value::Error { error, .. } => return Err(*error), other => { return Err(ShellError::OnlySupportsThisInputType { exp_input_type: "string or binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }); } }, diff --git a/crates/nu-command/src/filters/columns.rs b/crates/nu-command/src/filters/columns.rs index fb2fbf83ba..3bcc359c37 100644 --- a/crates/nu-command/src/filters/columns.rs +++ b/crates/nu-command/src/filters/columns.rs @@ -138,12 +138,12 @@ fn getcol( .into_pipeline_data(ctrlc) .set_metadata(metadata)), // Propagate errors - PipelineData::Value(Value::Error { error }, ..) => Err(*error), + PipelineData::Value(Value::Error { error, .. }, ..) => Err(*error), PipelineData::Value(other, ..) => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record or table".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record or table".into(), diff --git a/crates/nu-command/src/filters/drop/nth.rs b/crates/nu-command/src/filters/drop/nth.rs index fe4cc94531..a0e6ccd329 100644 --- a/crates/nu-command/src/filters/drop/nth.rs +++ b/crates/nu-command/src/filters/drop/nth.rs @@ -196,7 +196,7 @@ fn extract_int_or_range( .or(range_opt) .ok_or_else(|| ShellError::TypeMismatch { err_message: "int or range".into(), - span: value.span().unwrap_or_else(|_| Span::new(0, 0)), + span: value.span(), }) } diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index 02043c05c4..2d029ff051 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -157,6 +157,7 @@ with 'transpose' first."# } let input_span = x.span(); + let x_is_error = x.is_error(); match eval_block_with_early_return( &engine_state, &mut stack, @@ -169,9 +170,10 @@ with 'transpose' first."# Err(ShellError::Continue(v)) => Some(Value::nothing(v)), Err(ShellError::Break(_)) => None, Err(error) => { - let error = chain_error_with_input(error, input_span); + let error = chain_error_with_input(error, x_is_error, input_span); Some(Value::Error { error: Box::new(error), + span: input_span, }) } } @@ -196,6 +198,7 @@ with 'transpose' first."# Err(err) => { return Some(Value::Error { error: Box::new(err), + span, }) } }; @@ -207,6 +210,8 @@ with 'transpose' first."# } let input_span = x.span(); + let x_is_error = x.is_error(); + match eval_block_with_early_return( &engine_state, &mut stack, @@ -219,8 +224,12 @@ with 'transpose' first."# Err(ShellError::Continue(v)) => Some(Value::nothing(v)), Err(ShellError::Break(_)) => None, Err(error) => { - let error = Box::new(chain_error_with_input(error, input_span)); - Some(Value::Error { error }) + let error = + Box::new(chain_error_with_input(error, x_is_error, input_span)); + Some(Value::Error { + error, + span: input_span, + }) } } }) diff --git a/crates/nu-command/src/filters/filter.rs b/crates/nu-command/src/filters/filter.rs index a60b30192c..7d207bab29 100644 --- a/crates/nu-command/src/filters/filter.rs +++ b/crates/nu-command/src/filters/filter.rs @@ -102,7 +102,8 @@ a variable. On the other hand, the "row condition" syntax is not supported."# } } Err(error) => Some(Value::Error { - error: Box::new(chain_error_with_input(error, x.span())), + error: Box::new(chain_error_with_input(error, x.is_error(), x.span())), + span: x.span(), }), } }) @@ -122,6 +123,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."# Err(err) => { return Some(Value::Error { error: Box::new(err), + span, }) } }; @@ -149,7 +151,8 @@ a variable. On the other hand, the "row condition" syntax is not supported."# } } Err(error) => Some(Value::Error { - error: Box::new(chain_error_with_input(error, x.span())), + error: Box::new(chain_error_with_input(error, x.is_error(), x.span())), + span: x.span(), }), } }) @@ -182,7 +185,8 @@ a variable. On the other hand, the "row condition" syntax is not supported."# } } Err(error) => Some(Value::Error { - error: Box::new(chain_error_with_input(error, x.span())), + error: Box::new(chain_error_with_input(error, x.is_error(), x.span())), + span: x.span(), }), } .into_pipeline_data(ctrlc)) diff --git a/crates/nu-command/src/filters/find.rs b/crates/nu-command/src/filters/find.rs index cba2281ec3..1e0b108103 100644 --- a/crates/nu-command/src/filters/find.rs +++ b/crates/nu-command/src/filters/find.rs @@ -366,13 +366,7 @@ fn find_with_rest_and_highlight( let terms = call.rest::(&engine_state, stack, 0)?; let lower_terms = terms .iter() - .map(|v| { - if let Ok(span) = v.span() { - Value::string(v.into_string("", &config).to_lowercase(), span) - } else { - v.clone() - } - }) + .map(|v| Value::string(v.into_string("", &config).to_lowercase(), span)) .collect::>(); let style_computer = StyleComputer::from_config(&engine_state, stack); @@ -482,14 +476,14 @@ fn find_with_rest_and_highlight( } } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => return Err(*error), + 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.expect_span(), + other.span(), )); } }, @@ -510,11 +504,7 @@ fn value_should_be_printed( columns_to_search: &Vec, invert: bool, ) -> bool { - let lower_value = if let Ok(span) = value.span() { - Value::string(value.into_string("", filter_config).to_lowercase(), span) - } else { - value.clone() - }; + let lower_value = Value::string(value.into_string("", filter_config).to_lowercase(), span); let mut match_found = lower_terms.iter().any(|term| match value { Value::Bool { .. } @@ -579,7 +569,7 @@ fn record_matches_term( if !cols_to_search.contains(col) { return false; } - let lower_val = if val.span().is_ok() { + let lower_val = if !val.is_error() { Value::string( val.into_string("", filter_config).to_lowercase(), Span::test_data(), diff --git a/crates/nu-command/src/filters/first.rs b/crates/nu-command/src/filters/first.rs index 8a9660dac7..5cf700e887 100644 --- a/crates/nu-command/src/filters/first.rs +++ b/crates/nu-command/src/filters/first.rs @@ -164,12 +164,12 @@ fn first_helper( } } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary or range".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), }, PipelineData::ListStream(mut ls, metadata) => { diff --git a/crates/nu-command/src/filters/flatten.rs b/crates/nu-command/src/filters/flatten.rs index 8b91d542ec..7f23fc442f 100644 --- a/crates/nu-command/src/filters/flatten.rs +++ b/crates/nu-command/src/filters/flatten.rs @@ -175,11 +175,8 @@ enum TableInside<'a> { }, } -fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span, all: bool) -> Vec { - let tag = match item.span() { - Ok(x) => x, - Err(e) => return vec![Value::Error { error: Box::new(e) }], - }; +fn flat_value(columns: &[CellPath], item: &Value, name_tag: Span, all: bool) -> Vec { + let tag = item.span(); if item.as_record().is_ok() { let mut out = IndexMap::::new(); @@ -194,17 +191,15 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span, all: bool) -> error: Box::new(ShellError::OnlySupportsThisInputType { exp_input_type: "record".into(), wrong_type: other.get_type().to_string(), - dst_span: _name_tag, - src_span: other.expect_span(), + dst_span: name_tag, + src_span: other.span(), }), + span: name_tag, }]; } }; - let s = match item.span() { - Ok(x) => x, - Err(e) => return vec![Value::Error { error: Box::new(e) }], - }; + let s = item.span(); for (column_index, (column, value)) in record.iter().enumerate() { let column_requested = columns.iter().find(|c| c.into_string() == *column); @@ -233,7 +228,7 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span, all: bool) -> "value originates from here".into(), s, *span - ))} + )), span: *span} ]; } // it's a table (a list of record, we can flatten inner record) @@ -269,7 +264,7 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span, all: bool) -> "value originates from here".into(), s, *span - ))} + )), span: *span} ]; } diff --git a/crates/nu-command/src/filters/headers.rs b/crates/nu-command/src/filters/headers.rs index 6de7eeab40..b824c3b5d1 100644 --- a/crates/nu-command/src/filters/headers.rs +++ b/crates/nu-command/src/filters/headers.rs @@ -121,7 +121,7 @@ fn replace_headers( } _ => Err(ShellError::TypeMismatch { err_message: "record".to_string(), - span: value.span()?, + span: value.span(), }), } } @@ -148,7 +148,7 @@ fn extract_headers( return Err(ShellError::TypeMismatch { err_message: "needs compatible type: Null, String, Bool, Float, Int" .to_string(), - span: v.span()?, + span: v.span(), }); } } @@ -185,7 +185,7 @@ fn extract_headers( })?, _ => Err(ShellError::TypeMismatch { err_message: "record".to_string(), - span: value.span()?, + span: value.span(), }), } } diff --git a/crates/nu-command/src/filters/insert.rs b/crates/nu-command/src/filters/insert.rs index a52b692ef5..42a6dcf0ef 100644 --- a/crates/nu-command/src/filters/insert.rs +++ b/crates/nu-command/src/filters/insert.rs @@ -173,12 +173,18 @@ fn insert( pd.into_value(span), span, ) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input } - Err(e) => Value::Error { error: Box::new(e) }, + Err(e) => Value::Error { + error: Box::new(e), + span, + }, } }, ctrlc, @@ -209,7 +215,10 @@ fn insert( if let Err(e) = input.insert_data_at_cell_path(&cell_path.members, replacement, span) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input diff --git a/crates/nu-command/src/filters/items.rs b/crates/nu-command/src/filters/items.rs index 3e0a9337d7..dc93a9f2af 100644 --- a/crates/nu-command/src/filters/items.rs +++ b/crates/nu-command/src/filters/items.rs @@ -1,4 +1,3 @@ -use super::utils::chain_error_with_input; use nu_engine::{eval_block_with_early_return, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Closure, Command, EngineState, Stack}; @@ -7,6 +6,8 @@ use nu_protocol::{ SyntaxShape, Type, Value, }; +use super::utils::chain_error_with_input; + #[derive(Clone)] pub struct Items; @@ -85,9 +86,10 @@ impl Command for Items { Ok(v) => Some(v.into_value(span)), Err(ShellError::Break(_)) => None, Err(error) => { - let error = chain_error_with_input(error, Ok(input_span)); + let error = chain_error_with_input(error, false, input_span); Some(Value::Error { error: Box::new(error), + span, }) } } @@ -105,12 +107,12 @@ impl Command for Items { dst_span: call.head, src_span: input_span, }), - PipelineData::Value(Value::Error { error }, ..) => Err(*error), + PipelineData::Value(Value::Error { error, .. }, ..) => Err(*error), PipelineData::Value(other, ..) => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record".into(), wrong_type: other.get_type().to_string(), dst_span: call.head, - src_span: other.expect_span(), + src_span: other.span(), }), PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record".into(), diff --git a/crates/nu-command/src/filters/last.rs b/crates/nu-command/src/filters/last.rs index c33febe9b8..d66f34df4b 100644 --- a/crates/nu-command/src/filters/last.rs +++ b/crates/nu-command/src/filters/last.rs @@ -170,12 +170,12 @@ impl Command for Last { } } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary or range".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), }, PipelineData::ExternalStream { span, .. } => { diff --git a/crates/nu-command/src/filters/length.rs b/crates/nu-command/src/filters/length.rs index ebcfb24752..c1fb54c60b 100644 --- a/crates/nu-command/src/filters/length.rs +++ b/crates/nu-command/src/filters/length.rs @@ -74,7 +74,7 @@ fn length_row(call: &Call, input: PipelineData) -> Result { match val { // Propagate existing errors - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), _ => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "string or raw data".into(), wrong_type: val.get_type().to_string(), dst_span: head, - src_span: val.expect_span(), + src_span: val.span(), }), } } @@ -131,6 +131,7 @@ impl Command for Lines { Ok(x) => x, Err(err) => Value::Error { error: Box::new(err), + span: head, }, }) .into_pipeline_data(ctrlc)), @@ -234,13 +235,13 @@ impl Iterator for RawStreamLinesAdapter { self.queue.append(&mut lines); } // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => return Some(Err(*error)), + Value::Error { error, .. } => return Some(Err(*error)), other => { return Some(Err(ShellError::OnlySupportsThisInputType { exp_input_type: "string".into(), wrong_type: other.get_type().to_string(), dst_span: self.span, - src_span: other.expect_span(), + src_span: other.span(), })); } } diff --git a/crates/nu-command/src/filters/merge.rs b/crates/nu-command/src/filters/merge.rs index 06a18985cd..b5e4df20b5 100644 --- a/crates/nu-command/src/filters/merge.rs +++ b/crates/nu-command/src/filters/merge.rs @@ -115,11 +115,13 @@ repeating this process with row 1, and so on."# Ok(to_merge) => Value::record(do_merge(inp, to_merge), call.head), Err(error) => Value::Error { error: Box::new(error), + span: call.head, }, }, (_, None) => inp, (Err(error), _) => Value::Error { error: Box::new(error), + span: call.head, }, }); @@ -137,10 +139,10 @@ repeating this process with row 1, and so on."# (PipelineData::Value(val, ..), ..) => { // Only point the "value originates here" arrow at the merge value // if it was generated from a block. Otherwise, point at the pipeline value. -Leon 2022-10-27 - let span = if val.span()? == Span::test_data() { + let span = if val.span() == Span::test_data() { Span::new(call.head.start, call.head.start) } else { - val.span()? + val.span() }; Err(ShellError::PipelineMismatch { diff --git a/crates/nu-command/src/filters/move_.rs b/crates/nu-command/src/filters/move_.rs index 30593f91cd..d8aed5cfc8 100644 --- a/crates/nu-command/src/filters/move_.rs +++ b/crates/nu-command/src/filters/move_.rs @@ -117,11 +117,11 @@ impl Command for Move { let before_or_after = match (after, before) { (Some(v), None) => Spanned { item: BeforeOrAfter::After(v.as_string()?), - span: v.span()?, + span: v.span(), }, (None, Some(v)) => Spanned { item: BeforeOrAfter::Before(v.as_string()?), - span: v.span()?, + span: v.span(), }, (Some(_), Some(_)) => { return Err(ShellError::GenericError( @@ -155,11 +155,13 @@ impl Command for Move { Ok(val) => val, Err(error) => Value::Error { error: Box::new(error), + span: call.head, }, } } Err(error) => Value::Error { error: Box::new(error), + span: call.head, }, }); @@ -233,7 +235,7 @@ fn move_record_columns( return Err(ShellError::GenericError( "Cannot move columns".to_string(), "column does not exist".to_string(), - Some(column.span()?), + Some(column.span()), None, Vec::new(), )); diff --git a/crates/nu-command/src/filters/par_each.rs b/crates/nu-command/src/filters/par_each.rs index b3bfc262da..4f745f3831 100644 --- a/crates/nu-command/src/filters/par_each.rs +++ b/crates/nu-command/src/filters/par_each.rs @@ -142,6 +142,8 @@ impl Command for ParEach { } let val_span = x.span(); + let x_is_error = x.is_error(); + match eval_block_with_early_return( engine_state, &mut stack, @@ -153,7 +155,10 @@ impl Command for ParEach { Ok(v) => v.into_value(span), Err(error) => Value::Error { - error: Box::new(chain_error_with_input(error, val_span)), + error: Box::new(chain_error_with_input( + error, x_is_error, val_span, + )), + span: val_span, }, } }) @@ -176,6 +181,8 @@ impl Command for ParEach { } let val_span = x.span(); + let x_is_error = x.is_error(); + match eval_block_with_early_return( engine_state, &mut stack, @@ -186,7 +193,10 @@ impl Command for ParEach { ) { Ok(v) => v.into_value(span), Err(error) => Value::Error { - error: Box::new(chain_error_with_input(error, val_span)), + error: Box::new(chain_error_with_input( + error, x_is_error, val_span, + )), + span: val_span, }, } }) @@ -209,6 +219,8 @@ impl Command for ParEach { } let val_span = x.span(); + let x_is_error = x.is_error(); + match eval_block_with_early_return( engine_state, &mut stack, @@ -219,7 +231,10 @@ impl Command for ParEach { ) { Ok(v) => v.into_value(span), Err(error) => Value::Error { - error: Box::new(chain_error_with_input(error, val_span)), + error: Box::new(chain_error_with_input( + error, x_is_error, val_span, + )), + span: val_span, }, } }) @@ -240,6 +255,7 @@ impl Command for ParEach { Err(err) => { return Value::Error { error: Box::new(err), + span, } } }; @@ -265,6 +281,7 @@ impl Command for ParEach { Ok(v) => v.into_value(span), Err(error) => Value::Error { error: Box::new(error), + span, }, } }) diff --git a/crates/nu-command/src/filters/rename.rs b/crates/nu-command/src/filters/rename.rs index 546ca55e9d..69c8836519 100644 --- a/crates/nu-command/src/filters/rename.rs +++ b/crates/nu-command/src/filters/rename.rs @@ -122,7 +122,7 @@ fn rename( return Err(ShellError::TypeMismatch { err_message: "The column list cannot be empty and must contain only two values: the column's name and its replacement value" .to_string(), span: column_span }); } else { - (Some(columns[0].span()?), column_span) + (Some(columns[0].span()), column_span) } } else { (None, call.head) @@ -180,9 +180,19 @@ fn rename( redirect_stderr, ); match eval_result { - Err(e) => return Value::Error { error: Box::new(e) }, + Err(e) => { + return Value::Error { + error: Box::new(e), + span, + } + } Ok(res) => match res.collect_string_strict(span) { - Err(e) => return Value::Error { error: Box::new(e) }, + Err(e) => { + return Value::Error { + error: Box::new(e), + span, + } + } Ok(new_c) => *c = new_c.0, }, } @@ -204,6 +214,7 @@ fn rename( // Arrow 2 points at the input value. span, )), + span, }; } for (idx, val) in record.cols.iter_mut().enumerate() { @@ -234,8 +245,9 @@ fn rename( exp_input_type: "record".into(), wrong_type: other.get_type().to_string(), dst_span: head_span, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head_span, }, }, engine_state.ctrlc.clone(), diff --git a/crates/nu-command/src/filters/select.rs b/crates/nu-command/src/filters/select.rs index edd40b2d0b..80d07ce794 100644 --- a/crates/nu-command/src/filters/select.rs +++ b/crates/nu-command/src/filters/select.rs @@ -64,14 +64,14 @@ produce a table, a list will produce a list, and a record will produce a record. let columns: Vec = call.rest(engine_state, stack, 0)?; let mut new_columns: Vec = vec![]; for col_val in columns { - let col_span = &col_val.span()?; + let col_span = &col_val.span(); match col_val { Value::CellPath { val, .. } => { new_columns.push(val); } Value::List { vals, .. } => { for value in vals { - let val_span = &value.span()?; + let val_span = &value.span(); match value { Value::String { val, .. } => { let cv = CellPath { @@ -97,7 +97,7 @@ produce a table, a list will produce a list, and a record will produce a record. return Err(ShellError::CantConvert { to_type: "cell path".into(), from_type: y.get_type().to_string(), - span: y.span()?, + span: y.span(), help: None, }); } @@ -128,7 +128,7 @@ produce a table, a list will produce a list, and a record will produce a record. return Err(ShellError::CantConvert { to_type: "cell path".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }); } diff --git a/crates/nu-command/src/filters/split_by.rs b/crates/nu-command/src/filters/split_by.rs index a62e26aa5f..be2324a9a7 100644 --- a/crates/nu-command/src/filters/split_by.rs +++ b/crates/nu-command/src/filters/split_by.rs @@ -163,7 +163,7 @@ pub fn split( None => Err(ShellError::CantFindColumn { col_name: column_name.item.to_string(), span: column_name.span, - src_span: row.span().unwrap_or(column_name.span), + src_span: row.span(), }), }, ); diff --git a/crates/nu-command/src/filters/take/take_.rs b/crates/nu-command/src/filters/take/take_.rs index 53f066cb77..10d13a4755 100644 --- a/crates/nu-command/src/filters/take/take_.rs +++ b/crates/nu-command/src/filters/take/take_.rs @@ -73,12 +73,12 @@ impl Command for Take { .into_pipeline_data(ctrlc) .set_metadata(metadata)), // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary or range".into(), wrong_type: other.get_type().to_string(), dst_span: call.head, - src_span: other.expect_span(), + src_span: other.span(), }), }, PipelineData::ListStream(ls, metadata) => Ok(ls diff --git a/crates/nu-command/src/filters/transpose.rs b/crates/nu-command/src/filters/transpose.rs index 822ae61879..d90331d6e6 100644 --- a/crates/nu-command/src/filters/transpose.rs +++ b/crates/nu-command/src/filters/transpose.rs @@ -271,7 +271,7 @@ pub fn transpose( } v => Value::List { vals: vec![v.clone(), x.clone()], - span: v.expect_span(), + span: v.span(), }, }; record.cols.remove(index); @@ -309,7 +309,7 @@ pub fn transpose( } v => Value::List { vals: vec![v.clone(), Value::nothing(name)], - span: v.expect_span(), + span: v.span(), }, }; record.cols.remove(index); diff --git a/crates/nu-command/src/filters/update.rs b/crates/nu-command/src/filters/update.rs index 0e7a2137a4..f542290226 100644 --- a/crates/nu-command/src/filters/update.rs +++ b/crates/nu-command/src/filters/update.rs @@ -140,7 +140,12 @@ fn update( let input_at_path = match input.clone().follow_cell_path(&cell_path.members, false) { - Err(e) => return Value::Error { error: Box::new(e) }, + Err(e) => { + return Value::Error { + error: Box::new(e), + span, + } + } Ok(v) => v, }; let output = eval_block( @@ -157,12 +162,18 @@ fn update( if let Err(e) = input.update_data_at_cell_path(&cell_path.members, pd.into_value(span)) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input } - Err(e) => Value::Error { error: Box::new(e) }, + Err(e) => Value::Error { + error: Box::new(e), + span, + }, } }, ctrlc, @@ -199,7 +210,10 @@ fn update( let replacement = replacement.clone(); if let Err(e) = input.update_data_at_cell_path(&cell_path.members, replacement) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input diff --git a/crates/nu-command/src/filters/upsert.rs b/crates/nu-command/src/filters/upsert.rs index 912d64be9e..56f378869a 100644 --- a/crates/nu-command/src/filters/upsert.rs +++ b/crates/nu-command/src/filters/upsert.rs @@ -178,12 +178,18 @@ fn upsert( if let Err(e) = input.upsert_data_at_cell_path(&cell_path.members, pd.into_value(span)) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input } - Err(e) => Value::Error { error: Box::new(e) }, + Err(e) => Value::Error { + error: Box::new(e), + span, + }, } }, ctrlc, @@ -219,7 +225,10 @@ fn upsert( let replacement = replacement.clone(); if let Err(e) = input.upsert_data_at_cell_path(&cell_path.members, replacement) { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } input diff --git a/crates/nu-command/src/filters/utils.rs b/crates/nu-command/src/filters/utils.rs index 1853f6d0e4..9e9b64cef0 100644 --- a/crates/nu-command/src/filters/utils.rs +++ b/crates/nu-command/src/filters/utils.rs @@ -7,9 +7,10 @@ use nu_protocol::{ pub fn chain_error_with_input( error_source: ShellError, - input_span: Result, + input_is_error: bool, + span: Span, ) -> ShellError { - if let Ok(span) = input_span { + if !input_is_error { return ShellError::EvalBlockWithInput(span, vec![error_source]); } error_source diff --git a/crates/nu-command/src/filters/values.rs b/crates/nu-command/src/filters/values.rs index 26608e9f15..d1cb0e0b27 100644 --- a/crates/nu-command/src/filters/values.rs +++ b/crates/nu-command/src/filters/values.rs @@ -119,7 +119,7 @@ pub fn get_values<'a>( } } } - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), _ => { return Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record or table".into(), @@ -176,12 +176,12 @@ fn values( Ok(val.vals.into_pipeline_data(ctrlc).set_metadata(metadata)) } // Propagate errors - PipelineData::Value(Value::Error { error }, ..) => Err(*error), + PipelineData::Value(Value::Error { error, .. }, ..) => Err(*error), PipelineData::Value(other, ..) => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record or table".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "record or table".into(), diff --git a/crates/nu-command/src/filters/where_.rs b/crates/nu-command/src/filters/where_.rs index f3493dd075..59e09eff78 100644 --- a/crates/nu-command/src/filters/where_.rs +++ b/crates/nu-command/src/filters/where_.rs @@ -101,6 +101,7 @@ not supported."# } Err(err) => Some(Value::Error { error: Box::new(err), + span, }), } }) diff --git a/crates/nu-command/src/formats/from/json.rs b/crates/nu-command/src/formats/from/json.rs index 6ec900399f..cada9d8663 100644 --- a/crates/nu-command/src/formats/from/json.rs +++ b/crates/nu-command/src/formats/from/json.rs @@ -77,6 +77,7 @@ impl Command for FromJson { Ok(v) => Some(v), Err(error) => Some(Value::Error { error: Box::new(error), + span, }), } } @@ -120,6 +121,7 @@ fn convert_nujson_to_value(value: &nu_json::Value, span: Span) -> Value { span, help: None, }), + span, } } else { Value::Int { diff --git a/crates/nu-command/src/formats/from/ods.rs b/crates/nu-command/src/formats/from/ods.rs index 8e6bbb8e7e..fea867a76f 100644 --- a/crates/nu-command/src/formats/from/ods.rs +++ b/crates/nu-command/src/formats/from/ods.rs @@ -45,7 +45,7 @@ impl Command for FromOds { let sel_sheets = if let Some(Value::List { vals: columns, .. }) = call.get_flag(engine_state, stack, "sheets")? { - convert_columns(columns.as_slice(), call.head)? + convert_columns(columns.as_slice())? } else { vec![] }; @@ -69,14 +69,14 @@ impl Command for FromOds { } } -fn convert_columns(columns: &[Value], span: Span) -> Result, ShellError> { +fn convert_columns(columns: &[Value]) -> Result, ShellError> { let res = columns .iter() .map(|value| match &value { Value::String { val: s, .. } => Ok(s.clone()), _ => Err(ShellError::IncompatibleParametersSingle { msg: "Incorrect column format, Only string as column name".to_string(), - span: value.span().unwrap_or(span), + span: value.span(), }), }) .collect::, _>>()?; @@ -93,13 +93,13 @@ fn collect_binary(input: PipelineData, span: Span) -> Result, ShellError Some(Value::Binary { val: b, .. }) => { bytes.extend_from_slice(&b); } - Some(Value::Error { error }) => return Err(*error), + 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.expect_span(), + x.span(), )) } None => break, diff --git a/crates/nu-command/src/formats/from/xlsx.rs b/crates/nu-command/src/formats/from/xlsx.rs index 317cca198f..28d0406186 100644 --- a/crates/nu-command/src/formats/from/xlsx.rs +++ b/crates/nu-command/src/formats/from/xlsx.rs @@ -45,7 +45,7 @@ impl Command for FromXlsx { let sel_sheets = if let Some(Value::List { vals: columns, .. }) = call.get_flag(engine_state, stack, "sheets")? { - convert_columns(columns.as_slice(), call.head)? + convert_columns(columns.as_slice())? } else { vec![] }; @@ -69,14 +69,14 @@ impl Command for FromXlsx { } } -fn convert_columns(columns: &[Value], span: Span) -> Result, ShellError> { +fn convert_columns(columns: &[Value]) -> Result, ShellError> { let res = columns .iter() .map(|value| match &value { Value::String { val: s, .. } => Ok(s.clone()), _ => Err(ShellError::IncompatibleParametersSingle { msg: "Incorrect column format, Only string as column name".to_string(), - span: value.span().unwrap_or(span), + span: value.span(), }), }) .collect::, _>>()?; @@ -98,7 +98,7 @@ fn collect_binary(input: PipelineData, span: Span) -> Result, ShellError "Expected binary from pipeline".to_string(), "value originates from here".into(), span, - x.expect_span(), + x.span(), )) } None => break, diff --git a/crates/nu-command/src/formats/to/delimited.rs b/crates/nu-command/src/formats/to/delimited.rs index 5508773c17..e906c390d8 100644 --- a/crates/nu-command/src/formats/to/delimited.rs +++ b/crates/nu-command/src/formats/to/delimited.rs @@ -14,8 +14,8 @@ fn from_value_to_delimited_string( Value::Record { val, span } => record_to_delimited(val, *span, separator, config, head), Value::List { vals, span } => table_to_delimited(vals, *span, separator, config, head), // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error.clone()), - v => Err(make_unsupported_input_error(v, head, v.expect_span())), + Value::Error { error, .. } => Err(*error.clone()), + v => Err(make_unsupported_input_error(v, head, v.span())), } } @@ -119,7 +119,7 @@ fn to_string_tagged_value( Value::Date { val, .. } => Ok(val.to_string()), Value::Nothing { .. } => Ok(String::new()), // Propagate existing errors - Value::Error { error } => Err(*error.clone()), + Value::Error { error, .. } => Err(*error.clone()), _ => Err(make_unsupported_input_error(v, head, span)), } } @@ -161,7 +161,7 @@ pub fn to_delimited_data( Err(_) => Err(ShellError::CantConvert { to_type: format_name.into(), from_type: value.get_type().to_string(), - span: value.span().unwrap_or(span), + span: value.span(), help: None, }), }?; diff --git a/crates/nu-command/src/formats/to/json.rs b/crates/nu-command/src/formats/to/json.rs index 518f0133b0..4426a4f8c0 100644 --- a/crates/nu-command/src/formats/to/json.rs +++ b/crates/nu-command/src/formats/to/json.rs @@ -76,6 +76,7 @@ impl Command for ToJson { span, help: None, }), + span, } .into_pipeline_data()), } @@ -126,7 +127,7 @@ pub fn value_to_json_value(v: &Value) -> Result { ), Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?), - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), Value::Closure { .. } | Value::Block { .. } | Value::Range { .. } diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 306349b6e8..acdefcb29e 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -85,6 +85,7 @@ impl Command for ToNuon { span, help: None, }), + span, } .into_pipeline_data()), } @@ -136,7 +137,7 @@ pub fn value_to_string( "could not convert binary to string".into(), "value originates from here".into(), span, - v.expect_span(), + v.span(), )); } } @@ -146,13 +147,13 @@ pub fn value_to_string( "blocks are currently not nuon-compatible".into(), "value originates from here".into(), span, - v.expect_span(), + v.span(), )), Value::Closure { .. } => Err(ShellError::UnsupportedInput( "closures are currently not nuon-compatible".into(), "value originates from here".into(), span, - v.expect_span(), + v.span(), )), Value::Bool { val, .. } => { if *val { @@ -165,19 +166,19 @@ pub fn value_to_string( "cellpaths are currently not nuon-compatible".to_string(), "value originates from here".into(), span, - v.expect_span(), + v.span(), )), Value::CustomValue { .. } => Err(ShellError::UnsupportedInput( "custom values are currently not nuon-compatible".to_string(), "value originates from here".into(), span, - v.expect_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)), // Propagate existing errors - Value::Error { error } => Err(*error.clone()), + Value::Error { error, .. } => Err(*error.clone()), // FIXME: make filesizes use the shortest lossless representation. Value::Filesize { val, .. } => Ok(format!("{}b", *val)), Value::Float { val, .. } => { @@ -246,7 +247,7 @@ pub fn value_to_string( "match patterns are currently not nuon-compatible".to_string(), "value originates from here".into(), span, - v.expect_span(), + v.span(), )), Value::Nothing { .. } => Ok("null".to_string()), Value::Range { val, .. } => Ok(format!( diff --git a/crates/nu-command/src/formats/to/text.rs b/crates/nu-command/src/formats/to/text.rs index 8db436a84e..4ce19b475f 100644 --- a/crates/nu-command/src/formats/to/text.rs +++ b/crates/nu-command/src/formats/to/text.rs @@ -148,7 +148,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), diff --git a/crates/nu-command/src/formats/to/toml.rs b/crates/nu-command/src/formats/to/toml.rs index aa2276779b..0b9e8e1763 100644 --- a/crates/nu-command/src/formats/to/toml.rs +++ b/crates/nu-command/src/formats/to/toml.rs @@ -77,7 +77,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result toml::Value::String("".to_string()), - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), Value::Binary { val, .. } => toml::Value::Array( val.iter() .map(|x| toml::Value::Integer(*x as i64)) @@ -125,6 +125,7 @@ fn toml_into_pipeline_data( span, help: None, }), + span, } .into_pipeline_data()), } @@ -138,12 +139,12 @@ fn value_to_toml_value( match v { Value::Record { .. } => helper(engine_state, v), // Propagate existing errors - Value::Error { error } => Err(*error.clone()), + 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.expect_span(), + v.span(), )), } } diff --git a/crates/nu-command/src/formats/to/xml.rs b/crates/nu-command/src/formats/to/xml.rs index 796d4a1f61..57663b0b99 100644 --- a/crates/nu-command/src/formats/to/xml.rs +++ b/crates/nu-command/src/formats/to/xml.rs @@ -97,7 +97,7 @@ fn to_xml_entry( top_level: bool, writer: &mut quick_xml::Writer, ) -> Result<(), ShellError> { - let entry_span = entry.span()?; + let entry_span = entry.span(); // Allow using strings directly as content. // So user can write @@ -197,7 +197,7 @@ fn to_tag_like( return Err(ShellError::CantConvert { to_type: "XML".into(), from_type: Type::Record(vec![]).to_string(), - span: content.span()?, + span: content.span(), help: Some("PI content expected to be a string".into()), }); } @@ -215,7 +215,7 @@ fn to_tag_like( return Err(ShellError::CantConvert { to_type: "XML".into(), from_type: attrs.get_type().to_string(), - span: attrs.span()?, + span: attrs.span(), help: Some("Tag attributes expected to be a record".into()), }); } @@ -228,7 +228,7 @@ fn to_tag_like( return Err(ShellError::CantConvert { to_type: "XML".into(), from_type: content.get_type().to_string(), - span: content.span()?, + span: content.span(), help: Some("Tag content expected to be a list".into()), }); } @@ -350,7 +350,7 @@ fn parse_attributes(attrs: Record) -> Result, ShellErro return Err(ShellError::CantConvert { to_type: "XML".to_string(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: Some("Attribute value expected to be a string".into()), }); } diff --git a/crates/nu-command/src/formats/to/yaml.rs b/crates/nu-command/src/formats/to/yaml.rs index a3f23d7209..a6fee74502 100644 --- a/crates/nu-command/src/formats/to/yaml.rs +++ b/crates/nu-command/src/formats/to/yaml.rs @@ -79,7 +79,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result { Value::Block { .. } => serde_yaml::Value::Null, Value::Closure { .. } => serde_yaml::Value::Null, Value::Nothing { .. } => serde_yaml::Value::Null, - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), Value::Binary { val, .. } => serde_yaml::Value::Sequence( val.iter() .map(|x| serde_yaml::Value::Number(serde_yaml::Number::from(*x))) @@ -118,6 +118,7 @@ fn to_yaml(input: PipelineData, head: Span) -> Result span: head, help: None, }), + span: head, } .into_pipeline_data()), } diff --git a/crates/nu-command/src/generators/seq_date.rs b/crates/nu-command/src/generators/seq_date.rs index 882edbde1e..79c6e936d2 100644 --- a/crates/nu-command/src/generators/seq_date.rs +++ b/crates/nu-command/src/generators/seq_date.rs @@ -197,7 +197,7 @@ pub fn run_seq_dates( return Err(ShellError::GenericError( "increment cannot be 0".to_string(), "increment cannot be 0".to_string(), - Some(increment.span()?), + Some(increment.span()), None, Vec::new(), )); diff --git a/crates/nu-command/src/hash/generic_digest.rs b/crates/nu-command/src/hash/generic_digest.rs index c88d6f2a45..cbbe4b1055 100644 --- a/crates/nu-command/src/hash/generic_digest.rs +++ b/crates/nu-command/src/hash/generic_digest.rs @@ -112,8 +112,9 @@ where exp_input_type: "string and binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, } .into_pipeline_data()); } @@ -157,22 +158,16 @@ where // Propagate existing errors Value::Error { .. } => return input.clone(), other => { - let span = match input.span() { - Ok(span) => span, - Err(error) => { - return Value::Error { - error: Box::new(error), - } - } - }; + let span = input.span(); return Value::Error { error: Box::new(ShellError::OnlySupportsThisInputType { exp_input_type: "string or binary".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), + span, }; } }; diff --git a/crates/nu-command/src/help/help_.rs b/crates/nu-command/src/help/help_.rs index d203e95005..765934d545 100644 --- a/crates/nu-command/src/help/help_.rs +++ b/crates/nu-command/src/help/help_.rs @@ -154,7 +154,7 @@ pub fn highlight_search_in_table( return Err(ShellError::NushellFailedSpanned { msg: "Expected record".to_string(), label: format!("got {}", record.get_type()), - span: record.span()?, + span: record.span(), }); }; diff --git a/crates/nu-command/src/hook.rs b/crates/nu-command/src/hook.rs index c83a864e23..4c82eaa464 100644 --- a/crates/nu-command/src/hook.rs +++ b/crates/nu-command/src/hook.rs @@ -44,7 +44,7 @@ pub fn eval_env_change_hook( x => { return Err(ShellError::TypeMismatch { err_message: "record for the 'env_change' hook".to_string(), - span: x.span()?, + span: x.span(), }); } } @@ -60,7 +60,7 @@ pub fn eval_hook( arguments: Vec<(String, Value)>, value: &Value, ) -> Result { - let value_span = value.span()?; + let value_span = value.span(); // Hooks can optionally be a record in this form: // { @@ -92,7 +92,7 @@ pub fn eval_hook( for (name, val) in arguments { let var_id = working_set.add_variable( name.as_bytes().to_vec(), - val.span()?, + val.span(), Type::Any, false, ); @@ -191,7 +191,7 @@ pub fn eval_hook( return Err(ShellError::UnsupportedConfigValue( "block".to_string(), format!("{}", other.get_type()), - other.span()?, + other.span(), )); } } @@ -214,7 +214,7 @@ pub fn eval_hook( for (name, val) in arguments { let var_id = working_set.add_variable( name.as_bytes().to_vec(), - val.span()?, + val.span(), Type::Any, false, ); @@ -293,7 +293,7 @@ pub fn eval_hook( return Err(ShellError::UnsupportedConfigValue( "block or string".to_string(), format!("{}", other.get_type()), - other.span()?, + other.span(), )); } } @@ -331,7 +331,7 @@ pub fn eval_hook( return Err(ShellError::UnsupportedConfigValue( "string, block, record, or list of commands".into(), format!("{}", other.get_type()), - other.span()?, + other.span(), )); } } @@ -374,7 +374,7 @@ fn run_hook_block( let pipeline_data = eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false)?; - if let PipelineData::Value(Value::Error { error }, _) = pipeline_data { + if let PipelineData::Value(Value::Error { error, .. }, _) = pipeline_data { return Err(*error); } diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index 75dae0630e..82f4a0098d 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -83,8 +83,9 @@ fn abs_helper(val: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/math/ceil.rs b/crates/nu-command/src/math/ceil.rs index e345e5a242..c6d12a5dd8 100644 --- a/crates/nu-command/src/math/ceil.rs +++ b/crates/nu-command/src/math/ceil.rs @@ -74,8 +74,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/math/floor.rs b/crates/nu-command/src/math/floor.rs index f60958254e..416558c619 100644 --- a/crates/nu-command/src/math/floor.rs +++ b/crates/nu-command/src/math/floor.rs @@ -74,8 +74,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/math/log.rs b/crates/nu-command/src/math/log.rs index 733c3b43e8..a7b40a3db6 100644 --- a/crates/nu-command/src/math/log.rs +++ b/crates/nu-command/src/math/log.rs @@ -109,6 +109,7 @@ fn operate(value: Value, head: Span, base: f64) -> Value { head, span, )), + span, }; } // Specialize for better precision/performance @@ -128,8 +129,9 @@ fn operate(value: Value, head: Span, base: f64) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/math/median.rs b/crates/nu-command/src/math/median.rs index 99a4639895..d4d29524ec 100644 --- a/crates/nu-command/src/math/median.rs +++ b/crates/nu-command/src/math/median.rs @@ -90,9 +90,9 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result Result Result { Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize)) } - Value::Error { error } => Err(*error.clone()), + Value::Error { error, .. } => Err(*error.clone()), other => Err(ShellError::UnsupportedInput( "Unable to give a result with this input".to_string(), "value originates from here".into(), head, - other.expect_span(), + other.span(), )), }) .collect::, ShellError>>()?; diff --git a/crates/nu-command/src/math/reducers.rs b/crates/nu-command/src/math/reducers.rs index d926534487..b7b130d2a7 100644 --- a/crates/nu-command/src/math/reducers.rs +++ b/crates/nu-command/src/math/reducers.rs @@ -42,9 +42,9 @@ pub fn max(data: Vec, span: Span, head: Span) -> Result, span: Span, head: Span) -> Result, span: Span, head: Span) -> Result { acc = acc.add(head, value, head)?; } - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => 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.expect_span(), + other.span(), )); } } @@ -145,14 +145,14 @@ pub fn product(data: Vec, span: Span, head: Span) -> Result { acc = acc.mul(head, value, head)?; } - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), other => { return Err(ShellError::UnsupportedInput( "Attempted to compute the product of a value that cannot be multiplied" .to_string(), "value originates from here".into(), head, - other.expect_span(), + other.span(), )); } } diff --git a/crates/nu-command/src/math/round.rs b/crates/nu-command/src/math/round.rs index ebe60b23dc..3e9277ebf7 100644 --- a/crates/nu-command/src/math/round.rs +++ b/crates/nu-command/src/math/round.rs @@ -126,8 +126,9 @@ fn operate(value: Value, head: Span, precision: Option) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/math/sqrt.rs b/crates/nu-command/src/math/sqrt.rs index 85b3134ff6..baf34c45e6 100644 --- a/crates/nu-command/src/math/sqrt.rs +++ b/crates/nu-command/src/math/sqrt.rs @@ -83,8 +83,9 @@ fn operate(value: Value, head: Span) -> Value { exp_input_type: "numeric".into(), wrong_type: other.get_type().to_string(), dst_span: head, - src_span: other.expect_span(), + src_span: other.span(), }), + span: head, }, } } @@ -97,6 +98,7 @@ fn error_negative_sqrt(head: Span, span: Span) -> Value { head, span, )), + span, } } diff --git a/crates/nu-command/src/math/utils.rs b/crates/nu-command/src/math/utils.rs index a704b1ddc7..bed730d211 100644 --- a/crates/nu-command/src/math/utils.rs +++ b/crates/nu-command/src/math/utils.rs @@ -34,10 +34,10 @@ fn helper_for_tables( .or_insert_with(|| vec![value.clone()]); } } - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), _ => { //Turns out we are not dealing with a table - return mf(values, val.expect_span(), name); + return mf(values, val.span(), name); } } } diff --git a/crates/nu-command/src/math/variance.rs b/crates/nu-command/src/math/variance.rs index 6437a90130..3f470e7a82 100644 --- a/crates/nu-command/src/math/variance.rs +++ b/crates/nu-command/src/math/variance.rs @@ -64,13 +64,13 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result { for value in values { let v = match &value { Value::Int { .. } | Value::Float { .. } => Ok(value), - Value::Error { error } => Err(*error.clone()), + Value::Error { error, .. } => Err(*error.clone()), _ => Err(ShellError::UnsupportedInput( "Attempted to compute the sum of squares of a non-integer, non-float value" .to_string(), "value originates from here".into(), span, - value.expect_span(), + value.span(), )), }?; let v_squared = &v.mul(span, v, span)?; diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index bf203f567a..8e468024de 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -278,7 +278,7 @@ pub fn request_set_timeout( if val.is_negative() || val < 1 { return Err(ShellError::TypeMismatch { err_message: "Timeout value must be an integer and larger than 0".to_string(), - span: timeout.expect_span(), + span: timeout.span(), }); } @@ -316,7 +316,7 @@ pub fn request_add_custom_headers( return Err(ShellError::CantConvert { to_type: "string list or single row".into(), from_type: x.get_type().to_string(), - span: headers.span().unwrap_or_else(|_| Span::new(0, 0)), + span: headers.span(), help: None, }); } @@ -335,7 +335,7 @@ pub fn request_add_custom_headers( return Err(ShellError::CantConvert { to_type: "string list or single row".into(), from_type: x.get_type().to_string(), - span: headers.span().unwrap_or_else(|_| Span::new(0, 0)), + span: headers.span(), help: None, }); } diff --git a/crates/nu-command/src/network/http/delete.rs b/crates/nu-command/src/network/http/delete.rs index 83c3991566..eb574d1e8a 100644 --- a/crates/nu-command/src/network/http/delete.rs +++ b/crates/nu-command/src/network/http/delete.rs @@ -183,7 +183,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/http/get.rs b/crates/nu-command/src/network/http/get.rs index eb3ffbed67..3bd613abdf 100644 --- a/crates/nu-command/src/network/http/get.rs +++ b/crates/nu-command/src/network/http/get.rs @@ -167,7 +167,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/http/head.rs b/crates/nu-command/src/network/http/head.rs index 69ff026e42..d6b4a86033 100644 --- a/crates/nu-command/src/network/http/head.rs +++ b/crates/nu-command/src/network/http/head.rs @@ -141,7 +141,7 @@ fn helper( args: Arguments, ctrlc: Option>, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let (requested_url, _) = http_parse_url(call, span, args.url)?; let client = http_client(args.insecure); diff --git a/crates/nu-command/src/network/http/options.rs b/crates/nu-command/src/network/http/options.rs index 7449fcbbb8..43035e123a 100644 --- a/crates/nu-command/src/network/http/options.rs +++ b/crates/nu-command/src/network/http/options.rs @@ -156,7 +156,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/http/patch.rs b/crates/nu-command/src/network/http/patch.rs index 27fc1c024f..f94d10e8da 100644 --- a/crates/nu-command/src/network/http/patch.rs +++ b/crates/nu-command/src/network/http/patch.rs @@ -173,7 +173,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/http/post.rs b/crates/nu-command/src/network/http/post.rs index bcdb0de8a1..bf0b3a0816 100644 --- a/crates/nu-command/src/network/http/post.rs +++ b/crates/nu-command/src/network/http/post.rs @@ -173,7 +173,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/http/put.rs b/crates/nu-command/src/network/http/put.rs index be6a26cb50..8b49e40aef 100644 --- a/crates/nu-command/src/network/http/put.rs +++ b/crates/nu-command/src/network/http/put.rs @@ -173,7 +173,7 @@ fn helper( call: &Call, args: Arguments, ) -> Result { - let span = args.url.span()?; + let span = args.url.span(); let ctrl_c = engine_state.ctrlc.clone(); let (requested_url, _) = http_parse_url(call, span, args.url)?; diff --git a/crates/nu-command/src/network/url/build_query.rs b/crates/nu-command/src/network/url/build_query.rs index 4f6a9abadb..3c397c36ef 100644 --- a/crates/nu-command/src/network/url/build_query.rs +++ b/crates/nu-command/src/network/url/build_query.rs @@ -94,12 +94,12 @@ fn to_url(input: PipelineData, head: Span) -> Result { } } // Propagate existing errors - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::UnsupportedInput( "Expected a table from pipeline".to_string(), "value originates from here".into(), head, - other.expect_span(), + other.span(), )), }) .collect(); diff --git a/crates/nu-command/src/network/url/encode.rs b/crates/nu-command/src/network/url/encode.rs index df15969233..60b7d31a53 100644 --- a/crates/nu-command/src/network/url/encode.rs +++ b/crates/nu-command/src/network/url/encode.rs @@ -109,8 +109,9 @@ fn action_all(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } @@ -130,8 +131,9 @@ fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/network/url/join.rs b/crates/nu-command/src/network/url/join.rs index f9869b7c5b..f36613af38 100644 --- a/crates/nu-command/src/network/url/join.rs +++ b/crates/nu-command/src/network/url/join.rs @@ -101,12 +101,12 @@ impl Command for SubCommand { url_components?.to_url(span) } - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::UnsupportedInput( "Expected a record from pipeline".to_string(), "value originates from here".into(), head, - other.expect_span(), + other.span(), )), }) .collect(); @@ -159,12 +159,12 @@ impl UrlComponents { port: Some(val), ..self }), - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::IncompatibleParametersSingle { msg: String::from( "Port parameter should be an unsigned integer or a string representing it", ), - span: other.expect_span(), + span: other.span(), }), }; } @@ -192,7 +192,7 @@ impl UrlComponents { // if query is present it means that also query_span is set. return Err(ShellError::IncompatibleParameters { left_message: format!("Mismatch, qs from params is: {qs}"), - left_span: value.expect_span(), + left_span: value.span(), right_message: format!("instead query is: {q}"), right_span: self.query_span.unwrap_or(Span::unknown()), }); @@ -205,10 +205,10 @@ impl UrlComponents { ..self }) } - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::IncompatibleParametersSingle { msg: String::from("Key params has to be a record"), - span: other.expect_span(), + span: other.span(), }), }; } @@ -250,7 +250,7 @@ impl UrlComponents { // if query is present it means that also params_span is set. return Err(ShellError::IncompatibleParameters { left_message: format!("Mismatch, query param is: {s}"), - left_span: value.expect_span(), + left_span: value.span(), right_message: format!("instead qs from params is: {q}"), right_span: self.params_span.unwrap_or(Span::unknown()), }); @@ -259,7 +259,7 @@ impl UrlComponents { Ok(Self { query: Some(format!("?{s}")), - query_span: Some(value.expect_span()), + query_span: Some(value.span()), ..self }) } diff --git a/crates/nu-command/src/network/url/parse.rs b/crates/nu-command/src/network/url/parse.rs index f6bc11627c..f2fa4a21eb 100644 --- a/crates/nu-command/src/network/url/parse.rs +++ b/crates/nu-command/src/network/url/parse.rs @@ -101,7 +101,7 @@ fn parse(value: Value, head: Span, engine_state: &EngineState) -> Result { diff --git a/crates/nu-command/src/path/exists.rs b/crates/nu-command/src/path/exists.rs index d457cd51db..a531a092a6 100644 --- a/crates/nu-command/src/path/exists.rs +++ b/crates/nu-command/src/path/exists.rs @@ -113,6 +113,7 @@ fn exists(path: &Path, span: Span, args: &Arguments) -> Value { Err(err) => { return Value::Error { error: Box::new(ShellError::IOErrorSpanned(err.to_string(), span)), + span, } } }, diff --git a/crates/nu-command/src/path/expand.rs b/crates/nu-command/src/path/expand.rs index 99e72e3d5e..3aacd9f101 100644 --- a/crates/nu-command/src/path/expand.rs +++ b/crates/nu-command/src/path/expand.rs @@ -140,6 +140,7 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value { None, Vec::new(), )), + span, }, } } else if args.not_follow_symlink { diff --git a/crates/nu-command/src/path/join.rs b/crates/nu-command/src/path/join.rs index 3a68f5604d..4acde4cfaa 100644 --- a/crates/nu-command/src/path/join.rs +++ b/crates/nu-command/src/path/join.rs @@ -188,6 +188,7 @@ fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value dst_span: head, src_span: span, }), + span, }, } } @@ -199,6 +200,7 @@ fn join_record(record: &Record, head: Span, span: Span, args: &Arguments) -> Val Ok(p) => join_single(p.as_path(), head, args), Err(error) => Value::Error { error: Box::new(error), + span, }, } } diff --git a/crates/nu-command/src/path/mod.rs b/crates/nu-command/src/path/mod.rs index 09672b0ae2..d1fc709a36 100644 --- a/crates/nu-command/src/path/mod.rs +++ b/crates/nu-command/src/path/mod.rs @@ -45,27 +45,28 @@ where fn handle_invalid_values(rest: Value, name: Span) -> Value { Value::Error { error: Box::new(err_from_value(&rest, name)), + span: name, } } fn err_from_value(rest: &Value, name: Span) -> ShellError { - match rest.span() { - Ok(span) => { + match rest { + Value::Error { error, .. } => *error.clone(), + _ => { if rest.is_nothing() { ShellError::OnlySupportsThisInputType { exp_input_type: "string, record or list".into(), wrong_type: "nothing".into(), dst_span: name, - src_span: span, + src_span: rest.span(), } } else { ShellError::PipelineMismatch { exp_input_type: "string, row or list".into(), dst_span: name, - src_span: span, + src_span: rest.span(), } } } - Err(error) => error, } } diff --git a/crates/nu-command/src/path/relative_to.rs b/crates/nu-command/src/path/relative_to.rs index 5b12986f07..15f80ff1b1 100644 --- a/crates/nu-command/src/path/relative_to.rs +++ b/crates/nu-command/src/path/relative_to.rs @@ -135,6 +135,7 @@ fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value { span, help: None, }), + span, }, } } diff --git a/crates/nu-command/src/platform/ansi/ansi_.rs b/crates/nu-command/src/platform/ansi/ansi_.rs index cb8ed32b1b..381fcea1e1 100644 --- a/crates/nu-command/src/platform/ansi/ansi_.rs +++ b/crates/nu-command/src/platform/ansi/ansi_.rs @@ -733,7 +733,7 @@ Operating system commands: return Err(ShellError::GenericError( "error parsing hex color".to_string(), format!("{err}"), - Some(code.span()?), + Some(code.span()), None, Vec::new(), )); @@ -772,7 +772,7 @@ Operating system commands: _ => { return Err(ShellError::IncompatibleParametersSingle { msg: format!("unknown ANSI format key: expected one of ['fg', 'bg', 'attr'], found '{k}'"), - span: code.expect_span(), + span: code.span(), }) } } diff --git a/crates/nu-command/src/platform/ansi/strip.rs b/crates/nu-command/src/platform/ansi/strip.rs index a0b567c59c..410656c964 100644 --- a/crates/nu-command/src/platform/ansi/strip.rs +++ b/crates/nu-command/src/platform/ansi/strip.rs @@ -55,7 +55,7 @@ impl Command for SubCommand { } } -fn action(input: &Value, _args: &CellPathOnlyArgs, command_span: Span) -> Value { +fn action(input: &Value, _args: &CellPathOnlyArgs, _span: Span) -> Value { match input { Value::String { val, span } => { Value::string(nu_utils::strip_ansi_likely(val).to_string(), *span) @@ -66,8 +66,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, command_span: Span) -> Value Value::Error { error: Box::new(ShellError::TypeMismatch { err_message: got, - span: other.span().unwrap_or(command_span), + span: other.span(), }), + span: other.span(), } } } diff --git a/crates/nu-command/src/platform/du.rs b/crates/nu-command/src/platform/du.rs index 4266793f77..3fbc5e599d 100644 --- a/crates/nu-command/src/platform/du.rs +++ b/crates/nu-command/src/platform/du.rs @@ -179,7 +179,10 @@ impl Command for Du { } } Err(e) => { - output.push(Value::Error { error: Box::new(e) }); + output.push(Value::Error { + error: Box::new(e), + span: tag, + }); } } } diff --git a/crates/nu-command/src/platform/input/input_listen.rs b/crates/nu-command/src/platform/input/input_listen.rs index 4cd1ce90ce..0dfcd0bf4c 100644 --- a/crates/nu-command/src/platform/input/input_listen.rs +++ b/crates/nu-command/src/platform/input/input_listen.rs @@ -184,7 +184,7 @@ impl EventTypeFilter { "--types expects a list of strings".to_string(), "value originates from here".into(), head, - value.span().unwrap_or(head), + value.span(), ) } diff --git a/crates/nu-command/src/strings/detect_columns.rs b/crates/nu-command/src/strings/detect_columns.rs index 74bfa2da09..95e043efae 100644 --- a/crates/nu-command/src/strings/detect_columns.rs +++ b/crates/nu-command/src/strings/detect_columns.rs @@ -219,6 +219,7 @@ fn detect_columns( let err = processing_error("could not find range index", name_span); return Value::Error { error: Box::new(err), + span: name_span, }; } } diff --git a/crates/nu-command/src/strings/encode_decode/base64.rs b/crates/nu-command/src/strings/encode_decode/base64.rs index d6f6a419d0..573d4679c3 100644 --- a/crates/nu-command/src/strings/encode_decode/base64.rs +++ b/crates/nu-command/src/strings/encode_decode/base64.rs @@ -99,12 +99,12 @@ fn action( Some(config_character_set.span), None, Vec::new(), - ))} + )), span:config_character_set.span} }; match input { // Propagate existing errors. Value::Error { .. } => input.clone(), - Value::Binary { val, .. } => match base64_config.action_type { + Value::Binary { val, span } => match base64_config.action_type { ActionType::Encode => { let mut enc_vec = Vec::new(); enc_vec.resize(val.len() * 4 / 3 + 4, 0); @@ -115,10 +115,11 @@ fn action( error: Box::new(ShellError::GenericError( "Error encoding data".into(), err.to_string(), - Some(Span::unknown()), + Some(*span), None, Vec::new(), )), + span: *span, } } }; @@ -131,8 +132,9 @@ fn action( "value originates from here".into(), command_span, // This line requires the Value::Error {} match above. - input.expect_span(), + input.span(), )), + span: command_span, }, }, Value::String { @@ -167,6 +169,7 @@ fn action( Some("consider using the `--binary` flag".to_owned()), Vec::new(), )), + span: *value_span, }, } } @@ -182,6 +185,7 @@ fn action( None, Vec::new(), )), + span: command_span, }, } } @@ -190,8 +194,9 @@ fn action( other => Value::Error { error: Box::new(ShellError::TypeMismatch { err_message: format!("string or binary, not {}", other.get_type()), - span: other.span().unwrap_or(command_span), + span: other.span(), }), + span: other.span(), }, } } diff --git a/crates/nu-command/src/strings/encode_decode/decode.rs b/crates/nu-command/src/strings/encode_decode/decode.rs index 1387261cdd..f99042637b 100644 --- a/crates/nu-command/src/strings/encode_decode/decode.rs +++ b/crates/nu-command/src/strings/encode_decode/decode.rs @@ -77,12 +77,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"# PipelineData::Value(v, ..) => match v { Value::Binary { val: bytes, .. } => super::encoding::decode(head, encoding, &bytes) .map(|val| val.into_pipeline_data()), - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), _ => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "binary".into(), wrong_type: v.get_type().to_string(), dst_span: head, - src_span: v.expect_span(), + src_span: v.span(), }), }, // This should be more precise, but due to difficulties in getting spans diff --git a/crates/nu-command/src/strings/encode_decode/encode.rs b/crates/nu-command/src/strings/encode_decode/encode.rs index f475701c67..8e977165e7 100644 --- a/crates/nu-command/src/strings/encode_decode/encode.rs +++ b/crates/nu-command/src/strings/encode_decode/encode.rs @@ -100,12 +100,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"# super::encoding::encode(head, encoding, &s, span, ignore_errors) .map(|val| val.into_pipeline_data()) } - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), _ => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "string".into(), wrong_type: v.get_type().to_string(), dst_span: head, - src_span: v.expect_span(), + src_span: v.span(), }), }, // This should be more precise, but due to difficulties in getting spans diff --git a/crates/nu-command/src/strings/format/date.rs b/crates/nu-command/src/strings/format/date.rs index e14d895c28..6e4e05b2d0 100644 --- a/crates/nu-command/src/strings/format/date.rs +++ b/crates/nu-command/src/strings/format/date.rs @@ -135,6 +135,7 @@ where err_message: "invalid format".to_string(), span, }), + span, }, } } @@ -155,6 +156,7 @@ fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span: value.debug_value(), head_span, )), + span: head_span, }, } } @@ -180,6 +182,7 @@ fn format_helper_rfc2822(value: Value, span: Span) -> Value { } _ => Value::Error { error: Box::new(ShellError::DatetimeParseError(value.debug_value(), span)), + span, }, } } diff --git a/crates/nu-command/src/strings/format/duration.rs b/crates/nu-command/src/strings/format/duration.rs index 38d1eccef3..66e6c29ad0 100644 --- a/crates/nu-command/src/strings/format/duration.rs +++ b/crates/nu-command/src/strings/format/duration.rs @@ -137,7 +137,10 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value { } } } - Err(e) => Value::Error { error: Box::new(e) }, + Err(e) => Value::Error { + error: Box::new(e), + span: *inner_span, + }, } } Value::Error { .. } => val.clone(), @@ -146,8 +149,9 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value { exp_input_type: "filesize".into(), wrong_type: val.get_type().to_string(), dst_span: span, - src_span: val.expect_span(), + src_span: val.span(), }), + span, }, } } diff --git a/crates/nu-command/src/strings/format/filesize.rs b/crates/nu-command/src/strings/format/filesize.rs index 0ad430c7a3..ab04467245 100644 --- a/crates/nu-command/src/strings/format/filesize.rs +++ b/crates/nu-command/src/strings/format/filesize.rs @@ -115,8 +115,9 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value { exp_input_type: "filesize".into(), wrong_type: val.get_type().to_string(), dst_span: span, - src_span: val.expect_span(), + src_span: val.span(), }), + span, }, } } diff --git a/crates/nu-command/src/strings/parse.rs b/crates/nu-command/src/strings/parse.rs index 2a520f5e46..01affd492a 100644 --- a/crates/nu-command/src/strings/parse.rs +++ b/crates/nu-command/src/strings/parse.rs @@ -186,7 +186,7 @@ fn operate( } }; - let v_span = v.span()?; + let v_span = v.span(); let record = columns .iter() .zip(captures.iter().skip(1)) @@ -203,7 +203,7 @@ fn operate( return Err(ShellError::PipelineMismatch { exp_input_type: "string".into(), dst_span: head, - src_span: v.span()?, + src_span: v.span(), }) } } @@ -346,14 +346,15 @@ impl Iterator for ParseStreamer { error: Box::new(ShellError::PipelineMismatch { exp_input_type: "string".into(), dst_span: self.span, - src_span: v.span().unwrap_or(self.span), + src_span: v.span(), }), + span: v.span(), }) }; let parsed = stream_helper( self.regex.clone(), - v.span().unwrap_or(self.span), + v.span(), s, self.columns.clone(), &mut self.excess, @@ -403,6 +404,7 @@ impl Iterator for ParseStreamerExternal { Some(Err(err)) => { return Some(Value::Error { error: Box::new(err), + span: self.span, }) } _ => return None, @@ -415,6 +417,7 @@ impl Iterator for ParseStreamerExternal { dst_span: self.span, src_span: self.span, }), + span: self.span, }) }; @@ -445,10 +448,11 @@ fn stream_helper( error: Box::new(ShellError::GenericError( "Error with regular expression captures".into(), e.to_string(), - None, - None, + Some(span), + Some(e.to_string()), Vec::new(), )), + span, }) } }; diff --git a/crates/nu-command/src/strings/size.rs b/crates/nu-command/src/strings/size.rs index c4dab8296a..96562df459 100644 --- a/crates/nu-command/src/strings/size.rs +++ b/crates/nu-command/src/strings/size.rs @@ -122,9 +122,14 @@ fn size( input.map( move |v| { // First, obtain the span. If this fails, propagate the error that results. - let value_span = match v.span() { - Err(v) => return Value::Error { error: Box::new(v) }, - Ok(v) => v, + let value_span = match &v { + Value::Error { error, span } => { + return Value::Error { + error: error.clone(), + span: *span, + } + } + v => v.span(), }; // Now, check if it's a string. match v.as_string() { @@ -135,6 +140,7 @@ fn size( dst_span: span, src_span: value_span, }), + span, }, } }, diff --git a/crates/nu-command/src/strings/split/chars.rs b/crates/nu-command/src/strings/split/chars.rs index 6e6b64fbeb..4f47667b4a 100644 --- a/crates/nu-command/src/strings/split/chars.rs +++ b/crates/nu-command/src/strings/split/chars.rs @@ -118,8 +118,13 @@ fn split_chars( } fn split_chars_helper(v: &Value, name: Span, graphemes: bool) -> Value { - match v.span() { - Ok(v_span) => { + match v { + Value::Error { error, span } => Value::Error { + error: error.clone(), + span: *span, + }, + v => { + let v_span = v.span(); if let Ok(s) = v.as_string() { Value::List { vals: if graphemes { @@ -144,12 +149,10 @@ fn split_chars_helper(v: &Value, name: Span, graphemes: bool) -> Value { dst_span: name, src_span: v_span, }), + span: name, } } } - Err(error) => Value::Error { - error: Box::new(error), - }, } } diff --git a/crates/nu-command/src/strings/split/column.rs b/crates/nu-command/src/strings/split/column.rs index 3b95bbeea2..0e89727c3c 100644 --- a/crates/nu-command/src/strings/split/column.rs +++ b/crates/nu-command/src/strings/split/column.rs @@ -201,17 +201,24 @@ fn split_column_helper( } vec![Value::record(record, head)] } else { - match v.span() { - Ok(span) => vec![Value::Error { - error: Box::new(ShellError::PipelineMismatch { - exp_input_type: "string".into(), - dst_span: head, - src_span: span, - }), - }], - Err(error) => vec![Value::Error { - error: Box::new(error), - }], + match v { + Value::Error { error, .. } => { + vec![Value::Error { + error: Box::new(*error.clone()), + span: head, + }] + } + v => { + let span = v.span(); + vec![Value::Error { + error: Box::new(ShellError::PipelineMismatch { + exp_input_type: "string".into(), + dst_span: head, + src_span: span, + }), + span, + }] + } } } } diff --git a/crates/nu-command/src/strings/split/list.rs b/crates/nu-command/src/strings/split/list.rs index 7bfc4fb8a4..428947de4a 100644 --- a/crates/nu-command/src/strings/split/list.rs +++ b/crates/nu-command/src/strings/split/list.rs @@ -169,8 +169,8 @@ impl Matcher { "Error with regular expression".into(), err.to_string(), match lhs { - Value::Error { error: _ } => None, - _ => Some(lhs.expect_span()), + Value::Error { .. } => None, + _ => Some(lhs.span()), }, None, Vec::new(), diff --git a/crates/nu-command/src/strings/split/row.rs b/crates/nu-command/src/strings/split/row.rs index 8d2eaa9be6..d225020720 100644 --- a/crates/nu-command/src/strings/split/row.rs +++ b/crates/nu-command/src/strings/split/row.rs @@ -146,8 +146,16 @@ fn split_row( } fn split_row_helper(v: &Value, regex: &Regex, max_split: Option, name: Span) -> Vec { - match v.span() { - Ok(v_span) => { + match v { + Value::Error { error, span } => { + vec![Value::Error { + error: Box::new(*error.clone()), + span: *span, + }] + } + v => { + let v_span = v.span(); + if let Ok(s) = v.as_string() { match max_split { Some(max_split) => regex @@ -166,12 +174,10 @@ fn split_row_helper(v: &Value, regex: &Regex, max_split: Option, name: Sp dst_span: name, src_span: v_span, }), + span: name, }] } } - Err(error) => vec![Value::Error { - error: Box::new(error), - }], } } diff --git a/crates/nu-command/src/strings/split/words.rs b/crates/nu-command/src/strings/split/words.rs index 5ec7b88650..4bc50b2d3c 100644 --- a/crates/nu-command/src/strings/split/words.rs +++ b/crates/nu-command/src/strings/split/words.rs @@ -153,8 +153,13 @@ fn split_words_helper(v: &Value, word_length: Option, span: Span, graphem // Let's go with the unicode one in hopes that it works on more than just ascii characters let regex_replace = Regex::new(r"[^\p{L}\']").expect("regular expression error"); - match v.span() { - Ok(v_span) => { + match v { + Value::Error { error, span } => Value::Error { + error: Box::new(*error.clone()), + span: *span, + }, + v => { + let v_span = v.span(); if let Ok(s) = v.as_string() { // let splits = s.unicode_words(); // let words = trim_to_words(s); @@ -195,12 +200,10 @@ fn split_words_helper(v: &Value, word_length: Option, span: Span, graphem dst_span: span, src_span: v_span, }), + span, } } } - Err(error) => Value::Error { - error: Box::new(error), - }, } } diff --git a/crates/nu-command/src/strings/str_/case/capitalize.rs b/crates/nu-command/src/strings/str_/case/capitalize.rs index a5fcaf51a3..e5c714de7e 100644 --- a/crates/nu-command/src/strings/str_/case/capitalize.rs +++ b/crates/nu-command/src/strings/str_/case/capitalize.rs @@ -100,6 +100,7 @@ fn operate( if let Err(error) = r { return Value::Error { error: Box::new(error), + span: head, }; } } @@ -122,8 +123,9 @@ fn action(input: &Value, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/case/downcase.rs b/crates/nu-command/src/strings/str_/case/downcase.rs index fb5d01fb4a..b054b1c3e8 100644 --- a/crates/nu-command/src/strings/str_/case/downcase.rs +++ b/crates/nu-command/src/strings/str_/case/downcase.rs @@ -111,6 +111,7 @@ fn operate( if let Err(error) = r { return Value::Error { error: Box::new(error), + span: head, }; } } @@ -133,8 +134,9 @@ fn action(input: &Value, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/case/mod.rs b/crates/nu-command/src/strings/str_/case/mod.rs index 2aa35e981b..c602a4c319 100644 --- a/crates/nu-command/src/strings/str_/case/mod.rs +++ b/crates/nu-command/src/strings/str_/case/mod.rs @@ -61,8 +61,9 @@ where exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/case/upcase.rs b/crates/nu-command/src/strings/str_/case/upcase.rs index ffb6e24869..a0b61a7081 100644 --- a/crates/nu-command/src/strings/str_/case/upcase.rs +++ b/crates/nu-command/src/strings/str_/case/upcase.rs @@ -80,6 +80,7 @@ fn operate( if let Err(error) = r { return Value::Error { error: Box::new(error), + span: head, }; } } @@ -102,8 +103,9 @@ fn action(input: &Value, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/contains.rs b/crates/nu-command/src/strings/str_/contains.rs index 740fa4451f..0e3b65bb75 100644 --- a/crates/nu-command/src/strings/str_/contains.rs +++ b/crates/nu-command/src/strings/str_/contains.rs @@ -187,8 +187,9 @@ fn action( exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/distance.rs b/crates/nu-command/src/strings/str_/distance.rs index 31d102a5f9..a6cdc9d27a 100644 --- a/crates/nu-command/src/strings/str_/distance.rs +++ b/crates/nu-command/src/strings/str_/distance.rs @@ -116,8 +116,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/ends_with.rs b/crates/nu-command/src/strings/str_/ends_with.rs index e3d8311bcc..ba2d446097 100644 --- a/crates/nu-command/src/strings/str_/ends_with.rs +++ b/crates/nu-command/src/strings/str_/ends_with.rs @@ -110,8 +110,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/expand.rs b/crates/nu-command/src/strings/str_/expand.rs index 08e4bbe1e8..1c8fed4bc5 100644 --- a/crates/nu-command/src/strings/str_/expand.rs +++ b/crates/nu-command/src/strings/str_/expand.rs @@ -197,14 +197,11 @@ impl Command for SubCommand { let is_path = call.has_flag("path"); input.map( move |v| { - let value_span = match v.span() { - Err(v) => return Value::Error { error: Box::new(v) }, - Ok(v) => v, - }; + let value_span = v.span(); match v.as_string() { Ok(s) => { let contents = if is_path { s.replace('\\', "\\\\") } else { s }; - str_expand(&contents, span, v.expect_span()) + str_expand(&contents, span, v.span()) } Err(_) => Value::Error { error: Box::new(ShellError::PipelineMismatch { @@ -212,6 +209,7 @@ impl Command for SubCommand { dst_span: span, src_span: value_span, }), + span, }, } }, @@ -236,7 +234,9 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value { }, Err(e) => match e { bracoxide::ExpansionError::NumConversionFailed(s) => Value::Error { error: - Box::new(ShellError::GenericError("Number Conversion Failed".to_owned(), format!("Number conversion failed at {s}."), Some(value_span), Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".to_owned()), vec![])) }, + Box::new(ShellError::GenericError("Number Conversion Failed".to_owned(), format!("Number conversion failed at {s}."), Some(value_span), Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".to_owned()), vec![])), + span, + }, }, } }, @@ -255,12 +255,15 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value { ParsingError::ExtraOBra(s) => ShellError::GenericError("Extra Opening Brace".to_owned(), format!("Used extra opening brace at {s}."), Some(value_span), Some("To escape opening brace use backslash, e.g. `\\{`".to_owned()), vec![]), ParsingError::NothingInBraces(s) => ShellError::GenericError("Nothing In Braces".to_owned(), format!("Nothing found inside braces at {s}."), Some(value_span), Some("Please provide valid content within the braces. Additionally, you can safely remove it, not needed.".to_owned()), vec![]), } - ) } + ), + span, + } } }, Err(e) => match e { TokenizationError::EmptyContent => Value::Error { error: Box::new(ShellError::PipelineEmpty { dst_span: value_span }), + span: value_span, }, TokenizationError::FormatNotSupported => Value::Error { error: Box::new( @@ -270,10 +273,12 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value { Some(value_span), Some("In brace expansion syntax, it is important to have an equal number of opening (`{`) and closing (`}`) braces. Please ensure that you provide a balanced pair of braces in your brace expansion pattern.".to_owned()), vec![] - )) + )), + span: value_span, }, TokenizationError::NoBraces => Value::Error { - error: Box::new(ShellError::GenericError("No Braces".to_owned(), "At least one `{}` brace expansion expected.".to_owned(), Some(value_span), Some("Please, examine the examples.".to_owned()), vec![])) + error: Box::new(ShellError::GenericError("No Braces".to_owned(), "At least one `{}` brace expansion expected.".to_owned(), Some(value_span), Some("Please, examine the examples.".to_owned()), vec![])), + span: value_span, } }, } diff --git a/crates/nu-command/src/strings/str_/index_of.rs b/crates/nu-command/src/strings/str_/index_of.rs index 3b9f877b43..fd49b14eda 100644 --- a/crates/nu-command/src/strings/str_/index_of.rs +++ b/crates/nu-command/src/strings/str_/index_of.rs @@ -163,6 +163,7 @@ fn action( let err = processing_error("could not find `index-of`", head); return Value::Error { error: Box::new(err), + span: head, }; } } @@ -202,8 +203,9 @@ fn action( exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/join.rs b/crates/nu-command/src/strings/str_/join.rs index 15e998c94d..561c908ac4 100644 --- a/crates/nu-command/src/strings/str_/join.rs +++ b/crates/nu-command/src/strings/str_/join.rs @@ -55,7 +55,7 @@ impl Command for StrJoin { for value in input { match value { - Value::Error { error } => { + Value::Error { error, .. } => { return Err(*error); } value => { diff --git a/crates/nu-command/src/strings/str_/length.rs b/crates/nu-command/src/strings/str_/length.rs index 5f08a7dd7f..ef1b07f8c6 100644 --- a/crates/nu-command/src/strings/str_/length.rs +++ b/crates/nu-command/src/strings/str_/length.rs @@ -117,8 +117,9 @@ fn action(input: &Value, arg: &Arguments, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/replace.rs b/crates/nu-command/src/strings/str_/replace.rs index 8d42c6b259..b84a3c9b7d 100644 --- a/crates/nu-command/src/strings/str_/replace.rs +++ b/crates/nu-command/src/strings/str_/replace.rs @@ -276,6 +276,7 @@ fn action( val_span: find.span, call_span: head, }), + span: find.span, }, } } @@ -286,8 +287,9 @@ fn action( exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/reverse.rs b/crates/nu-command/src/strings/str_/reverse.rs index f3fa11cb96..a05431e44f 100644 --- a/crates/nu-command/src/strings/str_/reverse.rs +++ b/crates/nu-command/src/strings/str_/reverse.rs @@ -89,8 +89,9 @@ fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value { exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/starts_with.rs b/crates/nu-command/src/strings/str_/starts_with.rs index 9a44551b99..21a4f5046d 100644 --- a/crates/nu-command/src/strings/str_/starts_with.rs +++ b/crates/nu-command/src/strings/str_/starts_with.rs @@ -123,8 +123,9 @@ fn action( exp_input_type: "string".into(), wrong_type: input.get_type().to_string(), dst_span: head, - src_span: input.expect_span(), + src_span: input.span(), }), + span: head, }, } } diff --git a/crates/nu-command/src/strings/str_/substring.rs b/crates/nu-command/src/strings/str_/substring.rs index c5ad1a6404..22d353e3d7 100644 --- a/crates/nu-command/src/strings/str_/substring.rs +++ b/crates/nu-command/src/strings/str_/substring.rs @@ -149,6 +149,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { err_message: "End must be greater than or equal to Start".to_string(), span: head, }), + span: head, }, Ordering::Less => Value::String { val: { @@ -195,8 +196,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { format!("input type: {:?}", other.get_type()), head, // This line requires the Value::Error match above. - other.expect_span(), + other.span(), )), + 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 39b8ce9206..7fdef6f030 100644 --- a/crates/nu-command/src/strings/str_/trim/trim_.rs +++ b/crates/nu-command/src/strings/str_/trim/trim_.rs @@ -208,8 +208,9 @@ fn action(input: &Value, arg: &Arguments, head: Span) -> Value { format!("input type: {:?}", other.get_type()), head, // This line requires the Value::Error match above. - other.expect_span(), + other.span(), )), + span: head, } } }, diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index f0726cef0f..0aa0453032 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -118,7 +118,7 @@ pub fn create_external_command( let env_vars_str = env_to_strings(engine_state, stack)?; fn value_as_spanned(value: Value) -> Result, ShellError> { - let span = value.span()?; + let span = value.span(); value .as_string() @@ -507,6 +507,7 @@ impl ExternalCommand { ); let _ = exit_code_tx.send(Value::Error { error: Box::new(ShellError::ExternalCommand { label: "core dumped".to_string(), help: format!("{cause}: child process '{commandname}' core dumped"), span: head }), + span: head, }); return Ok(()); } diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index bf0cc97d77..25d489a25e 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -297,7 +297,7 @@ fn handle_table_command( term_width, ) } - PipelineData::Value(Value::Error { error }, ..) => { + PipelineData::Value(Value::Error { error, .. }, ..) => { // Propagate this error outward, so that it goes to stderr // instead of stdout. Err(*error) diff --git a/crates/nu-command/tests/commands/length.rs b/crates/nu-command/tests/commands/length.rs index 9cf073a721..63318e65db 100644 --- a/crates/nu-command/tests/commands/length.rs +++ b/crates/nu-command/tests/commands/length.rs @@ -18,5 +18,5 @@ fn length_columns_no_rows() { fn length_fails_on_echo_record() { let actual = nu!("echo {a:1 b:2} | length"); - assert_eq!(actual.err.contains("only_supports_this_input_type"), true); + assert!(actual.err.contains("only_supports_this_input_type")); } diff --git a/crates/nu-engine/src/env.rs b/crates/nu-engine/src/env.rs index fa5c49a396..bc2265d6e4 100644 --- a/crates/nu-engine/src/env.rs +++ b/crates/nu-engine/src/env.rs @@ -18,6 +18,7 @@ const ENV_PATH_NAME: &str = "PATH"; const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS"; +#[allow(dead_code)] enum ConversionResult { Ok(Value), ConversionError(ShellError), // Failure during the conversion itself @@ -116,19 +117,19 @@ pub fn env_to_string( Ok(p) => Ok(p.to_string_lossy().to_string()), Err(_) => Err(ShellError::EnvVarNotAString { envvar_name: env_name.to_string(), - span: value.span()?, + span: value.span(), }), } } _ => Err(ShellError::EnvVarNotAString { envvar_name: env_name.to_string(), - span: value.span()?, + span: value.span(), }), } } else { Err(ShellError::EnvVarNotAString { envvar_name: env_name.to_string(), - span: value.span()?, + span: value.span(), }) } } @@ -167,7 +168,7 @@ pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result ConversionResult { if let Some(env_conversions) = stack.get_env_var(engine_state, ENV_CONVERSIONS) { - let env_span = match env_conversions.span() { - Ok(span) => span, - Err(e) => { - return ConversionResult::GeneralError(e); - } - }; - let val_span = match orig_val.span() { - Ok(span) => span, - Err(e) => { - return ConversionResult::GeneralError(e); - } - }; + let env_span = env_conversions.span(); + let val_span = orig_val.span(); let path_members = &[ PathMember::String { @@ -421,13 +412,7 @@ fn ensure_path(scope: &mut HashMap, env_path_name: &str) -> Optio } Some(val) => { // All other values are errors - let span = match val.span() { - Ok(sp) => sp, - Err(e) => { - error = error.or(Some(e)); - Span::unknown() // FIXME: any better span to use here? - } - }; + let span = val.span(); error = error.or_else(|| { Some(ShellError::GenericError( diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 66485c9266..bf2e4919cd 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -93,7 +93,7 @@ pub fn eval_call( } let span = if let Some(rest_item) = rest_items.first() { - rest_item.span()? + rest_item.span() } else { call.head }; @@ -284,7 +284,7 @@ pub fn eval_expression( span: expr.span, }), Expr::ValueWithUnit(e, unit) => match eval_expression(engine_state, stack, e)? { - Value::Int { val, .. } => Ok(compute(val, unit.item, unit.span)), + Value::Int { val, .. } => compute(val, unit.item, unit.span), x => Err(ShellError::CantConvert { to_type: "unit value".into(), from_type: x.get_type().to_string(), @@ -1155,6 +1155,7 @@ pub fn eval_block( input = PipelineData::Value( Value::Error { error: Box::new(error), + span: Span::unknown(), // FIXME: where does this span come from? }, None, ) @@ -1247,7 +1248,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result Result Result Result Result Result Result Result Value { +fn compute(size: i64, unit: Unit, span: Span) -> Result { unit.to_value(size, span) } @@ -1461,6 +1475,7 @@ fn collect_profiling_metadata( Ok((PipelineData::Empty, ..)) => Value::Nothing { span: element_span }, Err(err) => Value::Error { error: Box::new(err.clone()), + span: element_span, }, }; diff --git a/crates/nu-explore/src/explore.rs b/crates/nu-explore/src/explore.rs index 398da3bde0..23c40335f6 100644 --- a/crates/nu-explore/src/explore.rs +++ b/crates/nu-explore/src/explore.rs @@ -100,6 +100,7 @@ impl Command for Explore { Err(err) => Ok(PipelineData::Value( Value::Error { error: Box::new(err.into()), + span: call.head, }, None, )), diff --git a/crates/nu-explore/src/nu_common/command.rs b/crates/nu-explore/src/nu_common/command.rs index f3875e1b5b..53777d9b41 100644 --- a/crates/nu-explore/src/nu_common/command.rs +++ b/crates/nu-explore/src/nu_common/command.rs @@ -17,7 +17,7 @@ pub fn run_command_with_value( let pipeline = PipelineData::Value(input.clone(), None); let pipeline = run_nu_command(engine_state, stack, command, pipeline)?; - if let PipelineData::Value(Value::Error { error }, ..) = pipeline { + if let PipelineData::Value(Value::Error { error, .. }, ..) = pipeline { Err(ShellError::IOError(error.to_string())) } else { Ok(pipeline) diff --git a/crates/nu-explore/src/nu_common/value.rs b/crates/nu-explore/src/nu_common/value.rs index 1c880f3d89..12fb265955 100644 --- a/crates/nu-explore/src/nu_common/value.rs +++ b/crates/nu-explore/src/nu_common/value.rs @@ -56,6 +56,7 @@ fn collect_external_stream( let value = stdout.into_string().map_or_else( |error| Value::Error { error: Box::new(error), + span, }, |string| Value::string(string.item, span), ); @@ -67,6 +68,7 @@ fn collect_external_stream( let value = stderr.into_string().map_or_else( |error| Value::Error { error: Box::new(error), + span, }, |string| Value::string(string.item, span), ); diff --git a/crates/nu-parser/src/eval.rs b/crates/nu-parser/src/eval.rs index ae5495ecee..bed158d410 100644 --- a/crates/nu-parser/src/eval.rs +++ b/crates/nu-parser/src/eval.rs @@ -112,7 +112,13 @@ pub fn eval_constant( Expr::Nothing => Ok(Value::Nothing { span: expr.span }), Expr::ValueWithUnit(expr, unit) => { if let Ok(Value::Int { val, .. }) = eval_constant(working_set, expr) { - Ok(unit.item.to_value(val, unit.span)) + unit.item.to_value(val, unit.span).map_err(|_| { + ParseError::InvalidLiteral( + "literal can not fit in unit".into(), + "literal can not fit in unit".into(), + unit.span, + ) + }) } else { Err(ParseError::NotAConstant(expr.span)) } diff --git a/crates/nu-protocol/src/config.rs b/crates/nu-protocol/src/config.rs index df1618307c..4c09062a4a 100644 --- a/crates/nu-protocol/src/config.rs +++ b/crates/nu-protocol/src/config.rs @@ -355,14 +355,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -388,14 +388,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -421,14 +421,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -498,14 +498,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -643,7 +643,7 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{key2}.{x} is an unknown config setting" ); } @@ -660,14 +660,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct record vals[index] = Value::record( record! { @@ -852,14 +852,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -1022,14 +1022,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -1068,14 +1068,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -1090,7 +1090,7 @@ impl Value { if let Ok(map) = create_map(value) { config.explore = map; } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( config @@ -1107,7 +1107,7 @@ impl Value { if let Ok(map) = create_map(value) { config.color_config = map; } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( config @@ -1304,14 +1304,14 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{key}.{x} is an unknown config setting" ); } } } } else { - invalid!(vals[index].span().ok(), "should be a record"); + invalid!(Some(vals[index].span()), "should be a record"); // Reconstruct vals[index] = Value::record( record! { @@ -1328,7 +1328,7 @@ impl Value { cols, vals, index, - value.span().ok(), + Some(value.span()), "$env.config.{x} is an unknown config setting" ); } @@ -1340,7 +1340,7 @@ impl Value { Some(ShellError::GenericError( "Error while applying config changes".into(), "$env.config is not a record".into(), - self.span().ok(), + Some(self.span()), None, vec![], )), @@ -1377,7 +1377,7 @@ fn try_parse_trim_strategy( ShellError::GenericError( "Error while applying config changes".into(), "$env.config.table.trim is not a record".into(), - value.span().ok(), + Some(value.span()), Some("Please consult the documentation for configuring Nushell.".into()), vec![e], ) @@ -1392,7 +1392,7 @@ fn try_parse_trim_strategy( errors.push(ShellError::GenericError( "Error while applying config changes".into(), "$env.config.table.trim.methodology was not provided".into(), - value.span().ok(), + Some(value.span()), Some("Please consult the documentation for configuring Nushell.".into()), vec![], )); @@ -1409,7 +1409,7 @@ fn try_parse_trim_strategy( errors.push(ShellError::GenericError( "Error while applying config changes".into(), "$env.config.table.trim.wrapping_try_keep_words is not a bool".into(), - value.span().ok(), + Some(value.span()), Some("Please consult the documentation for configuring Nushell.".into()), vec![], )); @@ -1424,7 +1424,7 @@ fn try_parse_trim_strategy( errors.push(ShellError::GenericError( "Error while applying config changes".into(), "$env.config.table.trim.truncating_suffix is not a string".into(), - value.span().ok(), + Some(value.span()), Some("Please consult the documentation for configuring Nushell.".into()), vec![], )); @@ -1491,7 +1491,7 @@ fn create_hooks(value: &Value) -> Result { v => Err(ShellError::UnsupportedConfigValue( "record for 'hooks' config".into(), "non-record value".into(), - v.span().unwrap_or_else(|_| Span::unknown()), + v.span(), )), } } diff --git a/crates/nu-protocol/src/engine/pattern_match.rs b/crates/nu-protocol/src/engine/pattern_match.rs index aef44d18c4..307a6b2fb5 100644 --- a/crates/nu-protocol/src/engine/pattern_match.rs +++ b/crates/nu-protocol/src/engine/pattern_match.rs @@ -148,7 +148,10 @@ impl Matcher for Pattern { let span = unit.span; if let Expr::Int(size) = amount.expr { - &unit.item.to_value(size, span) == value + match &unit.item.to_value(size, span) { + Ok(v) => v == value, + _ => false, + } } else { false } diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 3e47d2efca..e127475e51 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -123,7 +123,7 @@ impl PipelineData { match self { PipelineData::ListStream(..) => None, PipelineData::ExternalStream { span, .. } => Some(*span), - PipelineData::Value(v, _) => v.span().ok(), + PipelineData::Value(v, _) => Some(v.span()), PipelineData::Empty => None, } } @@ -162,7 +162,10 @@ impl PipelineData { items.push(val); } Err(e) => { - return Value::Error { error: Box::new(e) }; + return Value::Error { + error: Box::new(e), + span, + }; } } } @@ -184,6 +187,7 @@ impl PipelineData { Err(err) => { return Value::Error { error: Box::new(err), + span, }; } } @@ -201,6 +205,7 @@ impl PipelineData { Err(err) => { return Value::Error { error: Box::new(err), + span, }; } } @@ -219,7 +224,7 @@ impl PipelineData { pub fn drain(self) -> Result<(), ShellError> { match self { - PipelineData::Value(Value::Error { error }, _) => Err(*error), + PipelineData::Value(Value::Error { error, .. }, _) => Err(*error), PipelineData::Value(_, _) => Ok(()), PipelineData::ListStream(stream, _) => stream.drain(), PipelineData::ExternalStream { stdout, stderr, .. } => { @@ -239,7 +244,7 @@ impl PipelineData { pub fn drain_with_exit_code(self) -> Result { match self { - PipelineData::Value(Value::Error { error }, _) => Err(*error), + PipelineData::Value(Value::Error { error, .. }, _) => Err(*error), PipelineData::Value(_, _) => Ok(0), PipelineData::ListStream(stream, _) => { stream.drain()?; @@ -301,12 +306,12 @@ impl PipelineData { Err(error) => Err(error), }, // Propagate errors by explicitly matching them before the final case. - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), other => Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary, raw data or range".into(), wrong_type: other.get_type().to_string(), dst_span: span, - src_span: other.expect_span(), + src_span: other.span(), }), }, PipelineData::Empty => Err(ShellError::OnlySupportsThisInputType { @@ -366,7 +371,7 @@ impl PipelineData { PipelineData::Value(Value::String { val, span }, metadata) => Ok((val, span, metadata)), PipelineData::Value(val, _) => Err(ShellError::TypeMismatch { err_message: "string".into(), - span: val.span()?, + span: val.span(), }), PipelineData::ListStream(_, _) => Err(ShellError::TypeMismatch { err_message: "string".into(), @@ -470,7 +475,7 @@ impl PipelineData { .map(f) .into_pipeline_data(ctrlc)), PipelineData::Value(v, ..) => match f(v) { - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), v => Ok(v.into_pipeline_data()), }, } @@ -589,7 +594,7 @@ impl PipelineData { if f(&v) { Ok(v.into_pipeline_data()) } else { - Ok(Value::Nothing { span: v.span()? }.into_pipeline_data()) + Ok(Value::Nothing { span: v.span() }.into_pipeline_data()) } } } @@ -807,7 +812,7 @@ impl PipelineData { ) -> Result { for item in self { let mut is_err = false; - let mut out = if let Value::Error { error } = item { + let mut out = if let Value::Error { error, .. } = item { let working_set = StateWorkingSet::new(engine_state); // Value::Errors must always go to stderr, not stdout. is_err = true; @@ -851,7 +856,7 @@ impl IntoIterator for PipelineData { metadata, )) } - PipelineData::Value(Value::Range { val, .. }, metadata) => { + PipelineData::Value(Value::Range { val, span }, metadata) => { match val.into_range_iter(None) { Ok(iter) => PipelineIterator(PipelineData::ListStream( ListStream { @@ -864,6 +869,7 @@ impl IntoIterator for PipelineData { ListStream { stream: Box::new(std::iter::once(Value::Error { error: Box::new(error), + span, })), ctrlc: None, }, @@ -914,7 +920,7 @@ fn drain_exit_code(exit_code: ListStream) -> Result { let mut exit_codes: Vec<_> = exit_code.into_iter().collect(); match exit_codes.pop() { #[cfg(unix)] - Some(Value::Error { error }) => Err(*error), + Some(Value::Error { error, .. }) => Err(*error), Some(Value::Int { val, .. }) => Ok(val), _ => Ok(0), } @@ -937,6 +943,7 @@ impl Iterator for PipelineIterator { Ok(x) => x, Err(err) => Value::Error { error: Box::new(err), + span: Span::unknown(), //FIXME: unclear where this span should come from }, }), } diff --git a/crates/nu-protocol/src/value/from.rs b/crates/nu-protocol/src/value/from.rs index a8b994e828..9519f58156 100644 --- a/crates/nu-protocol/src/value/from.rs +++ b/crates/nu-protocol/src/value/from.rs @@ -7,7 +7,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "f64".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -21,7 +21,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "i64".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } diff --git a/crates/nu-protocol/src/value/from_value.rs b/crates/nu-protocol/src/value/from_value.rs index 48a08d1195..6759efb399 100644 --- a/crates/nu-protocol/src/value/from_value.rs +++ b/crates/nu-protocol/src/value/from_value.rs @@ -36,7 +36,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "integer".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -53,7 +53,7 @@ impl FromValue for i64 { v => Err(ShellError::CantConvert { to_type: "integer".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -75,7 +75,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "float".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -90,7 +90,7 @@ impl FromValue for f64 { v => Err(ShellError::CantConvert { to_type: "float".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -134,7 +134,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "non-negative integer".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -169,7 +169,7 @@ impl FromValue for usize { v => Err(ShellError::CantConvert { to_type: "non-negative integer".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -185,7 +185,7 @@ impl FromValue for String { v => Err(ShellError::CantConvert { to_type: "string".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -202,12 +202,12 @@ impl FromValue for Spanned { return Err(ShellError::CantConvert { to_type: "string".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }) } }, - span: v.span()?, + span: v.span(), }) } } @@ -223,7 +223,7 @@ impl FromValue for Vec { c => Err(ShellError::CantConvert { to_type: "string".into(), from_type: c.get_type().to_string(), - span: c.span()?, + span: c.span(), help: None, }), }) @@ -231,7 +231,7 @@ impl FromValue for Vec { v => Err(ShellError::CantConvert { to_type: "string".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -252,7 +252,7 @@ impl FromValue for Vec> { c => Err(ShellError::CantConvert { to_type: "string".into(), from_type: c.get_type().to_string(), - span: c.span()?, + span: c.span(), help: None, }), }) @@ -260,7 +260,7 @@ impl FromValue for Vec> { v => Err(ShellError::CantConvert { to_type: "string".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -277,7 +277,7 @@ impl FromValue for Vec { c => Err(ShellError::CantConvert { to_type: "bool".into(), from_type: c.get_type().to_string(), - span: c.span()?, + span: c.span(), help: None, }), }) @@ -285,7 +285,7 @@ impl FromValue for Vec { v => Err(ShellError::CantConvert { to_type: "bool".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -294,7 +294,7 @@ impl FromValue for Vec { impl FromValue for CellPath { fn from_value(v: &Value) -> Result { - let span = v.span()?; + let span = v.span(); match v { Value::CellPath { val, .. } => Ok(val.clone()), Value::String { val, .. } => Ok(CellPath { @@ -334,7 +334,7 @@ impl FromValue for bool { v => Err(ShellError::CantConvert { to_type: "bool".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -351,7 +351,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "bool".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -365,7 +365,7 @@ impl FromValue for DateTime { v => Err(ShellError::CantConvert { to_type: "date".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -382,7 +382,7 @@ impl FromValue for Spanned> { v => Err(ShellError::CantConvert { to_type: "date".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -396,7 +396,7 @@ impl FromValue for Range { v => Err(ShellError::CantConvert { to_type: "range".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -413,7 +413,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "range".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -428,7 +428,7 @@ impl FromValue for Vec { v => Err(ShellError::CantConvert { to_type: "binary data".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -449,7 +449,7 @@ impl FromValue for Spanned> { v => Err(ShellError::CantConvert { to_type: "binary data".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -467,7 +467,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "range".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -482,7 +482,7 @@ impl FromValue for Vec { v => Err(ShellError::CantConvert { to_type: "Vector of values".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -496,7 +496,7 @@ impl FromValue for Record { v => Err(ShellError::CantConvert { to_type: "Record".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -517,7 +517,7 @@ impl FromValue for Closure { v => Err(ShellError::CantConvert { to_type: "Closure".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -531,7 +531,7 @@ impl FromValue for Block { v => Err(ShellError::CantConvert { to_type: "Block".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -555,7 +555,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "Closure".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -572,7 +572,7 @@ impl FromValue for Spanned { v => Err(ShellError::CantConvert { to_type: "Match pattern".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } @@ -586,7 +586,7 @@ impl FromValue for MatchPattern { v => Err(ShellError::CantConvert { to_type: "Match pattern".into(), from_type: v.get_type().to_string(), - span: v.span()?, + span: v.span(), help: None, }), } diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 3db34049c2..5ffd13ce89 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -96,6 +96,7 @@ pub enum Value { }, Error { error: Box, + span: Span, }, Binary { val: Vec, @@ -170,8 +171,9 @@ impl Clone for Value { span: *span, }, Value::Nothing { span } => Value::Nothing { span: *span }, - Value::Error { error } => Value::Error { + Value::Error { error, span } => Value::Error { error: error.clone(), + span: *span, }, Value::Binary { val, span } => Value::Binary { val: val.clone(), @@ -197,7 +199,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "boolean".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -209,7 +211,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "integer".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -222,7 +224,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "float".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -234,7 +236,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "filesize".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -246,7 +248,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "duration".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -258,7 +260,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "date".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -270,7 +272,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "range".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -288,7 +290,7 @@ impl Value { return Err(ShellError::CantConvert { to_type: "string".into(), from_type: "binary".into(), - span: self.span()?, + span: self.span(), help: None, }); } @@ -297,7 +299,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "string".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -318,7 +320,7 @@ impl Value { return Err(ShellError::CantConvert { to_type: "string".into(), from_type: "binary".into(), - span: self.span()?, + span: self.span(), help: None, }) } @@ -326,7 +328,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "string".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -347,7 +349,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "char".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -359,7 +361,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "path".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -371,7 +373,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "record".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -383,7 +385,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "list".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -396,7 +398,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "block".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -408,7 +410,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "closure".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -421,7 +423,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "binary".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -433,7 +435,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "cell path".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -445,7 +447,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "custom value".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -457,7 +459,7 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "lazy record".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } @@ -469,14 +471,14 @@ impl Value { x => Err(ShellError::CantConvert { to_type: "match pattern".into(), from_type: x.get_type().to_string(), - span: self.span()?, + span: self.span(), help: None, }), } } /// Get the span for the current value - pub fn span(&self) -> Result { + pub fn span(&self) -> Span { match self { Value::Bool { span, .. } | Value::Int { span, .. } @@ -495,18 +497,11 @@ impl Value { | Value::CellPath { span, .. } | Value::CustomValue { span, .. } | Value::LazyRecord { span, .. } - | Value::MatchPattern { span, .. } => Ok(*span), - Value::Error { error } => Err(*error.clone()), + | Value::MatchPattern { span, .. } + | Value::Error { span, .. } => *span, } } - /// Special variant of the above designed to be called only in - /// situations where the value not being a Value::Error has been guaranteed - /// by match arms. - pub fn expect_span(&self) -> Span { - self.span().expect("non-Error Value had no span") - } - /// Update the value with a new span pub fn with_span(mut self, new_span: Span) -> Value { match &mut self { @@ -625,7 +620,7 @@ impl Value { separator: &str, config: &Config, ) -> Result { - if let Value::Error { error } = self { + if let Value::Error { error, .. } = self { Err(*error.to_owned()) } else { Ok(self.into_string(separator, config)) @@ -677,11 +672,12 @@ impl Value { .collect::>() .join(separator) ), - Value::LazyRecord { val, .. } => { + Value::LazyRecord { val, span } => { let collected = match val.collect() { Ok(val) => val, Err(error) => Value::Error { error: Box::new(error), + span: *span, }, }; collected.into_string(separator, config) @@ -689,7 +685,7 @@ impl Value { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), @@ -744,7 +740,7 @@ impl Value { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), @@ -844,7 +840,7 @@ impl Value { Value::Block { val, .. } => format!(""), Value::Closure { val, .. } => format!(""), Value::Nothing { .. } => String::new(), - Value::Error { error } => format!("{error:?}"), + Value::Error { error, .. } => format!("{error:?}"), Value::Binary { val, .. } => format!("{val:?}"), Value::CellPath { val, .. } => val.into_string(), Value::CustomValue { val, .. } => val.value_string(), @@ -868,6 +864,10 @@ impl Value { matches!(self, Value::Nothing { .. }) } + pub fn is_error(&self) -> bool { + matches!(self, Value::Error { .. }) + } + /// Follow a given cell path into the value: for example accessing select elements in a stream or list pub fn follow_cell_path( self, @@ -961,7 +961,7 @@ impl Value { span: *origin_span, }) } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), x => { return Err(ShellError::IncompatiblePathAccess { type_name:format!("{}",x.get_type()), span: *origin_span }) } @@ -1041,7 +1041,7 @@ impl Value { return Err(ShellError::CantFindColumn { col_name: column_name.to_string(), span: *origin_span, - src_span: val.span().unwrap_or(*span), + src_span: val.span(), }); } } else if *optional && matches!(val, Value::Nothing { .. }) { @@ -1050,7 +1050,7 @@ impl Value { return Err(ShellError::CantFindColumn { col_name: column_name.to_string(), span: *origin_span, - src_span: val.span().unwrap_or(*span), + src_span: val.span(), }); } } @@ -1066,7 +1066,7 @@ impl Value { Value::Nothing { .. } if *optional => { return Ok(Value::nothing(*origin_span)); // short-circuit } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), x => { return Err(ShellError::IncompatiblePathAccess { type_name: format!("{}", x.get_type()), @@ -1078,7 +1078,7 @@ impl Value { } // If a single Value::Error was produced by the above (which won't happen if nullify_errors is true), unwrap it now. // Note that Value::Errors inside Lists remain as they are, so that the rest of the list can still potentially be used. - if let Value::Error { error } = current { + if let Value::Error { error, .. } = current { Err(*error) } else { Ok(current) @@ -1096,7 +1096,7 @@ impl Value { let new_val = callback(&orig.follow_cell_path(cell_path, false)?); match new_val { - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), new_val => self.upsert_data_at_cell_path(cell_path, new_val), } } @@ -1133,7 +1133,7 @@ impl Value { break; } else { let mut new_col = - Value::record(Record::new(), new_val.span()?); + Value::record(Record::new(), new_val.span()); new_col.upsert_data_at_cell_path( &cell_path[1..], new_val, @@ -1143,12 +1143,12 @@ impl Value { } } } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } } @@ -1167,7 +1167,7 @@ impl Value { let new_col = if cell_path.len() == 1 { new_val } else { - let mut new_col = Value::record(Record::new(), new_val.span()?); + let mut new_col = Value::record(Record::new(), new_val.span()); new_col.upsert_data_at_cell_path(&cell_path[1..], new_val)?; new_col }; @@ -1181,12 +1181,12 @@ impl Value { record.upsert_data_at_cell_path(cell_path, new_val)?; *self = record } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } }, @@ -1207,11 +1207,11 @@ impl Value { }); } } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::NotAList { dst_span: *span, - src_span: v.span()?, + src_span: v.span(), }) } }, @@ -1234,7 +1234,7 @@ impl Value { let new_val = callback(&orig.follow_cell_path(cell_path, false)?); match new_val { - Value::Error { error } => Err(*error), + Value::Error { error, .. } => Err(*error), new_val => self.update_data_at_cell_path(cell_path, new_val), } @@ -1277,12 +1277,12 @@ impl Value { }); } } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } } @@ -1314,12 +1314,12 @@ impl Value { record.update_data_at_cell_path(cell_path, new_val)?; *self = record } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } }, @@ -1338,11 +1338,11 @@ impl Value { }); } } - Value::Error { error } => return Err(*error.to_owned()), + Value::Error { error, .. } => return Err(*error.to_owned()), v => { return Err(ShellError::NotAList { dst_span: *span, - src_span: v.span()?, + src_span: v.span(), }) } }, @@ -1396,7 +1396,7 @@ impl Value { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } } @@ -1438,7 +1438,7 @@ impl Value { v => Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }), }, PathMember::Int { @@ -1463,7 +1463,7 @@ impl Value { } v => Err(ShellError::NotAList { dst_span: *span, - src_span: v.span()?, + src_span: v.span(), }), }, } @@ -1502,7 +1502,7 @@ impl Value { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }) } } @@ -1540,7 +1540,7 @@ impl Value { v => Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, - src_span: v.span()?, + src_span: v.span(), }), }, PathMember::Int { @@ -1564,7 +1564,7 @@ impl Value { } v => Err(ShellError::NotAList { dst_span: *span, - src_span: v.span()?, + src_span: v.span(), }), }, } @@ -1613,7 +1613,7 @@ impl Value { record.push(col_name, new_val.clone()); } // SIGH... - Value::Error { error } => return Err(*error.clone()), + Value::Error { error, .. } => return Err(*error.clone()), _ => { return Err(ShellError::UnsupportedInput( "expected table or record".into(), @@ -1684,7 +1684,7 @@ impl Value { v => { return Err(ShellError::NotAList { dst_span: *span, - src_span: v.span()?, + src_span: v.span(), }) } }, @@ -1774,9 +1774,10 @@ impl Value { Value::Nothing { span } } - pub fn error(error: ShellError) -> Value { + pub fn error(error: ShellError, span: Span) -> Value { Value::Error { error: Box::new(error), + span, } } @@ -2399,9 +2400,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2436,9 +2437,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2517,9 +2518,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2627,9 +2628,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2764,9 +2765,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2900,9 +2901,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -2923,9 +2924,9 @@ impl Value { return Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }); } @@ -2938,9 +2939,9 @@ impl Value { Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }) } } @@ -2966,9 +2967,9 @@ impl Value { return Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }); } @@ -2980,9 +2981,9 @@ impl Value { .ok_or(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }) } @@ -3007,9 +3008,9 @@ impl Value { return Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }); } @@ -3021,9 +3022,9 @@ impl Value { .ok_or(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }) } @@ -3048,9 +3049,9 @@ impl Value { return Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }); } @@ -3062,9 +3063,9 @@ impl Value { None => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3087,9 +3088,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3113,9 +3114,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3171,9 +3172,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3228,9 +3229,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3304,9 +3305,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3323,9 +3324,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3342,9 +3343,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3361,9 +3362,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3380,9 +3381,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3399,9 +3400,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3418,9 +3419,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3437,9 +3438,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3493,9 +3494,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3512,9 +3513,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3531,9 +3532,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3550,9 +3551,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } @@ -3585,9 +3586,9 @@ impl Value { _ => Err(ShellError::OperatorMismatch { op_span: op, lhs_ty: self.get_type().to_string(), - lhs_span: self.span()?, + lhs_span: self.span(), rhs_ty: rhs.get_type().to_string(), - rhs_span: rhs.span()?, + rhs_span: rhs.span(), }), } } diff --git a/crates/nu-protocol/src/value/range.rs b/crates/nu-protocol/src/value/range.rs index 0f4526722e..e09f4f1b0a 100644 --- a/crates/nu-protocol/src/value/range.rs +++ b/crates/nu-protocol/src/value/range.rs @@ -130,7 +130,7 @@ impl Range { self, ctrlc: Option>, ) -> Result { - let span = self.from.span()?; + let span = self.from.span(); Ok(RangeIterator::new(self, ctrlc, span)) } @@ -217,6 +217,7 @@ impl Iterator for RangeIterator { self.done = true; return Some(Value::Error { error: Box::new(ShellError::CannotCreateRange { span: self.span }), + span: self.span, }); }; @@ -237,6 +238,7 @@ impl Iterator for RangeIterator { self.done = true; return Some(Value::Error { error: Box::new(error), + span: self.span, }); } }; diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index 47bdfb750b..b812ef908f 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -76,7 +76,7 @@ impl RawStream { for next in self { match next { Ok(val) => { - if let Value::Error { error } = val { + if let Value::Error { error, .. } = val { return Err(*error); } } @@ -213,7 +213,7 @@ impl ListStream { pub fn drain(self) -> Result<(), ShellError> { for next in self { - if let Value::Error { error } = next { + if let Value::Error { error, .. } = next { return Err(*error); } } diff --git a/crates/nu-protocol/src/value/unit.rs b/crates/nu-protocol/src/value/unit.rs index 31c2c48ab4..6587d9b8e1 100644 --- a/crates/nu-protocol/src/value/unit.rs +++ b/crates/nu-protocol/src/value/unit.rs @@ -32,119 +32,111 @@ pub enum Unit { } impl Unit { - pub fn to_value(&self, size: i64, span: Span) -> Value { + pub fn to_value(&self, size: i64, span: Span) -> Result { match self { - Unit::Byte => Value::Filesize { val: size, span }, - Unit::Kilobyte => Value::Filesize { + Unit::Byte => Ok(Value::Filesize { val: size, span }), + Unit::Kilobyte => Ok(Value::Filesize { val: size * 1000, span, - }, - Unit::Megabyte => Value::Filesize { + }), + Unit::Megabyte => Ok(Value::Filesize { val: size * 1000 * 1000, span, - }, - Unit::Gigabyte => Value::Filesize { + }), + Unit::Gigabyte => Ok(Value::Filesize { val: size * 1000 * 1000 * 1000, span, - }, - Unit::Terabyte => Value::Filesize { + }), + Unit::Terabyte => Ok(Value::Filesize { val: size * 1000 * 1000 * 1000 * 1000, span, - }, - Unit::Petabyte => Value::Filesize { + }), + Unit::Petabyte => Ok(Value::Filesize { val: size * 1000 * 1000 * 1000 * 1000 * 1000, span, - }, - Unit::Exabyte => Value::Filesize { + }), + Unit::Exabyte => Ok(Value::Filesize { val: size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, span, - }, + }), - Unit::Kibibyte => Value::Filesize { + Unit::Kibibyte => Ok(Value::Filesize { val: size * 1024, span, - }, - Unit::Mebibyte => Value::Filesize { + }), + Unit::Mebibyte => Ok(Value::Filesize { val: size * 1024 * 1024, span, - }, - Unit::Gibibyte => Value::Filesize { + }), + Unit::Gibibyte => Ok(Value::Filesize { val: size * 1024 * 1024 * 1024, span, - }, - Unit::Tebibyte => Value::Filesize { + }), + Unit::Tebibyte => Ok(Value::Filesize { val: size * 1024 * 1024 * 1024 * 1024, span, - }, - Unit::Pebibyte => Value::Filesize { + }), + Unit::Pebibyte => Ok(Value::Filesize { val: size * 1024 * 1024 * 1024 * 1024 * 1024, span, - }, - Unit::Exbibyte => Value::Filesize { + }), + Unit::Exbibyte => Ok(Value::Filesize { val: size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, span, - }, + }), - Unit::Nanosecond => Value::Duration { val: size, span }, - Unit::Microsecond => Value::Duration { + Unit::Nanosecond => Ok(Value::Duration { val: size, span }), + Unit::Microsecond => Ok(Value::Duration { val: size * 1000, span, - }, - Unit::Millisecond => Value::Duration { + }), + Unit::Millisecond => Ok(Value::Duration { val: size * 1000 * 1000, span, - }, - Unit::Second => Value::Duration { + }), + Unit::Second => Ok(Value::Duration { val: size * 1000 * 1000 * 1000, span, - }, + }), Unit::Minute => match size.checked_mul(1000 * 1000 * 1000 * 60) { - Some(val) => Value::Duration { val, span }, - None => Value::Error { - error: Box::new(ShellError::GenericError( - "duration too large".into(), - "duration too large".into(), - Some(span), - None, - Vec::new(), - )), - }, + Some(val) => Ok(Value::Duration { val, span }), + None => Err(ShellError::GenericError( + "duration too large".into(), + "duration too large".into(), + Some(span), + None, + Vec::new(), + )), }, Unit::Hour => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60) { - Some(val) => Value::Duration { val, span }, - None => Value::Error { - error: Box::new(ShellError::GenericError( - "duration too large".into(), - "duration too large".into(), - Some(span), - None, - Vec::new(), - )), - }, + Some(val) => Ok(Value::Duration { val, span }), + None => Err(ShellError::GenericError( + "duration too large".into(), + "duration too large".into(), + Some(span), + None, + Vec::new(), + )), }, Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) { - Some(val) => Value::Duration { val, span }, - None => Value::Error { - error: Box::new(ShellError::GenericError( - "duration too large".into(), - "duration too large".into(), - Some(span), - None, - Vec::new(), - )), - }, + Some(val) => Ok(Value::Duration { val, span }), + None => Err(ShellError::GenericError( + "duration too large".into(), + "duration too large".into(), + Some(span), + None, + Vec::new(), + )), }, Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) { - Some(val) => Value::Duration { val, span }, - None => Value::Error { - error: Box::new(ShellError::GenericError( - "duration too large".into(), - "duration too large".into(), - Some(span), - None, - Vec::new(), - )), - }, + Some(val) => Ok(Value::Duration { val, span }), + None => Err(ShellError::GenericError( + "duration too large".into(), + "duration too large".into(), + Some(span), + None, + Vec::new(), + )), }, } } diff --git a/crates/nu-protocol/tests/test_value.rs b/crates/nu-protocol/tests/test_value.rs index 28faa4b1b3..91a72b05e1 100644 --- a/crates/nu-protocol/tests/test_value.rs +++ b/crates/nu-protocol/tests/test_value.rs @@ -38,7 +38,7 @@ fn test_comparison_nothing() { #[rstest] #[case(365 * 24 * 3600 * 1_000_000_000, "52wk 1day")] -#[case( ((((((((1 * 7) + 2) * 24 + 3) * 60 + 4) * 60) + 5) * 1000 + 6) * 1000 + 7) * 1000 + 8, +#[case( (((((((7 + 2) * 24 + 3) * 60 + 4) * 60) + 5) * 1000 + 6) * 1000 + 7) * 1000 + 8, "1wk 2day 3hr 4min 5sec 6ms 7µs 8ns")] fn test_duration_to_string(#[case] in_ns: i64, #[case] expected: &str) { let dur = Value::test_duration(in_ns); diff --git a/crates/nu-table/src/types/collapse.rs b/crates/nu-table/src/types/collapse.rs index 2ab2eadd7f..9682460130 100644 --- a/crates/nu-table/src/types/collapse.rs +++ b/crates/nu-table/src/types/collapse.rs @@ -1,5 +1,5 @@ use nu_color_config::StyleComputer; -use nu_protocol::{Config, Span, Value}; +use nu_protocol::{Config, Value}; use crate::UnstructuredTable; @@ -61,7 +61,7 @@ fn colorize_value(value: &mut Value, config: &Config, style_computer: &StyleComp let (text, style) = nu_value_to_string_clean(value, config, style_computer); if let Some(color) = style.color_style { let text = color.paint(text).to_string(); - let span = value.span().unwrap_or(Span::unknown()); + let span = value.span(); *value = Value::string(text, span); } } diff --git a/crates/nu-table/src/types/expanded.rs b/crates/nu-table/src/types/expanded.rs index 597b42ef03..5916ef1b30 100644 --- a/crates/nu-table/src/types/expanded.rs +++ b/crates/nu-table/src/types/expanded.rs @@ -110,7 +110,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult { return Ok(None); } - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { return Err(*error.clone()); } @@ -150,7 +150,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult { return Ok(None); } - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { return Err(*error.clone()); } @@ -238,7 +238,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult { return Ok(None); } - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { return Err(*error.clone()); } diff --git a/crates/nu-table/src/types/general.rs b/crates/nu-table/src/types/general.rs index 7f9195f6aa..6114e03bbb 100644 --- a/crates/nu-table/src/types/general.rs +++ b/crates/nu-table/src/types/general.rs @@ -130,7 +130,7 @@ fn to_table_with_header( return Ok(None); } - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { return Err(*error.clone()); } @@ -165,7 +165,7 @@ fn to_table_with_no_header( return Ok(None); } - if let Value::Error { error } = item { + if let Value::Error { error, .. } = item { return Err(*error.clone()); } diff --git a/crates/nu_plugin_custom_values/src/cool_custom_value.rs b/crates/nu_plugin_custom_values/src/cool_custom_value.rs index 900b693388..757e6c0b84 100644 --- a/crates/nu_plugin_custom_values/src/cool_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/cool_custom_value.rs @@ -37,7 +37,7 @@ impl CoolCustomValue { x => Err(ShellError::CantConvert { to_type: "cool".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu_plugin_custom_values/src/second_custom_value.rs b/crates/nu_plugin_custom_values/src/second_custom_value.rs index 617e78f3ab..48eff6d0e3 100644 --- a/crates/nu_plugin_custom_values/src/second_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/second_custom_value.rs @@ -34,7 +34,7 @@ impl SecondCustomValue { x => Err(ShellError::CantConvert { to_type: "cool".into(), from_type: x.get_type().to_string(), - span: x.span()?, + span: x.span(), help: None, }), } diff --git a/crates/nu_plugin_formats/src/from/ics.rs b/crates/nu_plugin_formats/src/from/ics.rs index 97babe222f..fef400c3b5 100644 --- a/crates/nu_plugin_formats/src/from/ics.rs +++ b/crates/nu_plugin_formats/src/from/ics.rs @@ -8,7 +8,7 @@ use std::io::BufReader; pub const CMD_NAME: &str = "from ics"; pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result { - let span = input.span().unwrap_or(call.head); + let span = input.span(); let input_string = input.as_string()?; let head = call.head; @@ -34,6 +34,7 @@ pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result Result { - let span = input.span().unwrap_or(call.head); + let span = input.span(); let input_string = input.as_string()?; let head = call.head; diff --git a/crates/nu_plugin_formats/src/from/vcf.rs b/crates/nu_plugin_formats/src/from/vcf.rs index e8ddcec7ee..8f3bdb6b33 100644 --- a/crates/nu_plugin_formats/src/from/vcf.rs +++ b/crates/nu_plugin_formats/src/from/vcf.rs @@ -7,7 +7,7 @@ use nu_protocol::{record, PluginExample, Record, ShellError, Span, Value}; pub const CMD_NAME: &str = "from vcf"; pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result { - let span = input.span().unwrap_or(call.head); + let span = input.span(); let input_string = input.as_string()?; let head = call.head; @@ -30,6 +30,7 @@ pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result Result Err(LabeledError { label: "requires text input".to_string(), msg: "Expected text from pipeline".to_string(), - span: Some(input.span()?), + span: Some(input.span()), }), } }