libimagentrytag: Replace error code with code generator macro

This commit is contained in:
Matthias Beyer 2016-05-15 16:53:31 +02:00
parent 20ac5247f1
commit 5c7412ebc6
3 changed files with 10 additions and 63 deletions

View file

@ -13,3 +13,6 @@ itertools = "0.4"
[dependencies.libimagstore] [dependencies.libimagstore]
path = "../libimagstore" path = "../libimagstore"
[dependencies.libimagerror]
path = "../libimagerror"

View file

@ -2,67 +2,10 @@ 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!(TagError, TagErrorKind,
pub enum TagErrorKind { TagTypeError => "Entry Header Tag Type wrong",
TagTypeError, HeaderReadError => "Error while reading entry header",
HeaderReadError, HeaderWriteError => "Error while writing entry header",
HeaderWriteError, NotATag => "String is not a tag"
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<Box<Error>>,
}
impl TagError {
pub fn new(errtype: TagErrorKind, cause: Option<Box<Error>>) -> 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)
}
}

View file

@ -19,6 +19,7 @@ extern crate regex;
extern crate toml; extern crate toml;
extern crate libimagstore; extern crate libimagstore;
#[macro_use] extern crate libimagerror;
pub mod error; pub mod error;
pub mod exec; pub mod exec;