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");