use std::{ error, fmt, }; #[derive(Debug)] pub enum Error { Unknown, Message(String), MessageStatic(&'static str), FileNotFound(std::path::PathBuf), BadPath(std::path::PathBuf), ExpectedFile(std::path::PathBuf), } impl error::Error for Error{} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "failed to parse args: ")?; match self { Error::Message(message) => write!(f, "{}", message), Error::MessageStatic(message) => write!(f, "{}", message), Error::FileNotFound(file) => write!(f, "file not found: `{:?}`", file), Error::BadPath(file) => write!(f, "bad path: `{:?}`", file), Error::ExpectedFile(file) => write!(f, "expected a file: `{:?}`", file), _ => write!(f, "unknown error"), } } } impl From for Error { fn from(string: String) -> Self { Self::Message(string) } } impl From<&'static str> for Error { fn from(string: &'static str) -> Self { Self::MessageStatic(string) } } use std::path::PathBuf; impl From for Error { fn from(path: PathBuf) -> Self { if !path.exists() { Self::FileNotFound(path) } else if path.is_dir() { Self::ExpectedFile(path) } else { Self::BadPath(path) } } }