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
This commit is contained in:
Justin Ma 2022-02-28 23:14:33 +08:00 committed by GitHub
parent 0924975b4c
commit b09acdb7f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 15 deletions

View File

@ -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,
)),
},
}
}

View File

@ -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,
)),
}
}
}

View File

@ -46,8 +46,14 @@ impl Command for ToNuon {
fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
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<String, ShellError> {
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)),

View File

@ -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,
),
},

View File

@ -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),
),
},

View File

@ -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),
),
},

View File

@ -94,7 +94,10 @@ fn operate(value: Value, head: Span, precision: Option<i64>) -> 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),
),
},

View File

@ -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),
),
},

View File

@ -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(