libimagentrylist: Replace error code with code generator macro
This commit is contained in:
parent
6850146e42
commit
20ac5247f1
3 changed files with 10 additions and 79 deletions
|
@ -11,3 +11,6 @@ toml = "0.1.25"
|
|||
[dependencies.libimagstore]
|
||||
path = "../libimagstore"
|
||||
|
||||
[dependencies.libimagerror]
|
||||
path = "../libimagerror"
|
||||
|
||||
|
|
|
@ -2,83 +2,10 @@ use std::error::Error;
|
|||
use std::fmt::Error as FmtError;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
/**
|
||||
* Kind of error
|
||||
*/
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum ListErrorKind {
|
||||
FormatError,
|
||||
EntryError,
|
||||
IterationError,
|
||||
CLIError,
|
||||
}
|
||||
|
||||
fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{
|
||||
match *err {
|
||||
ListErrorKind::FormatError => "FormatError",
|
||||
ListErrorKind::EntryError => "EntryError",
|
||||
ListErrorKind::IterationError => "IterationError",
|
||||
ListErrorKind::CLIError => "No CLI subcommand for listing entries",
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ListErrorKind {
|
||||
|
||||
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 ListError {
|
||||
err_type: ListErrorKind,
|
||||
cause: Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl ListError {
|
||||
|
||||
/**
|
||||
* Build a new ListError from an ListErrorKind, optionally with cause
|
||||
*/
|
||||
pub fn new(errtype: ListErrorKind, cause: Option<Box<Error>>) -> ListError {
|
||||
ListError {
|
||||
err_type: errtype,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error type of this ListError
|
||||
*/
|
||||
pub fn err_type(&self) -> ListErrorKind {
|
||||
self.err_type
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for ListError {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Error for ListError {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
counter_error_type_as_str(&self.err_type)
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
|
||||
}
|
||||
generate_error_types!(ListError, ListErrorKind,
|
||||
FormatError => "FormatError",
|
||||
EntryError => "EntryError",
|
||||
IterationError => "IterationError",
|
||||
CLIError => "No CLI subcommand for listing entries"
|
||||
);
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ extern crate clap;
|
|||
extern crate toml;
|
||||
|
||||
extern crate libimagstore;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
pub mod cli;
|
||||
pub mod error;
|
||||
|
|
Loading…
Reference in a new issue