diff options
author | Till Höppner | 2016-02-25 06:48:03 +0100 |
---|---|---|
committer | Till Höppner | 2016-02-25 06:48:03 +0100 |
commit | 9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a (patch) | |
tree | 1ca71876029cb466aa6230f1aead05b32f19bf6d /base/src/error.rs | |
parent | 685aac1cc537692b2cf9342dcb6c26fa74c3c920 (diff) | |
download | ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.tar.gz ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.tar.xz ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.zip |
Refactor... everything.
Diffstat (limited to 'base/src/error.rs')
-rw-r--r-- | base/src/error.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/base/src/error.rs b/base/src/error.rs new file mode 100644 index 0000000..2806c78 --- /dev/null +++ b/base/src/error.rs @@ -0,0 +1,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) + } +} |