aboutsummaryrefslogtreecommitdiff
path: root/base/src/error.rs
blob: 2806c7817542937d4604f7a7273d07b5111d78bb (plain)
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
use std::{error, fmt, io, result};
use std::error::Error as E;

use chrono::format::ParseError;

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    Parse(String),
    Chrono(ParseError),
    Io(io::Error),
    Custom(Box<error::Error>),
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str(self.description())
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        use self::Error::*;
        match self {
            &Parse(_) => "error while parsing",
            &Chrono(_) => "error while parsing time strings",
            &Io(_) => "error during input/output",
            &Custom(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        use self::Error::*;
        match self {
            &Parse(ref _e) => None,
            &Chrono(ref e) => Some(e),
            &Io(ref e) => Some(e),
            &Custom(ref e) => e.cause(),
        }
    }
}

impl From<ParseError> for Error {
    fn from(err: ParseError) -> Error {
        Error::Chrono(err)
    }
}

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