update miette and switch to GenericErrors (#5222)

This commit is contained in:
Kat Marchán
2022-04-18 05:34:10 -07:00
committed by GitHub
parent cf65f77b02
commit 1314a87cb0
141 changed files with 1569 additions and 689 deletions

View File

@ -136,11 +136,14 @@ 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::CantConvertWithHelp(
Err(_) => Err(ShellError::CantConvert(
"boolean".to_string(),
"string".to_string(),
span,
r#"the strings "true" and "false" can be converted into a bool"#.to_string(),
Some(
r#"the strings "true" and "false" can be converted into a bool"#
.to_string(),
),
)),
}
}

View File

@ -260,11 +260,11 @@ fn action(
Ok(d) => Value::Date { val: d, span: head },
Err(reason) => {
return Value::Error {
error: ShellError::CantConvertWithHelp(
error: ShellError::CantConvert(
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()
Some("you can use `into datetime` without a format string to enable flexible parsing".to_string())
),
}
}

View File

@ -101,7 +101,12 @@ fn action(input: &Value, head: Span) -> Value {
match other.parse::<f64>() {
Ok(x) => Value::Float { val: x, span: head },
Err(reason) => Value::Error {
error: ShellError::CantConvert("float".to_string(), reason.to_string(), *span),
error: ShellError::CantConvert(
"float".to_string(),
reason.to_string(),
*span,
None,
),
},
}
}

View File

@ -151,11 +151,11 @@ fn string_to_duration(s: &str, span: Span) -> Result<i64, ShellError> {
}
}
Err(ShellError::CantConvertWithHelp(
Err(ShellError::CantConvert(
"duration".to_string(),
"string".to_string(),
span,
"supported units are ns, us, ms, sec, min, hr, day, and wk".to_string(),
Some("supported units are ns, us, ms, sec, min, hr, day, and wk".to_string()),
))
}

View File

@ -148,7 +148,12 @@ pub fn action(input: &Value, span: Span) -> Value {
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
match a_string.trim().parse::<bytesize::ByteSize>() {
Ok(n) => Ok(n.0 as i64),
Err(_) => Err(ShellError::CantConvert("int".into(), "string".into(), span)),
Err(_) => Err(ShellError::CantConvert(
"int".into(),
"string".into(),
span,
None,
)),
}
}

View File

@ -207,7 +207,7 @@ 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("int".to_string(), "string".to_string(), head),
error: ShellError::CantConvert("int".to_string(), "string".to_string(), head, None),
},
}
}
@ -219,29 +219,30 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
let num = match i64::from_str_radix(b.trim_start_matches("0b"), 2) {
Ok(n) => n,
Err(_reason) => {
return Err(ShellError::CantConvertWithHelp(
return Err(ShellError::CantConvert(
"int".to_string(),
"string".to_string(),
span,
r#"digits following "0b" can only be 0 or 1"#.to_string(),
Some(r#"digits following "0b" can only be 0 or 1"#.to_string()),
))
}
};
Ok(num)
}
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::CantConvertWithHelp(
let num =
match i64::from_str_radix(h.trim_start_matches("0x"), 16) {
Ok(n) => n,
Err(_reason) => return Err(ShellError::CantConvert(
"int".to_string(),
"string".to_string(),
span,
r#"hexadecimal digits following "0x" should be in 0-9, a-f, or A-F"#
.to_string(),
))
}
};
Some(
r#"hexadecimal digits following "0x" should be in 0-9, a-f, or A-F"#
.to_string(),
),
)),
};
Ok(num)
}
_ => match a_string.parse::<i64>() {
@ -252,6 +253,7 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
"int".to_string(),
"string".to_string(),
span,
None,
)),
},
},

View File

@ -258,15 +258,20 @@ pub fn action(
),
},
Value::Binary { .. } => Value::Error {
error: ShellError::CantConvertWithHelp(
error: ShellError::CantConvert(
"string".into(),
"binary".into(),
span,
"try using the `decode` command".into(),
Some("try using the `decode` command".into()),
),
},
x => Value::Error {
error: ShellError::CantConvert(String::from("string"), x.get_type().to_string(), span),
error: ShellError::CantConvert(
String::from("string"),
x.get_type().to_string(),
span,
None,
),
},
}
}