Added from type info to cast errors

This commit is contained in:
Josh Mitchell
2020-11-15 16:54:42 +11:00
parent 8f34cdfcdf
commit 8f310f3fe7
2 changed files with 6 additions and 6 deletions

View File

@@ -30,7 +30,7 @@ pub enum Error {
/// Step was out of range for usize on this platform
StepSizeOutOfRange(i32),
/// A numeric cast from `value` failed during `task`
NumericCastFailed {
CastFromI32Failed {
source: std::num::TryFromIntError,
task: ErrorTask,
value: usize,
@@ -72,7 +72,7 @@ impl std::error::Error for Error {
match &self {
NullInStr(err) => Some(err),
CouldNotCheckNAtoms(err) => Some(err.as_ref()),
NumericCastFailed { source, .. } => Some(source),
CastFromI32Failed { source, .. } => Some(source),
_ => None,
}
}
@@ -135,9 +135,9 @@ impl std::fmt::Display for Error {
write!(f, "Failed to read number of atoms in trajectory file")
}
StepSizeOutOfRange(n) => write!(f, "Step {} does not fit in usize on this platform", n),
NumericCastFailed { value, task, .. } => write!(
CastFromI32Failed { value, task, .. } => write!(
f,
"Numeric cast from {value} failed while {task}",
"Numeric cast from {value}:usize to i32 failed while {task}",
value = value,
task = task
),

View File

@@ -109,7 +109,7 @@ fn path_to_cstring(path: impl AsRef<Path>) -> Result<CString> {
}
fn to_i32(value: usize, task: ErrorTask) -> Result<i32> {
value.try_into().map_err(|e| Error::NumericCastFailed {
value.try_into().map_err(|e| Error::CastFromI32Failed {
source: e,
value,
task,
@@ -809,7 +809,7 @@ mod tests {
Err(e) => e,
_ => panic!("Conversion from -1 to u8 succeeded"),
};
let expected = Error::NumericCastFailed {
let expected = Error::CastFromI32Failed {
source: try_from_int_err,
task: ErrorTask::Write,
value: 3_294_967_295_usize,