use crate::*; use std::error::Error; use std::fmt::{Display, Formatter, Result}; use std::io; /// 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: ErrorKind, cmd: ActualCmd, } #[derive(Debug)] #[non_exhaustive] pub enum ErrorKind { Config, Fetch, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? Compile, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? Install, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? } impl MyError { pub(crate) fn new(kind: ErrorKind, source: io::Error, cmd: ActualCmd) -> MyError { MyError { source, kind, cmd } } /// Return the kind of this error. pub fn kind(&self) -> &ErrorKind { &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<'_>) -> Result { match self.kind { ErrorKind::Config => write!( f, "Could not read configuration file: {}", self.source().unwrap() ), ErrorKind::Fetch => write!( f, "Could not fetch with command `{}`: {}", self.cmd, self.source().unwrap() ), ErrorKind::Compile => write!( f, "Could not compile with command `{}`: {}", self.cmd, self.source().unwrap() ), ErrorKind::Install => write!( f, "Could not install with command `{}`: {}", self.cmd, self.source().unwrap() ), } } }