98 lines
3.9 KiB
Rust
98 lines
3.9 KiB
Rust
//! A general error module for the snp program
|
|
#![cfg(not(tarpaulin_include))]
|
|
use std::{error, fmt, path::PathBuf};
|
|
|
|
/// Snippet error, general error wrapper for the Snippet module
|
|
/// All error are wrapper with more information available with the source() method
|
|
#[derive(Debug)]
|
|
#[allow(dead_code)]
|
|
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),
|
|
/// Yaml error, generated during serialisation of snippets
|
|
YAMLError(serde_yaml::Error),
|
|
/// The file doesn't exits and can't be used
|
|
NotFound(PathBuf),
|
|
/// The file doen't exist and no creation was asked
|
|
NotFoundNoRetryError(PathBuf),
|
|
/// The snippet was found but shouldn't be erased because of flags
|
|
FoundNoEraseError(PathBuf),
|
|
/// The snippet couldn't be reparsed
|
|
ReparsingError,
|
|
/// The custom formatter coulnd't access an index
|
|
IndexError(usize),
|
|
}
|
|
|
|
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, "An error occured while parsing the input"),
|
|
SnippetError::IOError(..) => write!(f, "An error occured while editing the file"),
|
|
SnippetError::YAMLError(..) => {
|
|
write!(f, "Couldn't convert the given arguments into a yaml")
|
|
}
|
|
SnippetError::NotFound(ref p) => write!(
|
|
f,
|
|
"The file {:?} doesn't exists",p.file_name().unwrap(),
|
|
),
|
|
SnippetError::NotFoundNoRetryError(ref p) => {
|
|
write!(
|
|
f,
|
|
"The config {:?} doen't exists. Use -c to create it.",
|
|
p.file_name(),
|
|
)
|
|
}
|
|
SnippetError::FoundNoEraseError(ref p) => write!(
|
|
f,
|
|
"The snippet {:?} already exists. Use -f/--force to overwrite it.",
|
|
p.file_name().unwrap(),
|
|
),
|
|
SnippetError::ReparsingError => write!(f,"The snipped couldn't be re-parsed, it might be caused by a bad value in your edit. \nThe snipped was saved as a raw file instead but cant be used as is."),
|
|
SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {} arguments",i,i+1),
|
|
}
|
|
}
|
|
}
|
|
|
|
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),
|
|
SnippetError::YAMLError(ref e) => Some(e),
|
|
SnippetError::NotFound(_) => None,
|
|
SnippetError::NotFoundNoRetryError(_) => None,
|
|
SnippetError::FoundNoEraseError(_) => None,
|
|
SnippetError::ReparsingError => None,
|
|
SnippetError::IndexError(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<clap::error::Error> for SnippetError {
|
|
fn from(err: clap::error::Error) -> SnippetError {
|
|
SnippetError::ClapError(err)
|
|
}
|
|
}
|
|
|
|
impl From<std::io::Error> for SnippetError {
|
|
fn from(err: std::io::Error) -> SnippetError {
|
|
SnippetError::IOError(err)
|
|
}
|
|
}
|
|
|
|
impl From<serde_yaml::Error> for SnippetError {
|
|
fn from(err: serde_yaml::Error) -> SnippetError {
|
|
SnippetError::YAMLError(err)
|
|
}
|
|
}
|