diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-01-24 20:19:56 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-02-09 13:45:13 +0100 |
commit | bd0d44d4b2ca193b98a70793b2f68f378f17e4d0 (patch) | |
tree | afae16507e3a64661e0edd48da19c992b7ae5c76 /imag-store | |
parent | fa4beeebcdf48a275ef5111ff87767a853113f13 (diff) |
Add error module for imag-store
Diffstat (limited to 'imag-store')
-rw-r--r-- | imag-store/src/error.rs | 81 | ||||
-rw-r--r-- | imag-store/src/main.rs | 1 |
2 files changed, 82 insertions, 0 deletions
diff --git a/imag-store/src/error.rs b/imag-store/src/error.rs new file mode 100644 index 00000000..a9884250 --- /dev/null +++ b/imag-store/src/error.rs @@ -0,0 +1,81 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Display, Formatter}; + +/** + * Kind of store error + */ +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum StoreErrorKind { + BackendError, + NoCommandlineCall, + // maybe more +} + +fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { + match e { + &StoreErrorKind::BackendError => "Backend Error", + &StoreErrorKind::NoCommandlineCall => "No commandline call", + } +} + +impl Display for StoreErrorKind { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", store_error_type_as_str(self))); + Ok(()) + } + +} + +#[derive(Debug)] +pub struct StoreError { + err_type: StoreErrorKind, + cause: Option<Box<Error>>, +} + +impl StoreError { + + /** + * Build a new StoreError from an StoreErrorKind, optionally with cause + */ + pub fn new(errtype: StoreErrorKind, cause: Option<Box<Error>>) + -> StoreError + { + StoreError { + err_type: errtype, + cause: cause, + } + } + + /** + * Get the error type of this StoreError + */ + pub fn err_type(&self) -> StoreErrorKind { + self.err_type.clone() + } + +} + +impl Display for StoreError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", store_error_type_as_str(&self.err_type.clone()))); + Ok(()) + } + +} + +impl Error for StoreError { + + fn description(&self) -> &str { + store_error_type_as_str(&self.err_type.clone()) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/imag-store/src/main.rs b/imag-store/src/main.rs index 1f087636..62188035 100644 --- a/imag-store/src/main.rs +++ b/imag-store/src/main.rs @@ -10,6 +10,7 @@ extern crate libimagutil; use libimagrt::runtime::Runtime; use std::process::exit; +mod error; mod ui; mod create; mod read; |