use std::error::Error as StdError; use std::fmt::{Display, Formatter}; use std::io; /// Errors that can occur when encoding or decoding. #[derive(Debug)] pub struct Error { /// Box this to keep the size of `Result` small. cause: Box, } #[derive(Debug)] enum Cause { Io(io::Error), Owned(Box), Static(&'static str), } impl Error { pub(crate) fn new_owned(msg: impl Into>) -> Self { Self { cause: Box::new(Cause::Owned(msg.into())), } } pub(crate) fn new_static(msg: &'static str) -> Self { Self { cause: Box::new(Cause::Static(msg)), } } } impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match &*self.cause { Cause::Io(e) => e.fmt(f), Cause::Owned(msg) => write!(f, "{msg}"), Cause::Static(msg) => write!(f, "{msg}"), } } } impl StdError for Error { fn source(&self) -> Option<&(dyn StdError + 'static)> { match &*self.cause { Cause::Io(e) => Some(e), Cause::Owned(_) => None, Cause::Static(_) => None, } } } impl From for Error { fn from(e: io::Error) -> Self { Self { cause: Box::new(Cause::Io(e)), } } }