ex_cli/
error.rs

1use std::path::{Path, StripPrefixError};
2use std::{io, num};
3use thiserror::Error;
4
5pub type MyResult<T> = Result<T, MyError>;
6
7#[derive(Debug, Error)]
8pub enum MyError {
9    #[error(transparent)]
10    Chrono(#[from] chrono::ParseError),
11    #[error(transparent)]
12    Clap(#[from] clap::error::Error),
13    #[error(transparent)]
14    Git(#[from] git2::Error),
15    #[error("{0}: {1}")]
16    Glob(glob::PatternError, String),
17    #[error(transparent)]
18    Io(#[from] io::Error),
19    #[error("{0}: {1}")]
20    Io2(io::Error, String),
21    #[error(transparent)]
22    ParseInt(#[from] num::ParseIntError),
23    #[error("{0}: {1}")]
24    PkZip(zip::result::ZipError, String),
25    #[error("{0}: {1}")]
26    Prefix(StripPrefixError, String),
27    #[error(transparent)]
28    Regex(#[from] regex::Error),
29    #[error("{0}: {1}")]
30    SevenZ(sevenz_rust2::Error, String),
31    #[error(transparent)]
32    Walk(#[from] walkdir::Error),
33    #[error("{0}")]
34    Text(String),
35}
36
37impl MyError {
38    pub fn eprint(&self) {
39        let error = self.to_string();
40        let error = error.trim_end();
41        eprintln!("{error}");
42    }
43}
44
45impl From<(glob::PatternError, &String)> for MyError {
46    fn from(value: (glob::PatternError, &String)) -> Self {
47        let (error, glob) = value;
48        let glob = glob.to_string();
49        Self::Glob(error, glob)
50    }
51}
52
53impl From<(io::Error, &Path)> for MyError {
54    fn from(value: (io::Error, &Path)) -> Self {
55        let (error, path) = value;
56        let path = path.display().to_string();
57        Self::Io2(error, path)
58    }
59}
60
61impl From<(zip::result::ZipError, &Path)> for MyError {
62    fn from(value: (zip::result::ZipError, &Path)) -> Self {
63        let (error, path) = value;
64        let path = path.display().to_string();
65        Self::PkZip(error, path)
66    }
67}
68
69impl From<(StripPrefixError, &Path)> for MyError {
70    fn from(value: (StripPrefixError, &Path)) -> Self {
71        let (error, path) = value;
72        let path = path.display().to_string();
73        Self::Prefix(error, path)
74    }
75}
76
77impl From<(sevenz_rust2::Error, &Path)> for MyError {
78    fn from(value: (sevenz_rust2::Error, &Path)) -> Self {
79        let (error, path) = value;
80        let path = path.display().to_string();
81        Self::SevenZ(error, path)
82    }
83}
84
85impl From<&str> for MyError {
86    fn from(error: &str) -> Self {
87        let error = error.to_string();
88        Self::Text(error)
89    }
90}