From e82ffc4deec90005e373a6dcd32cf98ed95d4971 Mon Sep 17 00:00:00 2001 From: Michael Angerman <1809991+stormasm@users.noreply.github.com> Date: Sat, 12 Feb 2022 21:30:57 -0800 Subject: [PATCH] sort_by error processing return ShellError instead of static String (#4453) * sort_by error processing part I return ShellError instead of static Strings * more explicit details on what types are failing * clippy fixes --- crates/nu-command/src/filters/sort_by.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/src/filters/sort_by.rs b/crates/nu-command/src/filters/sort_by.rs index 045221b69..edf008955 100644 --- a/crates/nu-command/src/filters/sort_by.rs +++ b/crates/nu-command/src/filters/sort_by.rs @@ -197,11 +197,13 @@ pub fn sort( b.into_string("", config).to_ascii_lowercase(), Span::test_data(), ); - coerce_compare(&lowercase_left, &lowercase_right) + coerce_compare(&lowercase_left, &lowercase_right, call) .expect("sort_by default bug") .compare() } else { - coerce_compare(a, b).expect("sort_by default bug").compare() + coerce_compare(a, b, call) + .expect("sort_by default bug") + .compare() } }); } @@ -216,7 +218,7 @@ pub fn process( call: &Call, insensitive: bool, config: &Config, -) -> Result { +) -> Result { let left_value = left.get_data_by_key(column); let left_res = match left_value { @@ -240,9 +242,9 @@ pub fn process( right_res.into_string("", config).to_ascii_lowercase(), Span::test_data(), ); - coerce_compare(&lowercase_left, &lowercase_right) + coerce_compare(&lowercase_left, &lowercase_right, call) } else { - coerce_compare(&left_res, &right_res) + coerce_compare(&left_res, &right_res, call) } } @@ -281,7 +283,8 @@ pub fn process_floats(left: &f64, right: &f64) -> std::cmp::Ordering { pub fn coerce_compare( left: &Value, right: &Value, -) -> Result { + call: &Call, +) -> Result { match (left, right) { (Value::Float { val: left, .. }, Value::Float { val: right, .. }) => { Ok(CompareValues::Floats(*left, *right)) @@ -301,7 +304,11 @@ pub fn coerce_compare( (Value::Bool { val: left, .. }, Value::Bool { val: right, .. }) => { Ok(CompareValues::Booleans(*left, *right)) } - _ => Err(("coerce_compare_left", "coerce_compare_right")), + + _ => { + let description = format!("not able to compare {:?} with {:?}\n", left, right); + Err(ShellError::TypeMismatch(description, call.head)) + } } }