clean up error handling a bit (#3297)

* clean up error handling a bit

* clippy
This commit is contained in:
Darren Schroeder 2021-04-10 10:10:23 -05:00 committed by GitHub
parent a131eddf54
commit 93f3ed98e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -126,27 +126,32 @@ pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
)); ));
} }
}, },
Primitive::Int(n_ref) => n_ref.to_bigint().expect("unexpected error"), Primitive::Int(n_ref) => n_ref.to_owned(),
Primitive::Boolean(a_bool) => match a_bool { Primitive::Boolean(a_bool) => match a_bool {
false => 0.to_bigint().expect("unexpected error"), false => BigInt::from(0),
true => 1.to_bigint().expect("unexpected error"), true => BigInt::from(1),
},
Primitive::Filesize(a_filesize) => match a_filesize.to_bigint() {
Some(n) => n,
None => {
return Err(ShellError::unimplemented(
"failed to convert filesize to bigint",
));
}
}, },
Primitive::Filesize(a_filesize) => a_filesize
.to_bigint()
.expect("Conversion should never fail."),
_ => { _ => {
return Err(ShellError::unimplemented( return Err(ShellError::unimplemented(
"'int' for non-numeric primitives", "'into int' for non-numeric primitives",
)) ))
} }
}) })
.into_value(&tag)), .into_value(&tag)),
UntaggedValue::Row(_) => Err(ShellError::labeled_error( UntaggedValue::Row(_) => Err(ShellError::labeled_error(
"specify column name to use, with 'int COLUMN'", "specify column name to use, with 'into int COLUMN'",
"found table", "found table",
tag, tag,
)), )),
_ => Err(ShellError::unimplemented("'int' for unsupported type")), _ => Err(ShellError::unimplemented("'into int' for unsupported type")),
} }
} }
@ -156,9 +161,7 @@ fn int_from_string(a_string: &str, tag: &Tag) -> Result<BigInt, ShellError> {
Err(_) => match a_string.parse::<f64>() { Err(_) => match a_string.parse::<f64>() {
Ok(res_float) => match res_float.to_bigint() { Ok(res_float) => match res_float.to_bigint() {
Some(n) => Ok(n), Some(n) => Ok(n),
None => Err(ShellError::unimplemented( None => Err(ShellError::unimplemented("failed to convert f64 to int")),
"failed to convert decimal to int",
)),
}, },
Err(_) => Err(ShellError::labeled_error( Err(_) => Err(ShellError::labeled_error(
"Could not convert string value to int", "Could not convert string value to int",