use core::fmt;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Error {
Truncated {
what: &'static str,
needed: usize,
found: usize,
},
InvalidMagic {
what: &'static str,
},
BadValue(&'static str),
Unsupported(&'static str),
TooLarge {
what: &'static str,
limit: usize,
},
}
impl Error {
pub fn truncated(what: &'static str, needed: usize, found: usize) -> Self {
Self::Truncated {
what,
needed,
found,
}
}
pub fn too_large(what: &'static str, limit: usize) -> Self {
Self::TooLarge { what, limit }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Truncated { what, .. } => {
f.write_str("truncated: ")?;
f.write_str(what)
}
Error::InvalidMagic { what } => {
f.write_str("invalid magic: ")?;
f.write_str(what)
}
Error::BadValue(what) => {
f.write_str("bad value: ")?;
f.write_str(what)
}
Error::Unsupported(what) => {
f.write_str("unsupported: ")?;
f.write_str(what)
}
Error::TooLarge { what, .. } => {
f.write_str("too large: ")?;
f.write_str(what)
}
}
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;