imag-view: Replace error code with code generator macro
This commit is contained in:
parent
60bf3c68a7
commit
9140c36301
3 changed files with 10 additions and 81 deletions
|
@ -21,3 +21,6 @@ path = "../libimagrt"
|
|||
[dependencies.libimagutil]
|
||||
path = "../libimagutil"
|
||||
|
||||
[dependencies.libimagerror]
|
||||
path = "../libimagerror"
|
||||
|
||||
|
|
|
@ -2,85 +2,10 @@ use std::error::Error;
|
|||
use std::fmt::Error as FmtError;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
/**
|
||||
* Kind of store error
|
||||
*/
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum ViewErrorKind {
|
||||
StoreError,
|
||||
NoVersion,
|
||||
PatternError,
|
||||
GlobBuildError,
|
||||
}
|
||||
|
||||
fn view_error_type_as_str(e: &ViewErrorKind) -> &'static str {
|
||||
match *e {
|
||||
ViewErrorKind::StoreError => "Store error",
|
||||
ViewErrorKind::NoVersion => "No version specified",
|
||||
ViewErrorKind::PatternError => "Error in Pattern",
|
||||
ViewErrorKind::GlobBuildError => "Could not build glob() Argument",
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ViewErrorKind {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "{}", view_error_type_as_str(self)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* View error type
|
||||
*/
|
||||
#[derive(Debug)]
|
||||
pub struct ViewError {
|
||||
err_type: ViewErrorKind,
|
||||
cause: Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl ViewError {
|
||||
|
||||
/**
|
||||
* Build a new ViewError from an ViewErrorKind, optionally with cause
|
||||
*/
|
||||
pub fn new(errtype: ViewErrorKind, cause: Option<Box<Error>>)
|
||||
-> ViewError
|
||||
{
|
||||
ViewError {
|
||||
err_type: errtype,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error type of this ViewError
|
||||
*/
|
||||
pub fn err_type(&self) -> ViewErrorKind {
|
||||
self.err_type
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for ViewError {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Error for ViewError {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
view_error_type_as_str(&self.err_type)
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
|
||||
}
|
||||
generate_error_types!(ViewError, ViewErrorKind,
|
||||
StoreError => "Store error",
|
||||
NoVersion => "No version specified",
|
||||
PatternError => "Error in Pattern",
|
||||
GlobBuildError => "Could not build glob() Argument"
|
||||
);
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ extern crate toml;
|
|||
extern crate libimagrt;
|
||||
extern crate libimagstore;
|
||||
extern crate libimagutil;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
use std::result::Result as RResult;
|
||||
use std::process::exit;
|
||||
|
|
Loading…
Reference in a new issue