mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 03:38:07 +02:00
CantConvert improvements (#4926)
* CantConvert improvements * cargo fmt
This commit is contained in:
@ -136,10 +136,11 @@ fn string_to_boolean(s: &str, span: Span) -> Result<bool, ShellError> {
|
||||
let val = o.parse::<f64>();
|
||||
match val {
|
||||
Ok(f) => Ok(f.abs() >= f64::EPSILON),
|
||||
Err(_) => Err(ShellError::CantConvert(
|
||||
Err(_) => Err(ShellError::CantConvertWithHelp(
|
||||
"boolean".to_string(),
|
||||
"string".to_string(),
|
||||
span,
|
||||
r#"the strings "true" and "false" can be converted into a bool"#.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
@ -260,10 +260,11 @@ fn action(
|
||||
Ok(d) => Value::Date { val: d, span: head },
|
||||
Err(reason) => {
|
||||
return Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
error: ShellError::CantConvertWithHelp(
|
||||
format!("could not parse as datetime using format '{}'", dt.0),
|
||||
reason.to_string(),
|
||||
head,
|
||||
"you can use `into datetime` without a format string to enable flexible parsing".to_string()
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -151,10 +151,11 @@ fn string_to_duration(s: &str, span: Span) -> Result<i64, ShellError> {
|
||||
}
|
||||
}
|
||||
|
||||
Err(ShellError::CantConvert(
|
||||
Err(ShellError::CantConvertWithHelp(
|
||||
"duration".to_string(),
|
||||
"string".to_string(),
|
||||
span,
|
||||
"supported units are ns, us, ms, sec, min, hr, day, and wk".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -206,8 +206,8 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
};
|
||||
match i64::from_str_radix(&i, radix) {
|
||||
Ok(n) => Value::Int { val: n, span: head },
|
||||
Err(reason) => Value::Error {
|
||||
error: ShellError::CantConvert("".to_string(), reason.to_string(), head),
|
||||
Err(_reason) => Value::Error {
|
||||
error: ShellError::CantConvert("int".to_string(), "string".to_string(), head),
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -218,11 +218,12 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
b if b.starts_with("0b") => {
|
||||
let num = match i64::from_str_radix(b.trim_start_matches("0b"), 2) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return Err(ShellError::CantConvert(
|
||||
"could not parse as integer".to_string(),
|
||||
reason.to_string(),
|
||||
Err(_reason) => {
|
||||
return Err(ShellError::CantConvertWithHelp(
|
||||
"int".to_string(),
|
||||
"string".to_string(),
|
||||
span,
|
||||
r#"digits following "0b" can only be 0 or 1"#.to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
@ -231,11 +232,13 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
h if h.starts_with("0x") => {
|
||||
let num = match i64::from_str_radix(h.trim_start_matches("0x"), 16) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return Err(ShellError::CantConvert(
|
||||
"could not parse as int".to_string(),
|
||||
reason.to_string(),
|
||||
Err(_reason) => {
|
||||
return Err(ShellError::CantConvertWithHelp(
|
||||
"int".to_string(),
|
||||
"string".to_string(),
|
||||
span,
|
||||
r#"hexadecimal digits following "0x" should be in 0-9, a-f, or A-F"#
|
||||
.to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
@ -246,7 +249,7 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
Err(_) => match a_string.parse::<f64>() {
|
||||
Ok(f) => Ok(f as i64),
|
||||
_ => Err(ShellError::CantConvert(
|
||||
"into int".to_string(),
|
||||
"int".to_string(),
|
||||
"string".to_string(),
|
||||
span,
|
||||
)),
|
||||
|
@ -257,6 +257,14 @@ pub fn action(
|
||||
span,
|
||||
),
|
||||
},
|
||||
Value::Binary { .. } => Value::Error {
|
||||
error: ShellError::CantConvertWithHelp(
|
||||
"string".into(),
|
||||
"binary".into(),
|
||||
span,
|
||||
"try using the `decode` command".into(),
|
||||
),
|
||||
},
|
||||
x => Value::Error {
|
||||
error: ShellError::CantConvert(String::from("string"), x.get_type().to_string(), span),
|
||||
},
|
||||
|
Reference in New Issue
Block a user