libimagentrylink: Replace error code with code generator macro

This commit is contained in:
Matthias Beyer 2016-05-15 16:53:31 +02:00
parent 8602d5855a
commit 6850146e42
3 changed files with 14 additions and 85 deletions

View file

@ -14,3 +14,6 @@ rust-crypto = "0.2.35"
[dependencies.libimagstore] [dependencies.libimagstore]
path = "../libimagstore" path = "../libimagstore"
[dependencies.libimagerror]
path = "../libimagerror"

View file

@ -2,89 +2,14 @@ use std::error::Error;
use std::fmt::Error as FmtError; use std::fmt::Error as FmtError;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
#[derive(Clone, Copy, Debug, PartialEq)] generate_error_types!(LinkError, LinkErrorKind,
pub enum LinkErrorKind { EntryHeaderReadError => "Error while reading an entry header",
EntryHeaderReadError, EntryHeaderWriteError => "Error while writing an entry header",
EntryHeaderWriteError, ExistingLinkTypeWrong => "Existing link entry has wrong type",
ExistingLinkTypeWrong, LinkTargetDoesNotExist => "Link target does not exist in the store",
LinkTargetDoesNotExist, InternalConversionError => "Error while converting values internally",
InternalConversionError, InvalidUri => "URI is not valid",
InvalidUri, StoreReadError => "Store read error",
StoreReadError, StoreWriteError => "Store write error"
StoreWriteError, );
}
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<Box<Error>>,
}
impl LinkError {
pub fn new(errtype: LinkErrorKind, cause: Option<Box<Error>>) -> 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)
}
}

View file

@ -20,6 +20,7 @@ extern crate url;
extern crate crypto; extern crate crypto;
#[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagstore;
#[macro_use] extern crate libimagerror;
module_entry_path_mod!("links", "0.1.0"); module_entry_path_mod!("links", "0.1.0");