libimaginteraction: Move from error-chain to failure

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2018-10-30 18:40:50 +01:00
parent 5627bbe454
commit cc503920d0
6 changed files with 28 additions and 117 deletions

View file

@ -26,9 +26,10 @@ lazy_static = "1"
log = "0.4.0" log = "0.4.0"
regex = "1" regex = "1"
toml = "0.4" toml = "0.4"
error-chain = "0.12"
handlebars = "1.0" handlebars = "1.0"
serde_json = "1" serde_json = "1"
failure = "0.1"
failure_derive = "0.1"
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" }

View file

@ -24,13 +24,13 @@ use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::result::Result as RResult; use std::result::Result as RResult;
use error::InteractionErrorKind;
use error::ResultExt;
use error::Result;
use regex::Regex; use regex::Regex;
use ansi_term::Colour::*; use ansi_term::Colour::*;
use interactor::*; use interactor::*;
use failure::Error;
use failure::ResultExt;
use failure::Fallible as Result;
use failure::err_msg;
/// Ask the user for a Yes/No answer. Optionally provide a default value. If none is provided, this /// Ask the user for a Yes/No answer. Optionally provide a default value. If none is provided, this
/// keeps loop{}ing /// keeps loop{}ing
@ -163,7 +163,8 @@ fn ask_string_<R: BufRead>(s: &str,
pub fn ask_select_from_list(list: &[&str]) -> Result<String> { pub fn ask_select_from_list(list: &[&str]) -> Result<String> {
pick_from_list(default_menu_cmd().as_mut(), list, "Selection: ") pick_from_list(default_menu_cmd().as_mut(), list, "Selection: ")
.chain_err(|| InteractionErrorKind::Unknown) .context(err_msg("Unknown interaction error"))
.map_err(Error::from)
} }
/// Helper function to print a imag question string. The `question` argument may not contain a /// Helper function to print a imag question string. The `question` argument may not contain a

View file

@ -1,83 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//
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")
}
}
}

View file

@ -43,13 +43,12 @@ extern crate clap;
extern crate toml; extern crate toml;
extern crate handlebars; extern crate handlebars;
extern crate serde_json; extern crate serde_json;
#[macro_use] extern crate error_chain; extern crate failure;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagerror; extern crate libimagerror;
pub mod ask; pub mod ask;
pub mod error;
pub mod filter; pub mod filter;
pub mod format; pub mod format;
pub mod ui; pub mod ui;

View file

@ -17,10 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use error::InteractionError as IE;
use error::InteractionErrorKind as IEK;
use error::ResultExt;
use failure::ResultExt;
use toml::Value; use toml::Value;
use rustyline::{Config, Editor}; use rustyline::{Config, Editor};
@ -46,32 +44,32 @@ impl Readline {
.as_str() .as_str()
.map(PathBuf::from) .map(PathBuf::from)
.ok_or(IE::from_kind(IEK::ConfigTypeError)) .ok_or(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .context(IEK::ConfigError)
.chain_err(|| IEK::ReadlineError)?; .context(IEK::ReadlineError)?;
let histsize = histsize let histsize = histsize
.as_int() .as_int()
.ok_or(IE::from_kind(IEK::ConfigTypeError)) .ok_or(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .context(IEK::ConfigError)
.chain_err(|| IEK::ReadlineError)?; .context(IEK::ReadlineError)?;
let histigndups = histigndups let histigndups = histigndups
.as_bool() .as_bool()
.ok_or(IE::from_kind(IEK::ConfigTypeError)) .ok_or(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .context(IEK::ConfigError)
.chain_err(|| IEK::ReadlineError)?; .context(IEK::ReadlineError)?;
let histignspace = histignspace let histignspace = histignspace
.as_bool() .as_bool()
.ok_or(IE::from_kind(IEK::ConfigTypeError)) .ok_or(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .context(IEK::ConfigError)
.chain_err(|| IEK::ReadlineError)?; .context(IEK::ReadlineError)?;
let prompt = prompt let prompt = prompt
.as_str() .as_str()
.ok_or(IE::from_kind(IEK::ConfigTypeError)) .ok_or(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .context(IEK::ConfigError)
.chain_err(|| IEK::ReadlineError)?; .context(IEK::ReadlineError)?;
let config = Config::builder(). let config = Config::builder().
.max_history_size(histsize) .max_history_size(histsize)
@ -83,10 +81,10 @@ impl Readline {
if !histfile.exists() { if !histfile.exists() {
let _ = File::create(histfile.clone()) let _ = File::create(histfile.clone())
.chain_err(|| IEK::ReadlineHistoryFileCreationError)?; .context(IEK::ReadlineHistoryFileCreationError)?;
} }
let _ = editor.load_history(&histfile).chain_err(|| ReadlineError)?; let _ = editor.load_history(&histfile).context(ReadlineError)?;
Ok(Readline { Ok(Readline {
editor: editor, editor: editor,

View file

@ -23,10 +23,8 @@ use clap::{Arg, ArgMatches};
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use error::InteractionError as IE; use failure::err_msg;
use error::Result; use failure::Fallible as Result;
use error::InteractionErrorKind as IEK;
use error::ResultExt;
pub fn id_argument<'a, 'b>() -> Arg<'a, 'b> { pub fn id_argument<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(id_argument_name()) Arg::with_name(id_argument_name())
@ -52,14 +50,12 @@ pub fn id_argument_long() -> &'static str {
pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> { pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> {
matches matches
.values_of(id_argument_name()) .values_of(id_argument_name())
.ok_or(IE::from_kind(IEK::IdMissingError)) .ok_or(err_msg("CLI error"))
.chain_err(|| IEK::CLIError)
.and_then(|vals| { .and_then(|vals| {
vals.into_iter() vals.into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(|mut v| { acc.and_then(|mut v| {
let elem = StoreId::new_baseless(PathBuf::from(String::from(elem))); let elem = StoreId::new_baseless(PathBuf::from(String::from(elem)))?;
let elem = elem.chain_err(|| IEK::StoreIdParsingError)?;
v.push(elem); v.push(elem);
Ok(v) Ok(v)
}) })
@ -71,11 +67,10 @@ pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result<Ve
use interactor::{pick_file, default_menu_cmd}; use interactor::{pick_file, default_menu_cmd};
get_id(matches) get_id(matches)
.chain_err(|| IEK::IdSelectingError)
.or_else(|_| { .or_else(|_| {
let path = store_path.clone(); let path = store_path.clone();
let p = pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)?; let p = pick_file(default_menu_cmd, path)?;
let id = StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)?; let id = StoreId::new_baseless(p)?;
Ok(vec![id]) Ok(vec![id])
}) })
} }