//! A general error module for the snp program use std::{error, fmt}; /// Snippet error, general error wrapper for the Snippet module /// All error are wrapper with more information available with the source() method #[derive(Debug)] pub enum SnippetError { /// A supposedly impossible to trigger error #[allow(dead_code)] Unknown, /// Clap error, originating from the Cli parser ClapError(clap::error::Error), /// A generic IOError, the source is mandatory to get the specifics IOError(std::io::Error), } #[derive(Debug)] struct PlaceHolder {} impl fmt::Display for SnippetError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { SnippetError::Unknown => write!(f, "An unknonw error occured"), SnippetError::ClapError(..) => write!(f, "A known error occured"), SnippetError::IOError(..) => write!(f, "An error occured while editing the file"), } } } impl error::Error for SnippetError { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { SnippetError::Unknown => None, // The cause is the underlying implementation error type. Is implicitly // cast to the trait object `&error::Error`. This works because the // underlying type already implements the `Error` trait. SnippetError::ClapError(ref e) => Some(e), SnippetError::IOError(ref e) => Some(e), } } } impl From for SnippetError { fn from(err: clap::error::Error) -> SnippetError { SnippetError::ClapError(err) } } impl From for SnippetError { fn from(err: std::io::Error) -> SnippetError { SnippetError::IOError(err) } }