diff options
Diffstat (limited to 'lib/etc/libimaginteraction')
-rw-r--r-- | lib/etc/libimaginteraction/Cargo.toml | 1 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/ask.rs | 6 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/error.rs | 82 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/lib.rs | 6 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/readline.rs | 37 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/result.rs | 24 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/ui.rs | 18 |
7 files changed, 99 insertions, 75 deletions
diff --git a/lib/etc/libimaginteraction/Cargo.toml b/lib/etc/libimaginteraction/Cargo.toml index a72de4af..c409e9b8 100644 --- a/lib/etc/libimaginteraction/Cargo.toml +++ b/lib/etc/libimaginteraction/Cargo.toml @@ -21,6 +21,7 @@ lazy_static = "0.2.*" log = "0.3" regex = "0.2" toml = "^0.4" +error-chain = "0.10" libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/etc/libimaginteraction/src/ask.rs b/lib/etc/libimaginteraction/src/ask.rs index 2a393d0e..96dbc560 100644 --- a/lib/etc/libimaginteraction/src/ask.rs +++ b/lib/etc/libimaginteraction/src/ask.rs @@ -24,9 +24,9 @@ use std::io::BufRead; use std::io::BufReader; use std::result::Result as RResult; -use error::InteractionError; use error::InteractionErrorKind; -use result::Result; +use error::ResultExt; +use error::Result; use regex::Regex; use ansi_term::Colour::*; @@ -163,7 +163,7 @@ fn ask_string_<R: BufRead>(s: &str, pub fn ask_select_from_list(list: &[&str]) -> Result<String> { pick_from_list(default_menu_cmd().as_mut(), list, "Selection: ") - .map_err(|e| InteractionError::new(InteractionErrorKind::Unknown, Some(Box::new(e)))) + .chain_err(|| InteractionErrorKind::Unknown) } /// Helper function to print a imag question string. The `question` argument may not contain a diff --git a/lib/etc/libimaginteraction/src/error.rs b/lib/etc/libimaginteraction/src/error.rs index 1a4be7df..28345c94 100644 --- a/lib/etc/libimaginteraction/src/error.rs +++ b/lib/etc/libimaginteraction/src/error.rs @@ -17,23 +17,67 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -generate_error_module!( - generate_error_types!(InteractionError, InteractionErrorKind, - Unknown => "Unknown Error", - CLIError => "Error on commandline", - IdMissingError => "Commandline: ID missing", - StoreIdParsingError => "Error while parsing StoreId", - IdSelectingError => "Error while selecting id", - ConfigError => "Configuration error", - ConfigMissingError => "Configuration missing", - ConfigTypeError => "Config Type Error", - NoConfigError => "No configuration", - ReadlineHistoryFileCreationError => "Could not create history file for readline", - ReadlineError => "Readline error" - ); -); - -pub use self::error::InteractionError; -pub use self::error::InteractionErrorKind; -pub use self::error::MapErrInto; +error_chain! { + types { + InteractionError, InteractionErrorKind, ResultExt, Result; + } + + errors { + Unknown { + description("Unknown Error") + display("Unknown Error") + } + + CLIError { + description("Error on commandline") + display("Error on commandline") + } + + IdMissingError { + description("Commandline: ID missing") + display("Commandline: ID missing") + } + + StoreIdParsingError { + description("Error while parsing StoreId") + display("Error while parsing StoreId") + } + + IdSelectingError { + description("Error while selecting id") + display("Error while selecting id") + } + + ConfigError { + description("Configuration error") + display("Configuration error") + } + + ConfigMissingError { + description("Configuration missing") + display("Configuration missing") + } + + ConfigTypeError { + description("Config Type Error") + display("Config Type Error") + } + + NoConfigError { + description("No configuration") + display("No configuration") + } + + ReadlineHistoryFileCreationError { + description("Could not create history file for readline") + display("Could not create history file for readline") + } + + ReadlineError { + description("Readline error") + display("Readline error") + } + + } +} diff --git a/lib/etc/libimaginteraction/src/lib.rs b/lib/etc/libimaginteraction/src/lib.rs index cd38071a..0a2aa118 100644 --- a/lib/etc/libimaginteraction/src/lib.rs +++ b/lib/etc/libimaginteraction/src/lib.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +#![recursion_limit="256"] + #![deny( dead_code, non_camel_case_types, @@ -39,13 +41,13 @@ extern crate ansi_term; extern crate regex; extern crate clap; extern crate toml; +#[macro_use] extern crate error_chain; extern crate libimagstore; -#[macro_use] extern crate libimagerror; +extern crate libimagerror; pub mod ask; pub mod error; pub mod filter; -pub mod result; pub mod ui; diff --git a/lib/etc/libimaginteraction/src/readline.rs b/lib/etc/libimaginteraction/src/readline.rs index 7b998dd0..7d498ce5 100644 --- a/lib/etc/libimaginteraction/src/readline.rs +++ b/lib/etc/libimaginteraction/src/readline.rs @@ -17,8 +17,9 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use error::InteractionError as IE; use error::InteractionErrorKind as IEK; -use error::MapErrInto; +use error::ResultExt; use toml::Value; @@ -44,37 +45,37 @@ impl Readline { let histfile = try!(match histfile { Value::String(s) => PathBuf::from(s), - _ => Err(IEK::ConfigTypeError.into_error()) - .map_err_into(IEK::ConfigError) - .map_err_into(IEK::ReadlineError) + _ => Err(IE::from_kind(IEK::ConfigTypeError)) + .chain_err(|| IEK::ConfigError) + .chain_err(|| IEK::ReadlineError) }); let histsize = try!(match histsize { Value::Integer(i) => i, - _ => Err(IEK::ConfigTypeError.into_error()) - .map_err_into(IEK::ConfigError) - .map_err_into(IEK::ReadlineError) + _ => Err(IE::from_kind(IEK::ConfigTypeError)) + .chain_err(|| IEK::ConfigError) + .chain_err(|| IEK::ReadlineError) }); let histigndups = try!(match histigndups { Value::Boolean(b) => b, - _ => Err(IEK::ConfigTypeError.into_error()) - .map_err_into(IEK::ConfigError) - .map_err_into(IEK::ReadlineError) + _ => Err(IE::from_kind(IEK::ConfigTypeError)) + .chain_err(|| IEK::ConfigError) + .chain_err(|| IEK::ReadlineError) }); let histignspace = try!(match histignspace { Value::Boolean(b) => b, - _ => Err(IEK::ConfigTypeError.into_error()) - .map_err_into(IEK::ConfigError) - .map_err_into(IEK::ReadlineError) + _ => Err(IE::from_kind(IEK::ConfigTypeError)) + .chain_err(|| IEK::ConfigError) + .chain_err(|| IEK::ReadlineError) }); let prompt = try!(match prompt { Value::String(s) => s, - _ => Err(IEK::ConfigTypeError.into_error()) - .map_err_into(IEK::ConfigError) - .map_err_into(IEK::ReadlineError) + _ => Err(IE::from_kind(IEK::ConfigTypeError)) + .chain_err(|| IEK::ConfigError) + .chain_err(|| IEK::ReadlineError) }); let config = Config::builder(). @@ -87,10 +88,10 @@ impl Readline { if !histfile.exists() { let _ = try!(File::create(histfile.clone()) - .map_err_into(IEK::ReadlineHistoryFileCreationError)); + .chain_err(|| IEK::ReadlineHistoryFileCreationError)); } - let _ = try!(editor.load_history(&histfile).map_err_into(ReadlineError)); + let _ = try!(editor.load_history(&histfile).chain_err(|| ReadlineError)); Ok(Readline { editor: editor, diff --git a/lib/etc/libimaginteraction/src/result.rs b/lib/etc/libimaginteraction/src/result.rs deleted file mode 100644 index 8eeaf47b..00000000 --- a/lib/etc/libimaginteraction/src/result.rs +++ /dev/null @@ -1,24 +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::result::Result as RResult; - -use error::InteractionError; - -pub type Result<T> = RResult<T, InteractionError>; diff --git a/lib/etc/libimaginteraction/src/ui.rs b/lib/etc/libimaginteraction/src/ui.rs index 49c4619d..d1712f94 100644 --- a/lib/etc/libimaginteraction/src/ui.rs +++ b/lib/etc/libimaginteraction/src/ui.rs @@ -22,11 +22,11 @@ use std::path::PathBuf; use clap::{Arg, ArgMatches}; use libimagstore::storeid::StoreId; -use libimagerror::into::IntoError; -use result::Result; -use error::MapErrInto; +use error::InteractionError as IE; +use error::Result; use error::InteractionErrorKind as IEK; +use error::ResultExt; pub fn id_argument<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name(id_argument_name()) @@ -52,14 +52,14 @@ pub fn id_argument_long() -> &'static str { pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> { matches .values_of(id_argument_name()) - .ok_or(IEK::IdMissingError.into_error()) - .map_err_into(IEK::CLIError) + .ok_or(IE::from_kind(IEK::IdMissingError)) + .chain_err(|| IEK::CLIError) .and_then(|vals| { vals.into_iter() .fold(Ok(vec![]), |acc, elem| { acc.and_then(|mut v| { let elem = StoreId::new_baseless(PathBuf::from(String::from(elem))); - let elem = try!(elem.map_err_into(IEK::StoreIdParsingError)); + let elem = try!(elem.chain_err(|| IEK::StoreIdParsingError)); v.push(elem); Ok(v) }) @@ -70,12 +70,12 @@ pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> { pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result<Vec<StoreId>> { use interactor::{pick_file, default_menu_cmd}; - match get_id(matches).map_err_into(IEK::IdSelectingError) { + match get_id(matches).chain_err(|| IEK::IdSelectingError) { Ok(v) => Ok(v), Err(_) => { let path = store_path.clone(); - let p = try!(pick_file(default_menu_cmd, path).map_err_into(IEK::IdSelectingError)); - let id = try!(StoreId::new_baseless(p).map_err_into(IEK::StoreIdParsingError)); + let p = try!(pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)); + let id = try!(StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)); Ok(vec![id]) }, } |