use std::error;
use std::fmt;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Error {
InvalidRecordLength {
actual: usize,
},
InvalidFieldLength {
expected: usize,
actual: usize,
},
InvalidCharacter {
field: &'static str,
byte: u8,
expected: &'static str,
},
InvalidVariant {
field: &'static str,
bytes: Vec<u8>,
expected: &'static str,
},
NotANumber {
bytes: Vec<u8>,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidRecordLength { actual } => {
write!(f, "record should be 132 byte long but is {actual}")
}
Self::InvalidFieldLength { expected, actual } => {
write!(f, "field should be {expected} byte long but is {actual}")
}
Self::InvalidCharacter {
field,
byte,
expected,
} => {
write!(
f,
"{field} is \"{}\" but should be {expected}",
*byte as char
)
}
Self::InvalidVariant {
field,
bytes,
expected,
} => {
let s = String::from_utf8_lossy(bytes);
write!(f, "found \"{s}\" in {field} but should be {expected}")
}
Self::NotANumber { bytes } => {
let s = String::from_utf8_lossy(bytes);
write!(f, "field should be a number but is \"{s}\"")
}
}
}
}
impl error::Error for Error {}