Use IntoError trait for less ugly code

This commit is contained in:
Matthias Beyer 2016-05-27 10:27:06 +02:00
parent 70b6a4f587
commit e3db21b009
4 changed files with 14 additions and 13 deletions

View file

@ -182,6 +182,6 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
.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())
}

View file

@ -7,6 +7,8 @@ use error::RuntimeErrorKind;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Entry;
use libimagerror::into::IntoError;
pub type EditResult<T> = Result<T, RuntimeError>;
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())
}
}

View file

@ -10,7 +10,7 @@ generate_error_types!(RuntimeError, RuntimeErrorKind,
impl From<IOError> for RuntimeError {
fn from(ioe: IOError) -> RuntimeError {
RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(ioe)))
RuntimeErrorKind::IOError.into_error_with_cause(Box::new(ioe))
}
}

View file

@ -37,13 +37,13 @@ impl<'a> Runtime<'a> {
*/
pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, 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<Box<Error>> = 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))
}
/**