use std::{fmt, io, num, path};
pub enum MyError {
Clap(clap::error::Error),
Glob(glob::PatternError),
Io(io::Error),
ParseInt(num::ParseIntError),
Prefix(path::StripPrefixError),
Regex(regex::Error),
Walk(walkdir::Error),
Text(&'static str),
}
impl MyError {
pub fn eprint(&self) {
let error = self.to_string();
let error = error.trim_end();
eprintln!("{error}");
}
}
pub type MyResult<T> = Result<T, MyError>;
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Clap(ref error) => write!(f, "Clap error: {error}"),
Self::Glob(ref error) => write!(f, "Glob error: {error}"),
Self::Io(ref error) => write!(f, "I/O error: {error}"),
Self::ParseInt(ref error) => write!(f, "Parse error: {error}"),
Self::Prefix(ref error) => write!(f, "Prefix error: {error}"),
Self::Regex(ref error) => write!(f, "Regex error: {error}"),
Self::Walk(ref error) => write!(f, "Walk error: {error}"),
Self::Text(ref error) => write!(f, "Text error: {error}"),
}
}
}
impl fmt::Debug for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Clap(ref error) => error.fmt(f),
Self::Glob(ref error) => error.fmt(f),
Self::Io(ref error) => error.fmt(f),
Self::ParseInt(ref error) => error.fmt(f),
Self::Prefix(ref error) => error.fmt(f),
Self::Regex(ref error) => error.fmt(f),
Self::Walk(ref error) => error.fmt(f),
Self::Text(ref error) => error.fmt(f),
}
}
}
impl From<clap::error::Error> for MyError {
fn from(error: clap::error::Error) -> MyError {
Self::Clap(error)
}
}
impl From<glob::PatternError> for MyError {
fn from(error: glob::PatternError) -> MyError {
Self::Glob(error)
}
}
impl From<io::Error> for MyError {
fn from(error: io::Error) -> MyError {
Self::Io(error)
}
}
impl From<num::ParseIntError> for MyError {
fn from(error: num::ParseIntError) -> MyError {
Self::ParseInt(error)
}
}
impl From<path::StripPrefixError> for MyError {
fn from(error: path::StripPrefixError) -> MyError {
Self::Prefix(error)
}
}
impl From<regex::Error> for MyError {
fn from(error: regex::Error) -> MyError {
Self::Regex(error)
}
}
impl From<walkdir::Error> for MyError {
fn from(error: walkdir::Error) -> MyError {
Self::Walk(error)
}
}
impl From<&'static str> for MyError {
fn from(error: &'static str) -> MyError {
Self::Text(error)
}
}