use core::fmt;
#[derive(Debug)]
pub enum Error {
NotFound,
InvalidArgument(String),
WrongType {
expected: &'static str,
got: &'static str,
},
Encode(String),
Decode(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NotFound => write!(f, "not found"),
Error::InvalidArgument(s) => write!(f, "invalid argument: {s}"),
Error::WrongType { expected, got } => {
write!(f, "wrong type: expected {expected}, got {got}")
}
Error::Encode(e) => write!(f, "encode error: {e}"),
Error::Decode(e) => write!(f, "decode error: {e}"),
}
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_debug_smoke() {
let e = Error::Decode("x".into());
let _ = format!("{e:?}");
}
#[test]
fn error_display_smoke() {
let e = Error::WrongType {
expected: "string",
got: "hash",
};
let s = e.to_string();
assert!(s.contains("expected string"));
assert!(s.contains("got hash"));
}
}