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
use log::SetLoggerError;
use std::{borrow::Cow, fmt, io};

#[derive(Debug)]
pub enum Error {
    Io(io::Error),
    Log(SetLoggerError),
    Desc(Cow<'static, str>),
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(io) => write!(fmt, "{}", io),
            Error::Log(log) => write!(fmt, "{}", log),
            Error::Desc(desc) => write!(fmt, "{}", desc.as_ref()),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(io) => io.source(),
            Error::Log(_) => None,
            Error::Desc(_) => None,
        }
    }
}

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

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

impl From<&'static str> for Error {
    fn from(str: &'static str) -> Self {
        Error::Desc(str.into())
    }
}

impl From<String> for Error {
    fn from(str: String) -> Self {
        Error::Desc(str.into())
    }
}

impl From<Cow<'static, str>> for Error {
    fn from(str: Cow<'static, str>) -> Self {
        Error::Desc(str)
    }
}