diff --git a/imag-store/src/error.rs b/imag-store/src/error.rs index 472070f2..ae9162f0 100644 --- a/imag-store/src/error.rs +++ b/imag-store/src/error.rs @@ -1,9 +1,10 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(StoreError, StoreErrorKind, - BackendError => "Backend Error", - NoCommandlineCall => "No commandline call" +generate_error_module!( + generate_error_types!(StoreError, StoreErrorKind, + BackendError => "Backend Error", + NoCommandlineCall => "No commandline call" + ); ); +pub use self::error::StoreError; +pub use self::error::StoreErrorKind; + diff --git a/imag-view/src/error.rs b/imag-view/src/error.rs index 64aa071d..77c7b95d 100644 --- a/imag-view/src/error.rs +++ b/imag-view/src/error.rs @@ -1,11 +1,12 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(ViewError, ViewErrorKind, - StoreError => "Store error", - NoVersion => "No version specified", - PatternError => "Error in Pattern", - GlobBuildError => "Could not build glob() Argument" +generate_error_module!( + generate_error_types!(ViewError, ViewErrorKind, + StoreError => "Store error", + NoVersion => "No version specified", + PatternError => "Error in Pattern", + GlobBuildError => "Could not build glob() Argument" + ); ); +pub use self::error::ViewError; +pub use self::error::ViewErrorKind; + diff --git a/libimagcounter/src/error.rs b/libimagcounter/src/error.rs index f2e11eeb..e8016fb6 100644 --- a/libimagcounter/src/error.rs +++ b/libimagcounter/src/error.rs @@ -1,11 +1,12 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(CounterError, CounterErrorKind, - StoreReadError => "Store read error", - StoreWriteError => "Store write error", - HeaderTypeError => "Header type error", - HeaderFieldMissingError => "Header field missing error" +generate_error_module!( + generate_error_types!(CounterError, CounterErrorKind, + StoreReadError => "Store read error", + StoreWriteError => "Store write error", + HeaderTypeError => "Header type error", + HeaderFieldMissingError => "Header field missing error" + ); ); +pub use self::error::CounterError; +pub use self::error::CounterErrorKind; + diff --git a/libimagentrylink/src/error.rs b/libimagentrylink/src/error.rs index b587b0ef..2802c414 100644 --- a/libimagentrylink/src/error.rs +++ b/libimagentrylink/src/error.rs @@ -1,15 +1,16 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -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" +generate_error_module!( + 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" + ); ); +pub use self::error::LinkError; +pub use self::error::LinkErrorKind; + diff --git a/libimagentrylist/src/error.rs b/libimagentrylist/src/error.rs index 15640042..2cd5cde5 100644 --- a/libimagentrylist/src/error.rs +++ b/libimagentrylist/src/error.rs @@ -1,11 +1,12 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(ListError, ListErrorKind, - FormatError => "FormatError", - EntryError => "EntryError", - IterationError => "IterationError", - CLIError => "No CLI subcommand for listing entries" +generate_error_module!( + generate_error_types!(ListError, ListErrorKind, + FormatError => "FormatError", + EntryError => "EntryError", + IterationError => "IterationError", + CLIError => "No CLI subcommand for listing entries" + ); ); +pub use self::error::ListError; +pub use self::error::ListErrorKind; + diff --git a/libimagentrytag/src/error.rs b/libimagentrytag/src/error.rs index 5be68b90..45895019 100644 --- a/libimagentrytag/src/error.rs +++ b/libimagentrytag/src/error.rs @@ -1,11 +1,12 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -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" +generate_error_module!( + 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" + ); ); +pub use self::error::TagError; +pub use self::error::TagErrorKind; + diff --git a/libimagentryview/src/error.rs b/libimagentryview/src/error.rs index 32982342..e54d2fcc 100644 --- a/libimagentryview/src/error.rs +++ b/libimagentryview/src/error.rs @@ -1,8 +1,9 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(ViewError, ViewErrorKind, - Unknown => "Unknown view error" +generate_error_module!( + generate_error_types!(ViewError, ViewErrorKind, + Unknown => "Unknown view error" + ); ); +pub use self::error::ViewError; +pub use self::error::ViewErrorKind; + diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index 471c6362..2f22078a 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -1,3 +1,22 @@ +#[macro_export] +macro_rules! generate_error_imports { + () => { + use std::error::Error; + use std::fmt::Error as FmtError; + use std::fmt::{Display, Formatter}; + } +} + +#[macro_export] +macro_rules! generate_error_module { + ( $exprs:item ) => { + pub mod error { + generate_error_imports!(); + $exprs + } + } +} + #[macro_export] macro_rules! generate_error_types { ( @@ -71,16 +90,17 @@ macro_rules! generate_error_types { #[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"); + generate_error_module!( + generate_error_types!(TestError, TestErrorKind, + TestErrorKindA => "testerrorkind a", + TestErrorKindB => "testerrorkind B"); + ); #[test] fn test_a() { + use self::error::{TestError, TestErrorKind}; + let kind = TestErrorKind::TestErrorKindA; assert_eq!(String::from("testerrorkind a"), format!("{}", kind)); @@ -90,6 +110,8 @@ mod test { #[test] fn test_b() { + use self::error::{TestError, TestErrorKind}; + let kind = TestErrorKind::TestErrorKindB; assert_eq!(String::from("testerrorkind B"), format!("{}", kind)); @@ -100,6 +122,55 @@ mod test { #[test] fn test_ab() { + use std::error::Error; + use self::error::{TestError, TestErrorKind}; + + 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())); + } + + pub mod anothererrormod { + generate_error_imports!(); + generate_error_types!(TestError, TestErrorKind, + TestErrorKindA => "testerrorkind a", + TestErrorKindB => "testerrorkind B"); + } + + #[test] + fn test_other_a() { + use self::anothererrormod::{TestError, TestErrorKind}; + + 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_other_b() { + use self::anothererrormod::{TestError, TestErrorKind}; + + 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_other_ab() { + use std::error::Error; + use self::anothererrormod::{TestError, TestErrorKind}; + let kinda = TestErrorKind::TestErrorKindA; let kindb = TestErrorKind::TestErrorKindB; assert_eq!(String::from("testerrorkind a"), format!("{}", kinda)); diff --git a/libimaginteraction/src/error.rs b/libimaginteraction/src/error.rs index a85c9b25..9285f0fd 100644 --- a/libimaginteraction/src/error.rs +++ b/libimaginteraction/src/error.rs @@ -1,8 +1,9 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -generate_error_types!(InteractionError, InteractionErrorKind, - Unknown => "Unknown Error" +generate_error_module!( + generate_error_types!(InteractionError, InteractionErrorKind, + Unknown => "Unknown Error" + ); ); +pub use self::error::InteractionError; +pub use self::error::InteractionErrorKind; + diff --git a/libimagnotes/src/error.rs b/libimagnotes/src/error.rs index 527aa710..bbb02d0c 100644 --- a/libimagnotes/src/error.rs +++ b/libimagnotes/src/error.rs @@ -1,11 +1,12 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; - -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" +generate_error_module!( + 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" + ); ); +pub use self::error::NoteError; +pub use self::error::NoteErrorKind; + diff --git a/libimagrt/src/error.rs b/libimagrt/src/error.rs index 93752552..50d640d7 100644 --- a/libimagrt/src/error.rs +++ b/libimagrt/src/error.rs @@ -1,7 +1,4 @@ -use std::error::Error; -use std::fmt::Display; -use std::fmt::Formatter; -use std::fmt::Error as FmtError; +generate_error_imports!(); use std::io::Error as IOError; generate_error_types!(RuntimeError, RuntimeErrorKind, diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index ad36963e..3e32bf96 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -1,6 +1,4 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; +generate_error_imports!(); use std::convert::From; generate_error_types!(StoreError, StoreErrorKind, diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs index ec06dd98..e55c2b8f 100644 --- a/libimagstore/src/hook/error.rs +++ b/libimagstore/src/hook/error.rs @@ -1,6 +1,4 @@ -use std::error::Error; -use std::fmt::Error as FmtError; -use std::fmt::{Display, Formatter}; +generate_error_imports!(); use std::convert::Into; generate_error_types!(HookError, HookErrorKind,