libimagentrytag: Replace error code with code generator macro
This commit is contained in:
parent
20ac5247f1
commit
5c7412ebc6
3 changed files with 10 additions and 63 deletions
|
@ -13,3 +13,6 @@ itertools = "0.4"
|
|||
[dependencies.libimagstore]
|
||||
path = "../libimagstore"
|
||||
|
||||
[dependencies.libimagerror]
|
||||
path = "../libimagerror"
|
||||
|
||||
|
|
|
@ -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<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)
|
||||
}
|
||||
|
||||
}
|
||||
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"
|
||||
);
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ extern crate regex;
|
|||
extern crate toml;
|
||||
|
||||
extern crate libimagstore;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
pub mod error;
|
||||
pub mod exec;
|
||||
|
|
Loading…
Reference in a new issue