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
pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug)]
pub struct Error {
    pub(crate) inner_error: anyhow::Error,
}

impl<'a> From<&'a str> for Error {
    fn from(msg: &'a str) -> Self {
        Error {
            inner_error: anyhow!(msg.to_string()),
        }
    }
}

impl From<String> for Error {
    fn from(msg: String) -> Self {
        Error {
            inner_error: anyhow!(msg),
        }
    }
}

impl From<anyhow::Error> for Error {
    fn from(e: anyhow::Error) -> Self {
        Error { inner_error: e }
    }
}

impl Error {
    pub fn wrap<E>(e: E) -> Self
    where
        E: std::error::Error + Into<anyhow::Error>,
    {
        Error {
            inner_error: anyhow!(e),
        }
    }
}

impl std::error::Error for Error {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner_error.fmt(f)
    }
}