use crate::*; use std::error::Error; use std::fmt; use std::fmt::{Display, Formatter}; use std::io; use std::result; pub type Result = result::Result; /// An error that can occur in this crate. /// /// Generally, this error corresponds to problems with underlying process. #[derive(Debug)] pub struct MyError { source: io::Error, kind: MyErrorKind, cmd: ActualCmd, } #[derive(Debug)] #[non_exhaustive] pub enum MyErrorKind { Config, PreInstall, Install, PostInstall, } impl MyError { pub(crate) fn new(kind: MyErrorKind, source: io::Error, cmd: ActualCmd) -> MyError { MyError { source, kind, cmd } } /// Return the kind of this error. pub fn kind(&self) -> &MyErrorKind { &self.kind } } impl Error for MyError { fn source(&self) -> Option<&(dyn Error + 'static)> { Some(&self.source) } } impl Display for MyError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self.kind { MyErrorKind::Config => write!( f, "Could not read configuration file: {}", self.source().unwrap() ), MyErrorKind::PreInstall => write!( f, "Could not do the pre_install with command {}: {}", self.cmd, self.source().unwrap() ), MyErrorKind::Install => write!( f, "Could not install with command {}: {}", self.cmd, self.source().unwrap() ), MyErrorKind::PostInstall => write!( f, "Could not do the post_install command {}: {}", self.cmd, self.source().unwrap() ), } } }