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
use bincode::Error as SedesError;
use std::convert::From;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::io::Error as IOError;
use std::result::Result as StdResult;

pub type Result<T> = StdResult<T, Error>;

#[derive(Debug)]
pub enum Error {
    Network(reqwest::Error),
    IOError(IOError),
    SedesError(SedesError),
    Parse(&'static str),
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::Network(network_err) => network_err.source(),
            Error::Parse(_) => None,
            Error::IOError(io_err) => io_err.source(),
            Error::SedesError(sedes_err) => sedes_err.source(),
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Error {
    pub fn from_parse(reason: &'static str) -> Error {
        Error::Parse(reason)
    }
}

impl From<reqwest::Error> for Error {
    fn from(error: reqwest::Error) -> Self {
        Error::Network(error)
    }
}

impl From<IOError> for Error {
    fn from(error: IOError) -> Self {
        Error::IOError(error)
    }
}

impl From<SedesError> for Error {
    fn from(error: SedesError) -> Self {
        Error::SedesError(error)
    }
}