diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-03 21:28:36 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-03 21:34:07 +0200 |
commit | 7ce44e80905cc60c656e385c7d1a4bb769f2c115 (patch) | |
tree | 9984e5d58814713c90db8624f87dbd759b2ff9be /lib | |
parent | 28d7085b2dbef61b95e41f05a276b256d66918dc (diff) |
Remove "IntoError" trait, use error_chain functionality
Diffstat (limited to 'lib')
53 files changed, 129 insertions, 497 deletions
diff --git a/lib/core/libimagerror/src/into.rs b/lib/core/libimagerror/src/into.rs deleted file mode 100644 index bc3cfb41..00000000 --- a/lib/core/libimagerror/src/into.rs +++ /dev/null @@ -1,33 +0,0 @@ -// -// imag - the personal information management suite for the commandline -// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; version -// 2.1 of the License. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -// - -use std::error::Error; - -/// Trait to help converting Error kinds into Error instances -pub trait IntoError { - type Target: Error; - - /// Convert the type into an error with no cause - fn into_error(self) -> Self::Target; - - /// Convert the type into an error with cause - fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target; - -} - diff --git a/lib/core/libimagerror/src/iter.rs b/lib/core/libimagerror/src/iter.rs index 0297bc6a..3de78922 100644 --- a/lib/core/libimagerror/src/iter.rs +++ b/lib/core/libimagerror/src/iter.rs @@ -168,10 +168,10 @@ impl<I, T, E> TraceIterator<T, E> for I where #[cfg(test)] mod test { use super::TraceIterator; - + #[derive(Copy, Clone, Eq, PartialEq, Debug)] struct TestError(i32); - + #[test] fn test_unwrap_with() { let original = vec![Ok(1), Err(TestError(2)), Ok(3), Err(TestError(4))]; diff --git a/lib/core/libimagerror/src/lib.rs b/lib/core/libimagerror/src/lib.rs index df0534ba..73d6ed70 100644 --- a/lib/core/libimagerror/src/lib.rs +++ b/lib/core/libimagerror/src/lib.rs @@ -36,6 +36,5 @@ #[macro_use] extern crate log; extern crate ansi_term; -pub mod into; pub mod trace; pub mod iter; diff --git a/lib/core/libimagrt/src/configuration.rs b/lib/core/libimagrt/src/configuration.rs index 3274e5c4..87eb86c8 100644 --- a/lib/core/libimagrt/src/configuration.rs +++ b/lib/core/libimagrt/src/configuration.rs @@ -23,8 +23,6 @@ use std::ops::Deref; use toml::Value; use clap::App; -use std::error::Error; - error_chain! { types { ConfigError, ConfigErrorKind, ResultExt, Result; @@ -57,20 +55,8 @@ error_chain! { } } } - -use libimagerror::into::IntoError; - -impl IntoError for ConfigErrorKind { - type Target = ConfigError; - - fn into_error(self) -> Self::Target { - ConfigError::from_kind(self) - } - - fn into_error_with_cause(self, _: Box<Error>) -> Self::Target { - ConfigError::from_kind(self) - } -} +use self::ConfigErrorKind as CEK; +use self::ConfigError as CE; /// `Configuration` object /// @@ -160,8 +146,6 @@ impl Configuration { pub fn override_config(&mut self, v: Vec<String>) -> Result<()> { use libimagutil::key_value_split::*; use libimagutil::iter::*; - use self::ConfigErrorKind as CEK; - use libimagerror::into::IntoError; use toml_query::read::TomlValueReadExt; @@ -184,14 +168,13 @@ impl Configuration { info!("Successfully overridden: {} = {}", k, v); Ok(v) }, - None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()), + None => Err(CE::from_kind(CEK::ConfigOverrideTypeNotMatching)), }, - None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), + None => Err(CE::from_kind(CEK::ConfigOverrideKeyNotAvailable)), }) ) .fold_result(|i| i) - .map_err(Box::new) - .map_err(|e| CEK::ConfigOverrideError.into_error_with_cause(e)) + .chain_err(|| CEK::ConfigOverrideError) } } @@ -311,13 +294,13 @@ fn fetch_config(searchpath: &PathBuf) -> Result<Value> { .unwrap_or_else(|| String::from("Line unknown, Column unknown")); let _ = write!(stderr(), "Config file parser error at {}", line_col); - trace_error(&ConfigErrorKind::TOMLParserError.into_error_with_cause(Box::new(e))); + trace_error(&e); None } } }) .nth(0) - .ok_or(ConfigErrorKind::NoConfigFileFound.into()) + .ok_or(CE::from_kind(ConfigErrorKind::NoConfigFileFound)) } pub trait InternalConfiguration { diff --git a/lib/core/libimagstore/src/error.rs b/lib/core/libimagstore/src/error.rs index df20ac53..2f172e34 100644 --- a/lib/core/libimagstore/src/error.rs +++ b/lib/core/libimagstore/src/error.rs @@ -17,9 +17,6 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use std::error::Error; -use libimagerror::into::IntoError; - error_chain! { types { StoreError, StoreErrorKind, ResultExt, Result; @@ -301,14 +298,3 @@ error_chain! { } } -impl IntoError for StoreErrorKind { - type Target = StoreError; - - fn into_error(self) -> Self::Target { - StoreError::from_kind(self) - } - - fn into_error_with_cause(self, _: Box<Error>) -> Self::Target { - StoreError::from_kind(self) - } -} diff --git a/lib/core/libimagstore/src/file_abstraction/inmemory.rs b/lib/core/libimagstore/src/file_abstraction/inmemory.rs index c1333aca..42ebf319 100644 --- a/lib/core/libimagstore/src/file_abstraction/inmemory.rs +++ b/lib/core/libimagstore/src/file_abstraction/inmemory.rs @@ -33,8 +33,6 @@ use super::Drain; use store::Entry; use storeid::StoreId; -use libimagerror::into::IntoError; - type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Entry>>>>; /// `FileAbstraction` type, this is the Test version! @@ -67,12 +65,12 @@ impl FileAbstractionInstance for InMemoryFileAbstractionInstance { self.fs_abstraction .lock() - .map_err(|_| SEK::LockError.into_error()) + .map_err(|_| SE::from_kind(SEK::LockError)) .and_then(|mut mtx| { mtx.get_mut() .get(&self.absent_path) .cloned() - .ok_or(SEK::FileNotFound.into_error()) + .ok_or(SE::from_kind(SEK::FileNotFound)) }) } @@ -108,7 +106,7 @@ impl InMemoryFileAbstraction { fn backend_cloned<'a>(&'a self) -> Result<HashMap<PathBuf, Entry>, SE> { self.virtual_filesystem .lock() - .map_err(|_| SEK::LockError.into_error()) + .map_err(|_| SE::from_kind(SEK::LockError)) .map(|mtx| mtx.deref().borrow().clone()) } @@ -124,7 +122,7 @@ impl FileAbstraction for InMemoryFileAbstraction { .get_mut() .remove(path) .map(|_| ()) - .ok_or(SEK::FileNotFound.into_error()) + .ok_or(SE::from_kind(SEK::FileNotFound)) } fn copy(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> { @@ -132,7 +130,7 @@ impl FileAbstraction for InMemoryFileAbstraction { let mut mtx = self.backend().lock().expect("Locking Mutex failed"); let backend = mtx.get_mut(); - let a = try!(backend.get(from).cloned().ok_or(SEK::FileNotFound.into_error())); + let a = try!(backend.get(from).cloned().ok_or(SE::from_kind(SEK::FileNotFound))); backend.insert(to.clone(), a); debug!("Copying: {:?} -> {:?} worked", from, to); Ok(()) @@ -143,7 +141,7 @@ impl FileAbstraction for InMemoryFileAbstraction { let mut mtx = self.backend().lock().expect("Locking Mutex failed"); let backend = mtx.get_mut(); - let a = try!(backend.get(from).cloned().ok_or(SEK::FileNotFound.into_error())); + let a = try!(backend.get(from).cloned().ok_or(SE::from_kind(SEK::FileNotFound))); backend.insert(to.clone(), a); debug!("Renaming: {:?} -> {:?} worked", from, to); Ok(()) diff --git a/lib/core/libimagstore/src/file_abstraction/stdio/mod.rs b/lib/core/libimagstore/src/file_abstraction/stdio/mod.rs index 9fbb8413..4ed0b067 100644 --- a/lib/core/libimagstore/src/file_abstraction/stdio/mod.rs +++ b/lib/core/libimagstore/src/file_abstraction/stdio/mod.rs @@ -37,8 +37,6 @@ use super::Drain; use super::InMemoryFileAbstraction; use store::Entry; -use libimagerror::into::IntoError; - pub mod mapper; pub mod out; use self::mapper::Mapper; @@ -60,7 +58,7 @@ impl<W, M> StdIoFileAbstraction<W, M> let _ = try!(out .backend() .lock() - .map_err(|_| SEK::LockError.into_error()) + .map_err(|_| SE::from_kind(SEK::LockError)) .map(|mut mtx| out.mapper().read_to_fs(in_stream, mtx.get_mut()))); Ok(StdIoFileAbstraction(out)) diff --git a/lib/core/libimagstore/src/file_abstraction/stdio/out.rs b/lib/core/libimagstore/src/file_abstraction/stdio/out.rs index 48f2ff72..950947f2 100644 --- a/lib/core/libimagstore/src/file_abstraction/stdio/out.rs +++ b/lib/core/libimagstore/src/file_abstraction/stdio/out.rs @@ -32,7 +32,6 @@ use std::sync::Mutex; use std::ops::Deref; use libimagerror::trace::*; -use libimagerror::into::IntoError; use error::StoreErrorKind as SEK; use error::StoreError as SE; @@ -74,7 +73,7 @@ impl<W, M> StdoutFileAbstraction<W, M> self.mem .backend() .lock() - .map_err(|_| SEK::LockError.into_error()) + .map_err(|_| SE::from_kind(SEK::LockError)) .map(|mtx| mtx.deref().borrow().clone()) } @@ -142,7 +141,7 @@ impl<W: Write, M: Mapper> FileAbstraction for StdoutFileAbstraction<W, M> { fn fill(&mut self, mut d: Drain) -> Result<(), SE> { debug!("Draining into : {:?}", self); - let mut mtx = try!(self.backend().lock().map_err(|_| SEK::IoError.into_error())); + let mut mtx = try!(self.backend().lock().map_err(|_| SE::from_kind(SEK::IoError))); let backend = mtx.get_mut(); for (path, element) in d.iter() { diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index abcdbcd5..fed4ca0b 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -49,7 +49,6 @@ pub use file_abstraction::FSFileAbstraction; pub use file_abstraction::InMemoryFileAbstraction; use libimagerror::trace::trace_error; -use libimagerror::into::IntoError; use libimagutil::debug_result::*; use self::glob_store_iter::*; @@ -274,7 +273,7 @@ impl Store { warn!("Implicitely creating store directory is denied"); warn!(" -> Either because configuration does not allow it"); warn!(" -> or because there is no configuration"); - return Err(SEK::CreateStoreDirDenied.into_error()) + return Err(SE::from_kind(SEK::CreateStoreDirDenied)) .chain_err(|| SEK::FileError) .chain_err(|| SEK::IoError); } @@ -284,7 +283,7 @@ impl Store { .map_dbg_err_str("Failed")); } else if location.is_file() { debug!("Store path exists as file"); - return Err(SEK::StorePathExists.into_error()); + return Err(SE::from_kind(SEK::StorePathExists)); } let store = Store { @@ -408,13 +407,13 @@ impl Store { { let mut hsmap = match self.entries.write() { - Err(_) => return Err(SEK::LockPoisoned.into_error()).chain_err(|| SEK::CreateCallError), + Err(_) => return Err(SE::from_kind(SEK::LockPoisoned)).chain_err(|| SEK::CreateCallError), Ok(s) => s, }; if hsmap.contains_key(&id) { debug!("Cannot create, internal cache already contains: '{}'", id); - return Err(SEK::EntryAlreadyExists.into_error()).chain_err(|| SEK::CreateCallError); + return Err(SE::from_kind(SEK::EntryAlreadyExists)).chain_err(|| SEK::CreateCallError); } hsmap.insert(id.clone(), { debug!("Creating: '{}'", id); @@ -637,7 +636,7 @@ impl Store { // if the entry is currently modified by the user, we cannot drop it match entries.get(&id) { None => { - return Err(SEK::FileNotFound.into_error()).chain_err(|| SEK::DeleteCallError) + return Err(SE::from_kind(SEK::FileNotFound)).chain_err(|| SEK::DeleteCallError) }, Some(e) => if e.is_borrowed() { return Err(SE::from_kind(SEK::IdLocked)).chain_err(|| SEK::DeleteCallError) @@ -678,12 +677,12 @@ impl Store { let hsmap = try!( self.entries .write() - .map_err(|_| SEK::LockPoisoned.into_error()) + .map_err(|_| SE::from_kind(SEK::LockPoisoned)) .chain_err(|| SEK::MoveCallError) ); if hsmap.contains_key(&new_id) { - return Err(SEK::EntryAlreadyExists.into_error()).chain_err(|| SEK::MoveCallError) + return Err(SE::from_kind(SEK::EntryAlreadyExists)).chain_err(|| SEK::MoveCallError) } let old_id = entry.get_location().clone(); diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index b26eb4ac..50dd0bc7 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -31,8 +31,6 @@ use error::StoreError as SE; use error::ResultExt; use store::Result; -use libimagerror::into::IntoError; - /// The Index into the Store #[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)] pub struct StoreId { @@ -109,7 +107,7 @@ impl StoreId { .unwrap_or_else(|| self.id.clone()) .to_str() .map(String::from) - .ok_or(SEK::StoreIdHandlingError.into_error()) + .ok_or(SE::from_kind(SEK::StoreIdHandlingError)) } /// Returns the components of the `id` part of the StoreId object. diff --git a/lib/domain/libimagbookmark/src/collection.rs b/lib/domain/libimagbookmark/src/collection.rs index 6a8e9367..525500ff 100644 --- a/lib/domain/libimagbookmark/src/collection.rs +++ b/lib/domain/libimagbookmark/src/collection.rs @@ -29,6 +29,7 @@ use std::ops::DerefMut; use regex::Regex; use error::BookmarkErrorKind as BEK; +use error::BookmarkError as BE; use error::ResultExt; use result::Result; use module_path::ModuleEntryPath; @@ -40,7 +41,6 @@ use libimagentrylink::external::ExternalLinker; use libimagentrylink::external::iter::UrlIter; use libimagentrylink::internal::InternalLinker; use libimagentrylink::internal::Link as StoreLink; -use libimagerror::into::IntoError; use link::Link; @@ -91,7 +91,7 @@ impl<'a> BookmarkCollection<'a> { .chain_err(|| BEK::StoreReadError) .and_then(|fle| { match fle { - None => Err(BEK::CollectionNotFound.into_error()), + None => Err(BE::from_kind(BEK::CollectionNotFound)), Some(e) => Ok(BookmarkCollection { fle: e, store: store, diff --git a/lib/domain/libimagbookmark/src/error.rs b/lib/domain/libimagbookmark/src/error.rs index 34b5735d..0001e76a 100644 --- a/lib/domain/libimagbookmark/src/error.rs +++ b/lib/domain/libimagbookmark/src/error.rs @@ -17,10 +17,6 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use std::error::Error; - -use libimagerror::into::IntoError; - error_chain! { types { BookmarkError, BookmarkErrorKind, ResultExt, Result; @@ -55,14 +51,3 @@ error_chain! { } } -impl IntoError for BookmarkErrorKind { - type Target = BookmarkError; - - fn into_error(self) -> Self::Target { - BookmarkError::from_kind(self) - } - - fn into_error_with_cause(self, _: Box<Error>) -> Self::Target { - BookmarkError::from_kind(self) - } -} diff --git a/lib/domain/libimagcounter/src/counter.rs b/lib/domain/libimagcounter/src/counter.rs index e23a18df..f58e5bb5 100644 --- a/lib/domain/libimagcounter/src/counter.rs +++ b/lib/domain/libimagcounter/src/counter.rs @@ -32,7 +32,6 @@ use libimagstore::storeid::StoreIdIterator; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; -use libimagerror::into::IntoError; use module_path::ModuleEntryPath; use result::Result; @@ -79,17 +78,17 @@ impl<'a> Counter<'a> { let header = entry.get_header_mut(); let setres = header.set(&String::from("counter"), Value::Table(BTreeMap::new())); if setres.is_err() { - return Err(CEK::StoreWriteError.into_error()); + return Err(CE::from_kind(CEK::StoreWriteError)); } let setres = header.set(&String::from("counter.name"), Value::String(name)); if setres.is_err() { - return Err(CEK::StoreWriteError.into_error()) + return Err(CE::from_kind(CEK::StoreWriteError)) } let setres = header.set(&String::from("counter.value"), Value::Integer(init)); if setres.is_err() { - return Err(CEK::StoreWriteError.into_error()) + return Err(CE::from_kind(CEK::StoreWriteError)) } } @@ -107,7 +106,7 @@ impl<'a> Counter<'a> { let setres = header.set(&String::from("counter.unit"), Value::String(u.0)); if setres.is_err() { self.unit = None; - return Err(CEK::StoreWriteError.into_error()) + return Err(CE::from_kind(CEK::StoreWriteError)) } }; Ok(self) @@ -155,16 +154,16 @@ impl<'a> Counter<'a> { pub fn name(&self) -> Result<CounterName> { self.read_header_at("counter.name", |v| match v { Some(&Value::String(ref s)) => Ok(s.clone()), - Some(_) => Err(CEK::HeaderTypeError.into_error()), - _ => Err(CEK::StoreReadError.into_error()), + Some(_) => Err(CE::from_kind(CEK::HeaderTypeError)), + _ => Err(CE::from_kind(CEK::StoreReadError)), }) } pub fn value(&self) -> Result<i64> { self.read_header_at("counter.value", |v| match v { Some(&Value::Integer(i)) => Ok(i), - Some(_) => Err(CEK::HeaderTypeError.into_error()), - _ => Err(CEK::StoreReadError.into_error()), + Some(_) => Err(CE::from_kind(CEK::HeaderTypeError)), + _ => Err(CE::from_kind(CEK::StoreReadError)), }) } @@ -175,8 +174,8 @@ impl<'a> Counter<'a> { pub fn read_unit(&self) -> Result<Option<CounterUnit>> { self.read_header_at("counter.unit", |s| match s { Some(&Value::String(ref s)) => Ok(Some(CounterUnit::new(s.clone()))), - Some(_) => Err(CEK::HeaderTypeError.into_error()), - _ => Err(CEK::StoreReadError.into_error()), + Some(_) => Err(CE::from_kind(CEK::HeaderTypeError)), + _ => Err(CE::from_kind(CEK::StoreReadError)), }) } diff --git a/lib/domain/libimagcounter/src/error.rs b/lib/domain/libimagc |