From cb855ae33546aa1ce99a8411645a226856d9c4e0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 14:09:29 +0200 Subject: [PATCH 01/15] Initial import --- libimagerror/Cargo.toml | 6 ++++++ libimagerror/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 libimagerror/Cargo.toml create mode 100644 libimagerror/src/lib.rs diff --git a/libimagerror/Cargo.toml b/libimagerror/Cargo.toml new file mode 100644 index 00000000..bd769698 --- /dev/null +++ b/libimagerror/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagerror" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagerror/src/lib.rs b/libimagerror/src/lib.rs new file mode 100644 index 00000000..cdfbe1aa --- /dev/null +++ b/libimagerror/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +} From 2f03077abf0e0e048e7d088e0471fd027ae95328 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:00:51 +0200 Subject: [PATCH 02/15] Add error generator macro --- libimagerror/src/error_gen.rs | 113 ++++++++++++++++++++++++++++++++++ libimagerror/src/lib.rs | 7 +-- 2 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 libimagerror/src/error_gen.rs diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs new file mode 100644 index 00000000..471c6362 --- /dev/null +++ b/libimagerror/src/error_gen.rs @@ -0,0 +1,113 @@ +#[macro_export] +macro_rules! generate_error_types { + ( + $name: ident, + $kindname: ident, + $($kind:ident => $string:expr),* + ) => { + #[derive(Clone, Copy, Debug, PartialEq)] + pub enum $kindname { + $( $kind ),* + } + + impl Display for $kindname { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + let s = match *self { + $( $kindname::$kind => $string ),* + }; + try!(write!(fmt, "{}", s)); + Ok(()) + } + + } + + #[derive(Debug)] + pub struct $name { + err_type: $kindname, + cause: Option>, + } + + impl $name { + + pub fn new(errtype: $kindname, cause: Option>) -> $name { + $name { + err_type: errtype, + cause: cause, + } + } + + pub fn err_type(&self) -> $kindname { + self.err_type + } + + } + + impl Display for $name { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", self.err_type)); + Ok(()) + } + + } + + impl Error for $name { + + fn description(&self) -> &str { + match self.err_type { + $( $kindname::$kind => $string ),* + } + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + + } + + } +} + +#[cfg(test)] +mod test { + use std::error::Error; + use std::fmt::Error as FmtError; + use std::fmt::{Display, Formatter}; + + generate_error_types!(TestError, TestErrorKind, + TestErrorKindA => "testerrorkind a", + TestErrorKindB => "testerrorkind B"); + + #[test] + fn test_a() { + let kind = TestErrorKind::TestErrorKindA; + assert_eq!(String::from("testerrorkind a"), format!("{}", kind)); + + let e = TestError::new(kind, None); + assert_eq!(String::from("[testerrorkind a]"), format!("{}", e)); + } + + #[test] + fn test_b() { + let kind = TestErrorKind::TestErrorKindB; + assert_eq!(String::from("testerrorkind B"), format!("{}", kind)); + + let e = TestError::new(kind, None); + assert_eq!(String::from("[testerrorkind B]"), format!("{}", e)); + + } + + #[test] + fn test_ab() { + let kinda = TestErrorKind::TestErrorKindA; + let kindb = TestErrorKind::TestErrorKindB; + assert_eq!(String::from("testerrorkind a"), format!("{}", kinda)); + assert_eq!(String::from("testerrorkind B"), format!("{}", kindb)); + + let e = TestError::new(kinda, Some(Box::new(TestError::new(kindb, None)))); + assert_eq!(String::from("[testerrorkind a]"), format!("{}", e)); + assert_eq!(TestErrorKind::TestErrorKindA, e.err_type()); + assert_eq!(String::from("[testerrorkind B]"), format!("{}", e.cause().unwrap())); + } +} diff --git a/libimagerror/src/lib.rs b/libimagerror/src/lib.rs index cdfbe1aa..9f79f549 100644 --- a/libimagerror/src/lib.rs +++ b/libimagerror/src/lib.rs @@ -1,6 +1 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - } -} +pub mod error_gen; From a69fd780589db82aad7756d579c523aabc08fd13 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:39:52 +0200 Subject: [PATCH 03/15] Add dependency: libimagerror --- libimagstore/Cargo.toml | 3 +++ libimagstore/src/lib.rs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index 7c0fbf49..72d068d5 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -15,6 +15,9 @@ version = "2.0.1" crossbeam = "0.2.8" walkdir = "0.1.5" +[dependencies.libimagerror] +path = "../libimagerror" + [dev-dependencies] tempdir = "0.3.4" env_logger = "0.3" diff --git a/libimagstore/src/lib.rs b/libimagstore/src/lib.rs index e7efa3be..89657283 100644 --- a/libimagstore/src/lib.rs +++ b/libimagstore/src/lib.rs @@ -24,6 +24,8 @@ extern crate semver; extern crate crossbeam; extern crate walkdir; +#[macro_use] extern crate libimagerror; + pub mod storeid; pub mod error; pub mod hook; From 627fa7de46701a1848ffe8c464cc7dc4a1618b0c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:40:06 +0200 Subject: [PATCH 04/15] Replace error code with macro for code generation --- libimagstore/src/error.rs | 228 +++++++------------------------------- 1 file changed, 37 insertions(+), 191 deletions(-) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 1976622d..ad36963e 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -1,138 +1,45 @@ use std::error::Error; use std::fmt::Error as FmtError; -use std::fmt::{Debug, Display, Formatter}; -use std::fmt; +use std::fmt::{Display, Formatter}; use std::convert::From; -/** - * Kind of store error - */ -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum StoreErrorKind { - ConfigurationError, - FileError, - IdLocked, - IdNotFound, - OutOfMemory, - FileNotFound, - FileNotCreated, - IoError, - StorePathExists, - StorePathCreate, - LockError, - LockPoisoned, - EntryAlreadyBorrowed, - EntryAlreadyExists, - MalformedEntry, - HeaderPathSyntaxError, - HeaderPathTypeFailure, - HeaderKeyNotFound, - HeaderTypeFailure, - HookRegisterError, - AspectNameNotFoundError, - HookExecutionError, - PreHookExecuteError, - PostHookExecuteError, - StorePathLacksVersion, - GlobError, - EncodingError, - // maybe more -} +generate_error_types!(StoreError, StoreErrorKind, + ConfigurationError => "Store Configuration Error", + FileError => "File Error", + IoError => "IO Error", + IdLocked => "ID locked", + IdNotFound => "ID not found", + OutOfMemory => "Out of Memory", + FileNotFound => "File corresponding to ID not found", + FileNotCreated => "File corresponding to ID could not be created", + StorePathExists => "Store path exists", + StorePathCreate => "Store path create", + LockError => "Error locking datastructure", + LockPoisoned => "The internal Store Lock has been poisoned", + EntryAlreadyBorrowed => "Entry is already borrowed", + EntryAlreadyExists => "Entry already exists", + MalformedEntry => "Entry has invalid formatting, missing header", + HeaderPathSyntaxError => "Syntax error in accessor string", + HeaderPathTypeFailure => "Header has wrong type for path", + HeaderKeyNotFound => "Header Key not found", + HeaderTypeFailure => "Header type is wrong", + HookRegisterError => "Hook register error", + AspectNameNotFoundError => "Aspect name not found", + HookExecutionError => "Hook execution error", + PreHookExecuteError => "Pre-Hook execution error", + PostHookExecuteError => "Post-Hook execution error", + StorePathLacksVersion => "The supplied store path has no version part", + GlobError => "glob() error", + EncodingError => "Encoding error" +); -fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { - match *e { - StoreErrorKind::ConfigurationError => "Store Configuration Error", - StoreErrorKind::FileError | - StoreErrorKind::IoError => "File Error", - StoreErrorKind::IdLocked => "ID locked", - StoreErrorKind::IdNotFound => "ID not found", - StoreErrorKind::OutOfMemory => "Out of Memory", - StoreErrorKind::FileNotFound => "File corresponding to ID not found", - StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", - StoreErrorKind::StorePathExists | - StoreErrorKind::StorePathCreate => "Store path create", - StoreErrorKind::LockError => "Error locking datastructure", - StoreErrorKind::LockPoisoned - => "The internal Store Lock has been poisoned", - StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed", - StoreErrorKind::EntryAlreadyExists => "Entry already exists", - StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header", - StoreErrorKind::HeaderPathSyntaxError => "Syntax error in accessor string", - StoreErrorKind::HeaderPathTypeFailure => "Header has wrong type for path", - StoreErrorKind::HeaderKeyNotFound => "Header Key not found", - StoreErrorKind::HeaderTypeFailure => "Header type is wrong", - StoreErrorKind::HookRegisterError => "Hook register error", - StoreErrorKind::AspectNameNotFoundError => "Aspect name not found", - StoreErrorKind::HookExecutionError => "Hook execution error", - StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error", - StoreErrorKind::PostHookExecuteError => "Post-Hook execution error", - StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part", - StoreErrorKind::GlobError => "glob() error", - StoreErrorKind::EncodingError => "Encoding error", - } -} - -impl Display for StoreErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", store_error_type_as_str(self))); - Ok(()) - } - -} - -/** - * Store error type - */ -#[derive(Debug)] -pub struct StoreError { - err_type: StoreErrorKind, - cause: Option>, -} - -impl StoreError { - - /** - * Build a new StoreError from an StoreErrorKind, optionally with cause - */ - pub fn new(errtype: StoreErrorKind, cause: Option>) - -> StoreError - { - StoreError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this StoreError - */ - pub fn err_type(&self) -> StoreErrorKind { - self.err_type - } - -} - -impl Display for StoreError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", store_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for StoreError { - - fn description(&self) -> &str { - store_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(ParserError, ParserErrorKind, + TOMLParserErrors => "Several TOML-Parser-Errors", + MissingMainSection => "Missing main section", + MissingVersionInfo => "Missing version information in main section", + NonTableInBaseTable => "A non-table was found in the base table", + HeaderInconsistency => "The header is inconsistent" +); impl From for StoreError { fn from(ps: ParserError) -> StoreError { @@ -152,64 +59,3 @@ impl From<::std::io::Error> for StoreError { } } -#[derive(Clone)] -pub enum ParserErrorKind { - TOMLParserErrors, - MissingMainSection, - MissingVersionInfo, - NonTableInBaseTable, - HeaderInconsistency, -} - -pub struct ParserError { - kind: ParserErrorKind, - cause: Option>, -} - -impl ParserError { - - pub fn new(k: ParserErrorKind, cause: Option>) -> ParserError { - ParserError { - kind: k, - cause: cause, - } - } - -} - -impl Debug for ParserError { - - fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { - try!(write!(f, "{:?}", self.description())); - Ok(()) - } - -} - -impl Display for ParserError { - - fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { - try!(write!(f, "{}", self.description())); - Ok(()) - } - -} - -impl Error for ParserError { - - fn description(&self) -> &str { - match self.kind { - ParserErrorKind::TOMLParserErrors => "Several TOML-Parser-Errors", - ParserErrorKind::MissingMainSection => "Missing main section", - ParserErrorKind::MissingVersionInfo => "Missing version information in main section", - ParserErrorKind::NonTableInBaseTable => "A non-table was found in the base table", - ParserErrorKind::HeaderInconsistency => "The header is inconsistent", - } - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} - From 42f7e086a32d805f2965058b08cbdf303a331cb5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:41:25 +0200 Subject: [PATCH 05/15] Replace error code with macro for code generation --- libimagstore/src/hook/error.rs | 82 ++-------------------------------- 1 file changed, 4 insertions(+), 78 deletions(-) diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs index b9abd4fe..ec06dd98 100644 --- a/libimagstore/src/hook/error.rs +++ b/libimagstore/src/hook/error.rs @@ -3,14 +3,10 @@ use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; use std::convert::Into; -/** - * Kind of error - */ -#[derive(Clone, Copy, Debug)] -pub enum HookErrorKind { - HookExecutionError, - AccessTypeViolation, -} +generate_error_types!(HookError, HookErrorKind, + HookExecutionError => "Hook exec error", + AccessTypeViolation => "Hook access type violation" +); pub trait IntoHookError { fn into_hookerror(self) -> HookError; @@ -33,73 +29,3 @@ impl Into for (HookErrorKind, Box) { } -fn hook_error_type_as_str(e: &HookErrorKind) -> &'static str { - match *e { - HookErrorKind::HookExecutionError => "Hook exec error", - HookErrorKind::AccessTypeViolation => "Hook access type violation", - } -} - -impl Display for HookErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", hook_error_type_as_str(self))); - Ok(()) - } - -} - -/** - * Error type - */ -#[derive(Debug)] -pub struct HookError { - err_type: HookErrorKind, - cause: Option>, -} - -impl HookError { - - /** - * Build a new HookError from an HookErrorKind, optionally with cause - */ - pub fn new(errtype: HookErrorKind, cause: Option>) - -> HookError - { - HookError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this HookError - */ - pub fn err_type(&self) -> HookErrorKind { - self.err_type - } - -} - -impl Display for HookError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", hook_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for HookError { - - fn description(&self) -> &str { - hook_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} - - From 60bf3c68a7ab943f06b30f75cd380395930d51af Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 06/15] imag-store: Replace error code with code generator macro --- imag-store/Cargo.toml | 3 ++ imag-store/src/error.rs | 74 +++-------------------------------------- imag-store/src/main.rs | 1 + 3 files changed, 8 insertions(+), 70 deletions(-) diff --git a/imag-store/Cargo.toml b/imag-store/Cargo.toml index 0deba5a0..6c244322 100644 --- a/imag-store/Cargo.toml +++ b/imag-store/Cargo.toml @@ -19,3 +19,6 @@ path = "../libimagrt" [dependencies.libimagutil] path = "../libimagutil" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/imag-store/src/error.rs b/imag-store/src/error.rs index 30c9c8b5..472070f2 100644 --- a/imag-store/src/error.rs +++ b/imag-store/src/error.rs @@ -1,75 +1,9 @@ use std::error::Error; use std::fmt::Error as FmtError; -use std::clone::Clone; use std::fmt::{Display, Formatter}; -#[derive(Clone, Copy, Debug, PartialEq)] -/// Kind of store error -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>, -} - -impl StoreError { - - ///Build a new StoreError from an StoreErrorKind, optionally with cause - pub fn new(errtype: StoreErrorKind, cause: Option>) - -> StoreError - { - StoreError { - err_type: errtype, - cause: cause, - } - } - - /// Get the error type of this StoreError - pub fn err_type(&self) -> StoreErrorKind { - self.err_type - } - -} - -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) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(StoreError, StoreErrorKind, + BackendError => "Backend Error", + NoCommandlineCall => "No commandline call" +); diff --git a/imag-store/src/main.rs b/imag-store/src/main.rs index a1e9e09a..fd0a2c4d 100644 --- a/imag-store/src/main.rs +++ b/imag-store/src/main.rs @@ -22,6 +22,7 @@ extern crate toml; extern crate libimagrt; extern crate libimagstore; extern crate libimagutil; +#[macro_use] extern crate libimagerror; use libimagrt::runtime::Runtime; use std::process::exit; From 9140c36301dc8c1c6792b8806058b40406d03982 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 07/15] imag-view: Replace error code with code generator macro --- imag-view/Cargo.toml | 3 ++ imag-view/src/error.rs | 87 +++--------------------------------------- imag-view/src/main.rs | 1 + 3 files changed, 10 insertions(+), 81 deletions(-) diff --git a/imag-view/Cargo.toml b/imag-view/Cargo.toml index b3a52460..7c7b3d55 100644 --- a/imag-view/Cargo.toml +++ b/imag-view/Cargo.toml @@ -21,3 +21,6 @@ path = "../libimagrt" [dependencies.libimagutil] path = "../libimagutil" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/imag-view/src/error.rs b/imag-view/src/error.rs index 7a1ede78..64aa071d 100644 --- a/imag-view/src/error.rs +++ b/imag-view/src/error.rs @@ -2,85 +2,10 @@ use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; -/** - * Kind of store error - */ -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum ViewErrorKind { - StoreError, - NoVersion, - PatternError, - GlobBuildError, -} - -fn view_error_type_as_str(e: &ViewErrorKind) -> &'static str { - match *e { - ViewErrorKind::StoreError => "Store error", - ViewErrorKind::NoVersion => "No version specified", - ViewErrorKind::PatternError => "Error in Pattern", - ViewErrorKind::GlobBuildError => "Could not build glob() Argument", - } -} - -impl Display for ViewErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", view_error_type_as_str(self))); - Ok(()) - } - -} - -/** - * View error type - */ -#[derive(Debug)] -pub struct ViewError { - err_type: ViewErrorKind, - cause: Option>, -} - -impl ViewError { - - /** - * Build a new ViewError from an ViewErrorKind, optionally with cause - */ - pub fn new(errtype: ViewErrorKind, cause: Option>) - -> ViewError - { - ViewError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this ViewError - */ - pub fn err_type(&self) -> ViewErrorKind { - self.err_type - } - -} - -impl Display for ViewError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for ViewError { - - fn description(&self) -> &str { - view_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(ViewError, ViewErrorKind, + StoreError => "Store error", + NoVersion => "No version specified", + PatternError => "Error in Pattern", + GlobBuildError => "Could not build glob() Argument" +); diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 100d9aab..0efc25c8 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -23,6 +23,7 @@ extern crate toml; extern crate libimagrt; extern crate libimagstore; extern crate libimagutil; +#[macro_use] extern crate libimagerror; use std::result::Result as RResult; use std::process::exit; From 8602d5855a1b9b661516bc93db69850605b0253e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 08/15] libimagcounter: Replace error code with code generator macro --- libimagcounter/Cargo.toml | 3 ++ libimagcounter/src/error.rs | 87 +++---------------------------------- libimagcounter/src/lib.rs | 1 + 3 files changed, 10 insertions(+), 81 deletions(-) diff --git a/libimagcounter/Cargo.toml b/libimagcounter/Cargo.toml index 8d880182..da194678 100644 --- a/libimagcounter/Cargo.toml +++ b/libimagcounter/Cargo.toml @@ -11,3 +11,6 @@ semver = "0.2" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagcounter/src/error.rs b/libimagcounter/src/error.rs index 1aa347a6..f2e11eeb 100644 --- a/libimagcounter/src/error.rs +++ b/libimagcounter/src/error.rs @@ -2,85 +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 CounterErrorKind { - StoreReadError, - StoreWriteError, - HeaderTypeError, - HeaderFieldMissingError, -} - -fn counter_error_type_as_str(e: &CounterErrorKind) -> &'static str { - match *e { - CounterErrorKind::StoreReadError => "Store read error", - CounterErrorKind::StoreWriteError => "Store write error", - CounterErrorKind::HeaderTypeError => "Header type error", - CounterErrorKind::HeaderFieldMissingError => "Header field missing error", - } -} - -impl Display for CounterErrorKind { - - 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 CounterError { - err_type: CounterErrorKind, - cause: Option>, -} - -impl CounterError { - - /** - * Build a new CounterError from an CounterErrorKind, optionally with cause - */ - pub fn new(errtype: CounterErrorKind, cause: Option>) - -> CounterError - { - CounterError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this CounterError - */ - pub fn err_type(&self) -> CounterErrorKind { - self.err_type - } - -} - -impl Display for CounterError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for CounterError { - - 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!(CounterError, CounterErrorKind, + StoreReadError => "Store read error", + StoreWriteError => "Store write error", + HeaderTypeError => "Header type error", + HeaderFieldMissingError => "Header field missing error" +); diff --git a/libimagcounter/src/lib.rs b/libimagcounter/src/lib.rs index 77033170..0426384d 100644 --- a/libimagcounter/src/lib.rs +++ b/libimagcounter/src/lib.rs @@ -17,6 +17,7 @@ extern crate toml; #[macro_use] extern crate semver; #[macro_use] extern crate libimagstore; +#[macro_use] extern crate libimagerror; module_entry_path_mod!("counter", "0.1.0"); From 6850146e4286df3529e291eec129b92ebf6a02fb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 09/15] libimagentrylink: Replace error code with code generator macro --- libimagentrylink/Cargo.toml | 3 ++ libimagentrylink/src/error.rs | 95 ++++------------------------------- libimagentrylink/src/lib.rs | 1 + 3 files changed, 14 insertions(+), 85 deletions(-) diff --git a/libimagentrylink/Cargo.toml b/libimagentrylink/Cargo.toml index 7ac8457e..f14869be 100644 --- a/libimagentrylink/Cargo.toml +++ b/libimagentrylink/Cargo.toml @@ -14,3 +14,6 @@ rust-crypto = "0.2.35" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagentrylink/src/error.rs b/libimagentrylink/src/error.rs index a26e3d58..b587b0ef 100644 --- a/libimagentrylink/src/error.rs +++ b/libimagentrylink/src/error.rs @@ -2,89 +2,14 @@ use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum LinkErrorKind { - EntryHeaderReadError, - EntryHeaderWriteError, - ExistingLinkTypeWrong, - LinkTargetDoesNotExist, - InternalConversionError, - InvalidUri, - StoreReadError, - StoreWriteError, -} +generate_error_types!(LinkError, LinkErrorKind, + EntryHeaderReadError => "Error while reading an entry header", + EntryHeaderWriteError => "Error while writing an entry header", + ExistingLinkTypeWrong => "Existing link entry has wrong type", + LinkTargetDoesNotExist => "Link target does not exist in the store", + InternalConversionError => "Error while converting values internally", + InvalidUri => "URI is not valid", + StoreReadError => "Store read error", + StoreWriteError => "Store write error" +); -fn link_error_type_as_str(e: &LinkErrorKind) -> &'static str { - match *e { - LinkErrorKind::EntryHeaderReadError - => "Error while reading an entry header", - - LinkErrorKind::EntryHeaderWriteError - => "Error while writing an entry header", - - LinkErrorKind::ExistingLinkTypeWrong - => "Existing link entry has wrong type", - - LinkErrorKind::LinkTargetDoesNotExist - => "Link target does not exist in the store", - - LinkErrorKind::InternalConversionError - => "Error while converting values internally", - - LinkErrorKind::InvalidUri - => "URI is not valid", - - LinkErrorKind::StoreReadError - => "Store read error", - - LinkErrorKind::StoreWriteError - => "Store write error", - } -} - -impl Display for LinkErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", link_error_type_as_str(self))); - Ok(()) - } - -} - -#[derive(Debug)] -pub struct LinkError { - kind: LinkErrorKind, - cause: Option>, -} - -impl LinkError { - - pub fn new(errtype: LinkErrorKind, cause: Option>) -> LinkError { - LinkError { - kind: errtype, - cause: cause, - } - } - -} - -impl Display for LinkError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", link_error_type_as_str(&self.kind))); - Ok(()) - } - -} - -impl Error for LinkError { - - fn description(&self) -> &str { - link_error_type_as_str(&self.kind) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} diff --git a/libimagentrylink/src/lib.rs b/libimagentrylink/src/lib.rs index 448bb853..e04b6bf4 100644 --- a/libimagentrylink/src/lib.rs +++ b/libimagentrylink/src/lib.rs @@ -20,6 +20,7 @@ extern crate url; extern crate crypto; #[macro_use] extern crate libimagstore; +#[macro_use] extern crate libimagerror; module_entry_path_mod!("links", "0.1.0"); From 20ac5247f1be9cb65fd511e4a3ce693a45dd8a8a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 10/15] libimagentrylist: Replace error code with code generator macro --- libimagentrylist/Cargo.toml | 3 ++ libimagentrylist/src/error.rs | 85 +++-------------------------------- 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>, -} - -impl ListError { - - /** - * Build a new ListError from an ListErrorKind, optionally with cause - */ - pub fn new(errtype: ListErrorKind, cause: Option>) -> 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; From 5c7412ebc6cf48b9814085b83987d6d2086c8c13 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 11/15] libimagentrytag: Replace error code with code generator macro --- libimagentrytag/Cargo.toml | 3 ++ libimagentrytag/src/error.rs | 69 ++++-------------------------------- libimagentrytag/src/lib.rs | 1 + 3 files changed, 10 insertions(+), 63 deletions(-) diff --git a/libimagentrytag/Cargo.toml b/libimagentrytag/Cargo.toml index db2df486..2a4e1ed2 100644 --- a/libimagentrytag/Cargo.toml +++ b/libimagentrytag/Cargo.toml @@ -13,3 +13,6 @@ itertools = "0.4" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagentrytag/src/error.rs b/libimagentrytag/src/error.rs index 19d4a92e..5be68b90 100644 --- a/libimagentrytag/src/error.rs +++ b/libimagentrytag/src/error.rs @@ -2,67 +2,10 @@ use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum TagErrorKind { - TagTypeError, - HeaderReadError, - HeaderWriteError, - NotATag, -} - -fn tag_error_type_as_str(e: &TagErrorKind) -> &'static str { - match *e { - TagErrorKind::TagTypeError => "Entry Header Tag Type wrong", - TagErrorKind::HeaderReadError => "Error while reading entry header", - TagErrorKind::HeaderWriteError => "Error while writing entry header", - TagErrorKind::NotATag => "String is not a tag", - } -} - -impl Display for TagErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", tag_error_type_as_str(self))); - Ok(()) - } - -} - -#[derive(Debug)] -pub struct TagError { - kind: TagErrorKind, - cause: Option>, -} - -impl TagError { - - pub fn new(errtype: TagErrorKind, cause: Option>) -> TagError { - TagError { - kind: errtype, - cause: cause, - } - } - -} - -impl Display for TagError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", tag_error_type_as_str(&self.kind))); - Ok(()) - } - -} - -impl Error for TagError { - - fn description(&self) -> &str { - tag_error_type_as_str(&self.kind) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(TagError, TagErrorKind, + TagTypeError => "Entry Header Tag Type wrong", + HeaderReadError => "Error while reading entry header", + HeaderWriteError => "Error while writing entry header", + NotATag => "String is not a tag" +); diff --git a/libimagentrytag/src/lib.rs b/libimagentrytag/src/lib.rs index d90f5fa5..232de3b3 100644 --- a/libimagentrytag/src/lib.rs +++ b/libimagentrytag/src/lib.rs @@ -19,6 +19,7 @@ extern crate regex; extern crate toml; extern crate libimagstore; +#[macro_use] extern crate libimagerror; pub mod error; pub mod exec; From ea98cc41da8e6d8ac843265f70c2e4345c42920e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 12/15] libimagentryview: Replace error code with code generator macro --- libimagentryview/Cargo.toml | 3 ++ libimagentryview/src/error.rs | 77 ++--------------------------------- libimagentryview/src/lib.rs | 1 + 3 files changed, 7 insertions(+), 74 deletions(-) diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml index e9711be0..7c0124a3 100644 --- a/libimagentryview/Cargo.toml +++ b/libimagentryview/Cargo.toml @@ -8,3 +8,6 @@ authors = ["Matthias Beyer "] [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagentryview/src/error.rs b/libimagentryview/src/error.rs index 4bc8af54..32982342 100644 --- a/libimagentryview/src/error.rs +++ b/libimagentryview/src/error.rs @@ -2,78 +2,7 @@ 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 ViewErrorKind { -} - -fn counter_error_type_as_str(e: &ViewErrorKind) -> &'static str { - match e { - _ => "", - } -} - -impl Display for ViewErrorKind { - - 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 ViewError { - err_type: ViewErrorKind, - cause: Option>, -} - -impl ViewError { - - /** - * Build a new ViewError from an ViewErrorKind, optionally with cause - */ - pub fn new(errtype: ViewErrorKind, cause: Option>) - -> ViewError - { - ViewError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this ViewError - */ - pub fn err_type(&self) -> ViewErrorKind { - self.err_type - } - -} - -impl Display for ViewError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for ViewError { - - 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!(ViewError, ViewErrorKind, + Unknown => "Unknown view error" +); diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs index 73833b7d..950332c5 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -15,6 +15,7 @@ )] extern crate libimagstore; +#[macro_use] extern crate libimagerror; pub mod error; pub mod builtin; From 841d26fdd0c848359a23e3bd73b72ab73f32d868 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 13/15] libimaginteraction: Replace error code with code generator macro --- libimaginteraction/Cargo.toml | 3 ++ libimaginteraction/src/error.rs | 75 ++------------------------------- libimaginteraction/src/lib.rs | 1 + 3 files changed, 7 insertions(+), 72 deletions(-) diff --git a/libimaginteraction/Cargo.toml b/libimaginteraction/Cargo.toml index e0bea398..1ee8e63f 100644 --- a/libimaginteraction/Cargo.toml +++ b/libimaginteraction/Cargo.toml @@ -20,3 +20,6 @@ path = "../libimagentryfilter" [dependencies.libimagutil] path = "../libimagutil" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimaginteraction/src/error.rs b/libimaginteraction/src/error.rs index ed7d974e..a85c9b25 100644 --- a/libimaginteraction/src/error.rs +++ b/libimaginteraction/src/error.rs @@ -2,76 +2,7 @@ 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 InteractionErrorKind { - Unknown -} - -fn interaction_error_type_as_str(e: &InteractionErrorKind) -> &'static str { - match *e { - InteractionErrorKind::Unknown => "Unknown Error", - } -} - -impl Display for InteractionErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", interaction_error_type_as_str(self))); - Ok(()) - } - -} - -#[derive(Debug)] -pub struct InteractionError { - err_type: InteractionErrorKind, - cause: Option>, -} - -impl InteractionError { - - /** - * Build a new InteractionError from an InteractionErrorKind, optionally with cause - */ - pub fn new(errtype: InteractionErrorKind, cause: Option>) - -> InteractionError - { - InteractionError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this InteractionError - */ - pub fn err_type(&self) -> InteractionErrorKind { - self.err_type - } - -} - -impl Display for InteractionError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", interaction_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for InteractionError { - - fn description(&self) -> &str { - interaction_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(InteractionError, InteractionErrorKind, + Unknown => "Unknown Error" +); diff --git a/libimaginteraction/src/lib.rs b/libimaginteraction/src/lib.rs index ff0fff06..3600b942 100644 --- a/libimaginteraction/src/lib.rs +++ b/libimaginteraction/src/lib.rs @@ -8,6 +8,7 @@ extern crate regex; extern crate libimagentryfilter; extern crate libimagstore; #[macro_use] extern crate libimagutil; +#[macro_use] extern crate libimagerror; pub mod ask; pub mod error; From c730cce83baeb49cb90346456ccc2a09db675d75 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 14/15] libimagnotes: Replace error code with code generator macro --- libimagnotes/Cargo.toml | 3 ++ libimagnotes/src/error.rs | 86 +++------------------------------------ libimagnotes/src/lib.rs | 1 + 3 files changed, 10 insertions(+), 80 deletions(-) diff --git a/libimagnotes/Cargo.toml b/libimagnotes/Cargo.toml index 6bc99cee..f13cf218 100644 --- a/libimagnotes/Cargo.toml +++ b/libimagnotes/Cargo.toml @@ -11,6 +11,9 @@ toml = "0.1.25" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagerror] +path = "../libimagerror" + [dependencies.libimagrt] path = "../libimagrt" diff --git a/libimagnotes/src/error.rs b/libimagnotes/src/error.rs index ec1f5535..527aa710 100644 --- a/libimagnotes/src/error.rs +++ b/libimagnotes/src/error.rs @@ -2,84 +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 NoteErrorKind { - StoreWriteError, - StoreReadError, - HeaderTypeError, - NoteToEntryConversion, - // Nothing here yet -} - -fn note_error_type_as_str(e: &NoteErrorKind) -> &'static str { - match *e { - NoteErrorKind::StoreWriteError => "Error writing store", - NoteErrorKind::StoreReadError => "Error reading store", - NoteErrorKind::HeaderTypeError => "Header type error", - NoteErrorKind::NoteToEntryConversion => "Error converting Note instance to Entry instance", - } -} - -impl Display for NoteErrorKind { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", note_error_type_as_str(self))); - Ok(()) - } - -} - -/** - * Store error type - */ -#[derive(Debug)] -pub struct NoteError { - err_type: NoteErrorKind, - cause: Option>, -} - -impl NoteError { - - /** - * Build a new NoteError from an NoteErrorKind, optionally with cause - */ - pub fn new(errtype: NoteErrorKind, cause: Option>) -> NoteError { - NoteError { - err_type: errtype, - cause: cause, - } - } - - /** - * Get the error type of this NoteError - */ - pub fn err_type(&self) -> NoteErrorKind { - self.err_type - } - -} - -impl Display for NoteError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", note_error_type_as_str(&self.err_type))); - Ok(()) - } - -} - -impl Error for NoteError { - - fn description(&self) -> &str { - note_error_type_as_str(&self.err_type) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(NoteError, NoteErrorKind, + StoreWriteError => "Error writing store", + StoreReadError => "Error reading store", + HeaderTypeError => "Header type error", + NoteToEntryConversion => "Error converting Note instance to Entry instance" +); diff --git a/libimagnotes/src/lib.rs b/libimagnotes/src/lib.rs index 64ef75a2..ee43c9a3 100644 --- a/libimagnotes/src/lib.rs +++ b/libimagnotes/src/lib.rs @@ -20,6 +20,7 @@ extern crate toml; extern crate libimagrt; #[macro_use] extern crate libimagstore; +#[macro_use] extern crate libimagerror; extern crate libimagentrytag; module_entry_path_mod!("notes", "0.1.0"); From f27b114f850b94b1826faf9e07303ad9c7b96b9b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH 15/15] libimagrt: Replace error code with code generator macro --- libimagrt/Cargo.toml | 3 +++ libimagrt/src/error.rs | 59 ++++-------------------------------------- libimagrt/src/lib.rs | 1 + 3 files changed, 9 insertions(+), 54 deletions(-) diff --git a/libimagrt/Cargo.toml b/libimagrt/Cargo.toml index 485994b5..89610457 100644 --- a/libimagrt/Cargo.toml +++ b/libimagrt/Cargo.toml @@ -21,3 +21,6 @@ path = "../libimagstorestdhook" [dependencies.libimagutil] path = "../libimagutil" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/libimagrt/src/error.rs b/libimagrt/src/error.rs index ca343ede..93752552 100644 --- a/libimagrt/src/error.rs +++ b/libimagrt/src/error.rs @@ -4,60 +4,11 @@ use std::fmt::Formatter; use std::fmt::Error as FmtError; use std::io::Error as IOError; -#[derive(Debug, PartialEq, Clone, Copy)] -pub enum RuntimeErrorKind { - Instantiate, - IOError, - ProcessExitFailure, - - // more? -} - -#[derive(Debug)] -pub struct RuntimeError { - kind: RuntimeErrorKind, - cause: Option>, -} - -impl RuntimeError { - - pub fn new(kind: RuntimeErrorKind, cause: Option>) -> RuntimeError { - RuntimeError { - kind: kind, - cause: cause, - } - } - -} - -fn runtime_error_kind_as_str(e: &RuntimeErrorKind) -> &'static str { - match *e { - RuntimeErrorKind::Instantiate => "Could not instantiate", - RuntimeErrorKind::IOError => "IO Error", - RuntimeErrorKind::ProcessExitFailure => "Process exited with failure", - } -} - -impl Display for RuntimeError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", runtime_error_kind_as_str(&self.kind))); - Ok(()) - } - -} - -impl Error for RuntimeError { - - fn description(&self) -> &str { - runtime_error_kind_as_str(&self.kind) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - -} +generate_error_types!(RuntimeError, RuntimeErrorKind, + Instantiate => "Could not instantiate", + IOError => "IO Error", + ProcessExitFailure => "Process exited with failure" +); impl From for RuntimeError { diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index 7c9e3f59..ef0874e2 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -26,6 +26,7 @@ extern crate toml; extern crate libimagstore; extern crate libimagstorestdhook; extern crate libimagutil; +#[macro_use] extern crate libimagerror; mod configuration; mod logger;