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
#[derive(Debug)]
pub enum Error {
    Http(reqwest::Error),
    Parse(reqwest::Error),
    CodeforcesApi(String),
    User(&'static str),
}

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

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Http(ref e) => write!(f, "HTTP: {}", e),
            Error::Parse(ref e) => write!(f, "Parse: {}", e),
            Error::CodeforcesApi(ref s) => write!(f, "Codeforces API: {}", s),
            Error::User(ref s) => write!(f, "User: {}", s),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Http(ref e) => Some(e),
            Error::Parse(ref e) => Some(e),
            Error::CodeforcesApi(_) => None,
            Error::User(_) => None,
        }
    }
}