diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-05-15 16:53:31 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-05-17 00:26:36 +0200 |
commit | 20ac5247f1be9cb65fd511e4a3ce693a45dd8a8a (patch) | |
tree | c6e066992c1596c99b99a6473e95494f2a587529 /libimagentrylist | |
parent | 6850146e4286df3529e291eec129b92ebf6a02fb (diff) |
libimagentrylist: Replace error code with code generator macro
Diffstat (limited to 'libimagentrylist')
-rw-r--r-- | libimagentrylist/Cargo.toml | 3 | ||||
-rw-r--r-- | libimagentrylist/src/error.rs | 85 | ||||
-rw-r--r-- | libimagentrylist/src/lib.rs | 1 |
3 files changed, 10 insertions, 79 deletions
diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml index e8905dfe..462331c7 100644 --- a/libimagentrylist/Cargo.toml +++ b/libimagentrylist/Cargo.toml @@ -11,3 +11,6 @@ toml = "0.1.25" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagentrylist/src/error.rs b/libimagentrylist/src/error.rs index 24051753..15640042 100644 --- a/libimagentrylist/src/error.rs +++ b/libimagentrylist/src/error.rs @@ -2,83 +2,10 @@ use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; -/** - * Kind of error - */ -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum ListErrorKind { - FormatError, - EntryError, - IterationError, - CLIError, -} - -fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{ - match *err { - ListErrorKind::FormatError => "FormatError", - ListErrorKind::EntryError => "EntryError", - ListErrorKind::IterationError => "IterationError", - ListErrorKind::CLIError => "No CLI subcommand for listing entries", - } -} - -impl Display for ListErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", counter_error_type_as_str(self))); - Ok(()) - } - -} - -/** - * Store error type - */ -#[derive(Debug)] -pub struct ListError { - err_type: ListErrorKind, - cause: Option<Box<Error>>, -} - -impl ListError { - - /** - * Build a new ListError from an ListErrorKind, optionally with cause - */ - pub fn new(errtype: ListErrorKind, cause: Option<Box<Error>>) -> ListError { - ListError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this ListError - */ - pub fn err_type(&self) -> ListErrorKind { - self.err_type - } - -} - -impl Display for ListError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for ListError { - - fn description(&self) -> &str { - counter_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(ListError, ListErrorKind, + FormatError => "FormatError", + EntryError => "EntryError", + IterationError => "IterationError", + CLIError => "No CLI subcommand for listing entries" +); diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs index b364e4d9..082b6fa3 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -19,6 +19,7 @@ extern crate clap; extern crate toml; extern crate libimagstore; +#[macro_use] extern crate libimagerror; pub mod cli; pub mod error; |