From b09acdb7f98ec9694cfb223222577bc02ebba4a4 Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Mon, 28 Feb 2022 23:14:33 +0800 Subject: [PATCH] Fix unsupported type message for some math related commands (#4672) * Fix unsupported type message of some math related commands * changing the error form for UnsupportedInput * cargo fmt --- crates/nu-command/src/env/load_env.rs | 5 ++++- crates/nu-command/src/filesystem/save.rs | 10 ++++++++-- crates/nu-command/src/formats/to/nuon.rs | 25 +++++++++++++++++++----- crates/nu-command/src/math/abs.rs | 7 +++++-- crates/nu-command/src/math/ceil.rs | 5 ++++- crates/nu-command/src/math/floor.rs | 5 ++++- crates/nu-command/src/math/round.rs | 5 ++++- crates/nu-command/src/math/sqrt.rs | 5 ++++- crates/nu-protocol/src/shell_error.rs | 2 +- 9 files changed, 54 insertions(+), 15 deletions(-) diff --git a/crates/nu-command/src/env/load_env.rs b/crates/nu-command/src/env/load_env.rs index c4edfebc82..36b6e3daf1 100644 --- a/crates/nu-command/src/env/load_env.rs +++ b/crates/nu-command/src/env/load_env.rs @@ -75,7 +75,10 @@ impl Command for LoadEnv { } Ok(PipelineData::new(call.head)) } - _ => Err(ShellError::UnsupportedInput("Record".into(), span)), + _ => Err(ShellError::UnsupportedInput( + "Record not supported".into(), + span, + )), }, } } diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index c477338f02..f8c8a46a1b 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -95,7 +95,10 @@ impl Command for Save { Ok(PipelineData::new(span)) } - v => Err(ShellError::UnsupportedInput(v.get_type().to_string(), span)), + v => Err(ShellError::UnsupportedInput( + format!("{:?} not supported", v.get_type()), + span, + )), } } else { match input.into_value(span) { @@ -113,7 +116,10 @@ impl Command for Save { Ok(PipelineData::new(span)) } - v => Err(ShellError::UnsupportedInput(v.get_type().to_string(), span)), + v => Err(ShellError::UnsupportedInput( + format!("{:?} not supported", v.get_type()), + span, + )), } } } diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 5d6fda94d6..38ac8d3c87 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -46,8 +46,14 @@ impl Command for ToNuon { fn value_to_string(v: &Value, span: Span) -> Result { match v { - Value::Binary { .. } => Err(ShellError::UnsupportedInput("binary".into(), span)), - Value::Block { .. } => Err(ShellError::UnsupportedInput("block".into(), span)), + Value::Binary { .. } => Err(ShellError::UnsupportedInput( + "binary not supported".into(), + span, + )), + Value::Block { .. } => Err(ShellError::UnsupportedInput( + "block not supported".into(), + span, + )), Value::Bool { val, .. } => { if *val { Ok("$true".to_string()) @@ -55,11 +61,20 @@ fn value_to_string(v: &Value, span: Span) -> Result { Ok("$false".to_string()) } } - Value::CellPath { .. } => Err(ShellError::UnsupportedInput("cellpath".to_string(), span)), - Value::CustomValue { .. } => Err(ShellError::UnsupportedInput("custom".to_string(), span)), + Value::CellPath { .. } => Err(ShellError::UnsupportedInput( + "cellpath not supported".to_string(), + span, + )), + Value::CustomValue { .. } => Err(ShellError::UnsupportedInput( + "custom not supported".to_string(), + span, + )), Value::Date { val, .. } => Ok(val.to_rfc3339()), Value::Duration { val, .. } => Ok(format!("{}ns", *val)), - Value::Error { .. } => Err(ShellError::UnsupportedInput("error".to_string(), span)), + Value::Error { .. } => Err(ShellError::UnsupportedInput( + "error not supported".to_string(), + span, + )), Value::Filesize { val, .. } => Ok(format!("{}b", *val)), Value::Float { val, .. } => Ok(format!("{}", *val)), Value::Int { val, .. } => Ok(format!("{}", *val)), diff --git a/crates/nu-command/src/math/abs.rs b/crates/nu-command/src/math/abs.rs index fa3ece0a2b..f75b280205 100644 --- a/crates/nu-command/src/math/abs.rs +++ b/crates/nu-command/src/math/abs.rs @@ -62,9 +62,12 @@ fn abs_helper(val: Value, head: Span) -> Value { val: val.abs(), span, }, - _ => Value::Error { + other => Value::Error { error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), + format!( + "Only numerical values are supported, input type: {:?}", + other.get_type() + ), head, ), }, diff --git a/crates/nu-command/src/math/ceil.rs b/crates/nu-command/src/math/ceil.rs index ffc2a3b069..c21cd80e2e 100644 --- a/crates/nu-command/src/math/ceil.rs +++ b/crates/nu-command/src/math/ceil.rs @@ -53,7 +53,10 @@ fn operate(value: Value, head: Span) -> Value { }, other => Value::Error { error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), + format!( + "Only numerical values are supported, input type: {:?}", + other.get_type() + ), other.span().unwrap_or(head), ), }, diff --git a/crates/nu-command/src/math/floor.rs b/crates/nu-command/src/math/floor.rs index 5442eaf2e1..919ac1de7f 100644 --- a/crates/nu-command/src/math/floor.rs +++ b/crates/nu-command/src/math/floor.rs @@ -53,7 +53,10 @@ fn operate(value: Value, head: Span) -> Value { }, other => Value::Error { error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), + format!( + "Only numerical values are supported, input type: {:?}", + other.get_type() + ), other.span().unwrap_or(head), ), }, diff --git a/crates/nu-command/src/math/round.rs b/crates/nu-command/src/math/round.rs index 06b0b601ea..8e21ae70c6 100644 --- a/crates/nu-command/src/math/round.rs +++ b/crates/nu-command/src/math/round.rs @@ -94,7 +94,10 @@ fn operate(value: Value, head: Span, precision: Option) -> Value { Value::Int { .. } => value, other => Value::Error { error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), + format!( + "Only numerical values are supported, input type: {:?}", + other.get_type() + ), other.span().unwrap_or(head), ), }, diff --git a/crates/nu-command/src/math/sqrt.rs b/crates/nu-command/src/math/sqrt.rs index dcf50ac32e..efc54cc0ff 100644 --- a/crates/nu-command/src/math/sqrt.rs +++ b/crates/nu-command/src/math/sqrt.rs @@ -62,7 +62,10 @@ fn operate(value: Value, head: Span) -> Value { } other => Value::Error { error: ShellError::UnsupportedInput( - String::from("Only numerical values are supported"), + format!( + "Only numerical values are supported, input type: {:?}", + other.get_type() + ), other.span().unwrap_or(head), ), }, diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 88de14bc0d..25d8ee1d53 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -152,7 +152,7 @@ pub enum ShellError { #[error("Unsupported input")] #[diagnostic(code(nu::shell::unsupported_input), url(docsrs))] - UnsupportedInput(String, #[label("{0} not supported")] Span), + UnsupportedInput(String, #[label("{0}")] Span), #[error("Unable to parse datetime")] #[diagnostic(