1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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)
    }
}