diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-01 17:22:22 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-12-01 17:22:22 +0100 |
commit | 6f53964dc0530a9199995129a1b605f2875b61b5 (patch) | |
tree | 10b7620d9f9d6afb76bd022209ab47b8f134e392 /bin/domain/imag-contact/src/edit.rs | |
parent | b25bf80c9566ed65ba9583ee91b767910e7a1c0b (diff) | |
parent | 3a3f9ebeed5336bb2e1baf7aa9b0ae6decd961d6 (diff) |
Merge branch 'imag-contact/main-return-result' into master
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'bin/domain/imag-contact/src/edit.rs')
-rw-r--r-- | bin/domain/imag-contact/src/edit.rs | 64 |
1 files changed, 29 insertions, 35 deletions
diff --git a/bin/domain/imag-contact/src/edit.rs b/bin/domain/imag-contact/src/edit.rs index 78e436ba..ecfeffef 100644 --- a/bin/domain/imag-contact/src/edit.rs +++ b/bin/domain/imag-contact/src/edit.rs @@ -32,16 +32,16 @@ while_true, )] -use std::process::exit; use std::io::Read; use std::io::Write; -use failure::Error; use failure::err_msg; use failure::Fallible as Result; +use resiter::Filter; +use resiter::Map; +use resiter::AndThen; use libimagrt::runtime::Runtime; -use libimagerror::trace::MapErrTrace; use libimagstore::store::FileLockEntry; use libimagcontact::store::ContactStore; use libimagentryref::reference::fassade::RefFassade; @@ -49,61 +49,56 @@ use libimagentryref::hasher::default::DefaultHasher; use libimagentryref::reference::Ref; use libimagentryref::reference::Config as RefConfig; -pub fn edit(rt: &Runtime) { +pub fn edit(rt: &Runtime) -> Result<()> { let scmd = rt.cli().subcommand_matches("edit").unwrap(); let collection_name = rt.cli().value_of("contact-ref-collection-name").unwrap(); // default by clap - let ref_config = libimagentryref::util::get_ref_config(&rt, "imag-contact").map_err_trace_exit_unwrap(); + let ref_config = libimagentryref::util::get_ref_config(&rt, "imag-contact")?; let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap let force_override = true; // when editing, we want to override, right? let retry = !scmd.is_present("fail-on-parse-error"); if rt.output_is_pipe() { - error!("Cannot spawn editor if output is a pipe!"); - exit(1); + return Err(err_msg("Cannot spawn editor if output is a pipe!")) } let mut output = rt.stdout(); - let mut input = rt.stdin().unwrap_or_else(|| { - error!("No input stream. Cannot ask for permission."); - exit(1) - }); + let mut input = rt.stdin().ok_or_else(|| { + err_msg("No input stream. Cannot ask for permission.") + })?; - crate::util::find_contact_by_hash(rt, hash) - .for_each(|contact| { + crate::util::find_contact_by_hash(rt, hash)? + .filter_ok(|tpl| tpl.0) + .map_ok(|tpl| tpl.1) + .and_then_ok(|contact| { loop { let res = edit_contact(&rt, &contact, &ref_config, collection_name, force_override); + if !retry { - res.map_err_trace_exit_unwrap(); - } else if ask_continue(&mut input, &mut output) { - continue; -} else { - exit(1) -} + return res + } else if ask_continue(&mut input, &mut output)? { + continue; + } else { + return res + } } - }); + }) + .collect::<Result<Vec<_>>>() + .map(|_| ()) } fn edit_contact<'a>(rt: &Runtime, contact: &FileLockEntry<'a>, ref_config: &RefConfig, collection_name: &str, force_override: bool) -> Result<()> { let filepath = contact .as_ref_with_hasher::<DefaultHasher>() - .get_path(ref_config) - .map_err_trace_exit_unwrap(); + .get_path(ref_config)?; - let success = rt.editor() - .map_err_trace_exit_unwrap() - .ok_or_else(|| { - err_msg("I have no editor configured. Cannot continue!") - }) - .map_err_trace_exit_unwrap() + let success = rt.editor()? + .ok_or_else(|| err_msg("I have no editor configured. Cannot continue!"))? .arg(&filepath) - .status() - .map_err(Error::from) - .map_err_trace_exit_unwrap() + .status()? .success(); if !success { - error!("Editor failed!"); - exit(1); + return Err(err_msg("Editor failed!")) } rt.store() @@ -111,8 +106,7 @@ fn edit_contact<'a>(rt: &Runtime, contact: &FileLockEntry<'a>, ref_config: &RefC .map(|_| ()) } -fn ask_continue(inputstream: &mut dyn Read, outputstream: &mut dyn Write) -> bool { +fn ask_continue(inputstream: &mut dyn Read, outputstream: &mut dyn Write) -> Result<bool> { ::libimaginteraction::ask::ask_bool("Edit vcard", Some(true), inputstream, outputstream) - .map_err_trace_exit_unwrap() } |