diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 15b94e09..c3cb9735 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -4,77 +4,14 @@ use std::ops::Deref; use toml::{Parser, Value}; -/** - * Errors which are related to configuration-file loading - */ -pub mod error { - use std::error::Error; - use std::fmt::{Display, Formatter}; - use std::fmt::Error as FmtError; - - /// The kind of an error - #[derive(Clone, Debug, PartialEq)] - pub enum ConfigErrorKind { - NoConfigFileFound, - } - - /// Configuration error type - #[derive(Debug)] - pub struct ConfigError { - kind: ConfigErrorKind, - cause: Option>, - } - - impl ConfigError { - - /// Instantiate a new `ConfigError`, optionally with cause - pub fn new(kind: ConfigErrorKind, cause: Option>) -> ConfigError { - ConfigError { - kind: kind, - cause: cause, - } - } - - ///get the Kind of the Error - pub fn err_type(&self) -> ConfigErrorKind { - self.kind.clone() - } - - /// Get the string, the `ConfigError` can be described with - pub fn as_str(e: &ConfigError) -> &'static str { - match e.err_type() { - ConfigErrorKind::NoConfigFileFound => "No config file found", - } - } - - } - - impl Display for ConfigError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "{}", ConfigError::as_str(self))); - Ok(()) - } - - } - - impl Error for ConfigError { - - fn description(&self) -> &str { - ConfigError::as_str(self) - } - - fn cause(&self) -> Option<&Error> { - self.cause.as_ref().map(|e| &**e) - } - - } - -} +generate_error_module!( + generate_error_types!(ConfigError, ConfigErrorKind, + NoConfigFileFound => "No config file found" + ); +); use self::error::{ConfigError, ConfigErrorKind}; - /** * Result type of this module. Either `T` or `ConfigError` */ @@ -245,6 +182,6 @@ fn fetch_config(rtp: &PathBuf) -> Result { .filter(|loaded| loaded.is_some()) .nth(0) .map(|inner| Value::Table(inner.unwrap())) - .ok_or_else(|| ConfigError::new(ConfigErrorKind::NoConfigFileFound, None)) + .ok_or(ConfigErrorKind::NoConfigFileFound.into()) } diff --git a/libimagrt/src/edit.rs b/libimagrt/src/edit.rs index 93fa1a6b..67f495b8 100644 --- a/libimagrt/src/edit.rs +++ b/libimagrt/src/edit.rs @@ -7,6 +7,8 @@ use error::RuntimeErrorKind; use libimagstore::store::FileLockEntry; use libimagstore::store::Entry; +use libimagerror::into::IntoError; + pub type EditResult = Result; pub trait Edit { @@ -55,18 +57,19 @@ pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> { if let Some(mut editor) = rt.editor() { let exit_status = editor.arg(file_path).status(); - match exit_status.map(|s| s.success()) { + match exit_status.map(|s| s.success()).map_err(Box::new) { Ok(true) => { file.sync_data() .and_then(|_| file.seek(SeekFrom::Start(0))) .and_then(|_| file.read_to_string(s)) .map(|_| ()) - .map_err(|e| RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| RuntimeErrorKind::IOError.into_error_with_cause(e)) }, - Ok(false) => Err(RuntimeError::new(RuntimeErrorKind::ProcessExitFailure, None)), - Err(e) => Err(RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e)))), + Ok(false) => Err(RuntimeErrorKind::ProcessExitFailure.into()), + Err(e) => Err(RuntimeErrorKind::IOError.into_error_with_cause(e)), } } else { - Err(RuntimeError::new(RuntimeErrorKind::Instantiate, None)) + Err(RuntimeErrorKind::Instantiate.into()) } } diff --git a/libimagrt/src/error.rs b/libimagrt/src/error.rs index 50d640d7..e5f26baa 100644 --- a/libimagrt/src/error.rs +++ b/libimagrt/src/error.rs @@ -10,7 +10,7 @@ generate_error_types!(RuntimeError, RuntimeErrorKind, impl From for RuntimeError { fn from(ioe: IOError) -> RuntimeError { - RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(ioe))) + RuntimeErrorKind::IOError.into_error_with_cause(Box::new(ioe)) } } diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index ba520445..e693feff 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -37,13 +37,13 @@ impl<'a> Runtime<'a> { */ pub fn new(cli_spec: App<'a, 'a>) -> Result, RuntimeError> { use std::env; - use std::error::Error; use libimagstore::hook::position::HookPosition; use libimagstore::error::StoreErrorKind; use libimagstorestdhook::debug::DebugHook; use libimagerror::trace::trace_error; use libimagerror::trace::trace_error_dbg; + use libimagerror::into::IntoError; use configuration::error::ConfigErrorKind; @@ -72,8 +72,7 @@ impl<'a> Runtime<'a> { let cfg = match Configuration::new(&rtp) { Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound { - let cause : Option> = Some(Box::new(e)); - return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause)); + return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e))); } else { warn!("No config file found."); warn!("Continuing without configuration file"); @@ -128,9 +127,8 @@ impl<'a> Runtime<'a> { store: store, } }) - .map_err(|e| { - RuntimeError::new(RuntimeErrorKind::Instantiate, Some(Box::new(e))) - }) + .map_err(Box::new) + .map_err(|e| RuntimeErrorKind::Instantiate.into_error_with_cause(e)) } /**