wng_lib/
errors.rs

1pub type Result<T> = std::result::Result<T, WngError>;
2
3#[macro_export]
4macro_rules! error {
5    () => {
6        WngError {
7            line: line!(),
8            file: file!().to_string(),
9            message: "".to_owned(),
10        }
11    };
12    ($($msg:tt),*) => {
13        {
14            let mut message = String::new();
15
16            $(
17                message.push_str(&format!("{} ", $msg));
18            )*
19
20            message.pop(); // the trailing whitespace
21
22            WngError {
23                line: line!(),
24                file: file!().to_string(),
25                message,
26            }
27        }
28    };
29}
30
31impl From<std::io::Error> for WngError {
32    fn from(error: std::io::Error) -> Self {
33        let msg = format!("{}", error);
34        error!(msg)
35    }
36}
37
38impl From<toml::de::Error> for WngError {
39    fn from(error: toml::de::Error) -> Self {
40        let msg = format!("{}", error);
41        error!(msg)
42    }
43}
44
45impl From<toml::ser::Error> for WngError {
46    fn from(error: toml::ser::Error) -> Self {
47        let msg = format!("{}", error);
48        error!(msg)
49    }
50}
51
52impl From<git2::Error> for WngError {
53    fn from(error: git2::Error) -> Self {
54        error!((error.message()))
55    }
56}
57
58impl From<fs_extra::error::Error> for WngError {
59    fn from(error: fs_extra::error::Error) -> Self {
60        error!((format!("{}", error)))
61    }
62}
63
64#[derive(PartialEq, Eq)]
65pub struct WngError {
66    pub line: u32,
67    pub file: String,
68    pub message: String,
69}
70
71impl std::fmt::Display for WngError {
72    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
73        write!(f, "Error: {} - {}:{}", self.message, self.file, self.line)
74    }
75}
76
77impl std::fmt::Debug for WngError {
78    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
79        write!(f, "Error: {} - {}:{}", self.message, self.file, self.line)
80    }
81}