From 9140c36301dc8c1c6792b8806058b40406d03982 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 May 2016 16:53:31 +0200 Subject: [PATCH] imag-view: Replace error code with code generator macro --- imag-view/Cargo.toml | 3 ++ imag-view/src/error.rs | 87 +++--------------------------------------- imag-view/src/main.rs | 1 + 3 files changed, 10 insertions(+), 81 deletions(-) diff --git a/imag-view/Cargo.toml b/imag-view/Cargo.toml index b3a52460..7c7b3d55 100644 --- a/imag-view/Cargo.toml +++ b/imag-view/Cargo.toml @@ -21,3 +21,6 @@ path = "../libimagrt" [dependencies.libimagutil] path = "../libimagutil" +[dependencies.libimagerror] +path = "../libimagerror" + diff --git a/imag-view/src/error.rs b/imag-view/src/error.rs index 7a1ede78..64aa071d 100644 --- a/imag-view/src/error.rs +++ b/imag-view/src/error.rs @@ -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>, -} - -impl ViewError { - - /** - * Build a new ViewError from an ViewErrorKind, optionally with cause - */ - pub fn new(errtype: ViewErrorKind, cause: Option>) - -> 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" +); diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 100d9aab..0efc25c8 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -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;