libimagcounter: Replace error code with code generator macro

This commit is contained in:
Matthias Beyer 2016-05-15 16:53:31 +02:00
parent 9140c36301
commit 8602d5855a
3 changed files with 10 additions and 81 deletions

View file

@ -11,3 +11,6 @@ semver = "0.2"
[dependencies.libimagstore] [dependencies.libimagstore]
path = "../libimagstore" path = "../libimagstore"
[dependencies.libimagerror]
path = "../libimagerror"

View file

@ -2,85 +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};
/** generate_error_types!(CounterError, CounterErrorKind,
* Kind of error StoreReadError => "Store read error",
*/ StoreWriteError => "Store write error",
#[derive(Clone, Copy, Debug, PartialEq)] HeaderTypeError => "Header type error",
pub enum CounterErrorKind { HeaderFieldMissingError => "Header field missing error"
StoreReadError, );
StoreWriteError,
HeaderTypeError,
HeaderFieldMissingError,
}
fn counter_error_type_as_str(e: &CounterErrorKind) -> &'static str {
match *e {
CounterErrorKind::StoreReadError => "Store read error",
CounterErrorKind::StoreWriteError => "Store write error",
CounterErrorKind::HeaderTypeError => "Header type error",
CounterErrorKind::HeaderFieldMissingError => "Header field missing error",
}
}
impl Display for CounterErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", counter_error_type_as_str(self)));
Ok(())
}
}
/**
* Store error type
*/
#[derive(Debug)]
pub struct CounterError {
err_type: CounterErrorKind,
cause: Option<Box<Error>>,
}
impl CounterError {
/**
* Build a new CounterError from an CounterErrorKind, optionally with cause
*/
pub fn new(errtype: CounterErrorKind, cause: Option<Box<Error>>)
-> CounterError
{
CounterError {
err_type: errtype,
cause: cause,
}
}
/**
* Get the error type of this CounterError
*/
pub fn err_type(&self) -> CounterErrorKind {
self.err_type
}
}
impl Display for CounterError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type)));
Ok(())
}
}
impl Error for CounterError {
fn description(&self) -> &str {
counter_error_type_as_str(&self.err_type)
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|e| &**e)
}
}

View file

@ -17,6 +17,7 @@ extern crate toml;
#[macro_use] extern crate semver; #[macro_use] extern crate semver;
#[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagstore;
#[macro_use] extern crate libimagerror;
module_entry_path_mod!("counter", "0.1.0"); module_entry_path_mod!("counter", "0.1.0");