libimagrt: Replace error code with code generator macro
This commit is contained in:
parent
c730cce83b
commit
f27b114f85
3 changed files with 9 additions and 54 deletions
|
@ -21,3 +21,6 @@ path = "../libimagstorestdhook"
|
|||
[dependencies.libimagutil]
|
||||
path = "../libimagutil"
|
||||
|
||||
[dependencies.libimagerror]
|
||||
path = "../libimagerror"
|
||||
|
||||
|
|
|
@ -4,60 +4,11 @@ use std::fmt::Formatter;
|
|||
use std::fmt::Error as FmtError;
|
||||
use std::io::Error as IOError;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum RuntimeErrorKind {
|
||||
Instantiate,
|
||||
IOError,
|
||||
ProcessExitFailure,
|
||||
|
||||
// more?
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RuntimeError {
|
||||
kind: RuntimeErrorKind,
|
||||
cause: Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl RuntimeError {
|
||||
|
||||
pub fn new(kind: RuntimeErrorKind, cause: Option<Box<Error>>) -> RuntimeError {
|
||||
RuntimeError {
|
||||
kind: kind,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn runtime_error_kind_as_str(e: &RuntimeErrorKind) -> &'static str {
|
||||
match *e {
|
||||
RuntimeErrorKind::Instantiate => "Could not instantiate",
|
||||
RuntimeErrorKind::IOError => "IO Error",
|
||||
RuntimeErrorKind::ProcessExitFailure => "Process exited with failure",
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for RuntimeError {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "{}", runtime_error_kind_as_str(&self.kind)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Error for RuntimeError {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
runtime_error_kind_as_str(&self.kind)
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
|
||||
}
|
||||
generate_error_types!(RuntimeError, RuntimeErrorKind,
|
||||
Instantiate => "Could not instantiate",
|
||||
IOError => "IO Error",
|
||||
ProcessExitFailure => "Process exited with failure"
|
||||
);
|
||||
|
||||
impl From<IOError> for RuntimeError {
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ extern crate toml;
|
|||
extern crate libimagstore;
|
||||
extern crate libimagstorestdhook;
|
||||
extern crate libimagutil;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
mod configuration;
|
||||
mod logger;
|
||||
|
|
Loading…
Reference in a new issue