From 7731b88c9767df25bee982a5529a8a9dbf37255f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 22 Jan 2018 12:48:28 +0100 Subject: [PATCH] Remove map_err_trace_exit() calls in favour of map_err_trace_exit_unwrap() --- bin/core/imag-annotate/src/main.rs | 46 ++++++++++----------------- bin/core/imag-diagnostics/src/main.rs | 3 +- bin/core/imag-gps/src/main.rs | 3 +- bin/core/imag-link/src/main.rs | 2 +- bin/core/imag-mv/src/main.rs | 2 +- bin/core/imag-store/src/delete.rs | 2 +- bin/core/imag-store/src/retrieve.rs | 2 +- bin/core/imag-view/src/main.rs | 2 +- bin/domain/imag-contact/src/main.rs | 43 ++++++++----------------- bin/domain/imag-notes/src/main.rs | 41 +++++++++++------------- doc/src/09020-changelog.md | 2 ++ lib/core/libimagerror/src/trace.rs | 12 ++----- 12 files changed, 60 insertions(+), 100 deletions(-) diff --git a/bin/core/imag-annotate/src/main.rs b/bin/core/imag-annotate/src/main.rs index 94cbd48a..92a2f92e 100644 --- a/bin/core/imag-annotate/src/main.rs +++ b/bin/core/imag-annotate/src/main.rs @@ -82,22 +82,18 @@ fn add(rt: &Runtime) { let entry_name = scmd .value_of("entry") .map(PathBuf::from) - .map(|pb| pb.into_storeid().map_err_trace_exit(1).unwrap()) + .map(|pb| pb.into_storeid().map_err_trace_exit_unwrap(1)) .unwrap(); // safed by clap let _ = rt.store() .get(entry_name) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .ok_or(AE::from("Entry does not exist".to_owned())) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .annotate(rt.store(), annotation_name) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .edit_content(&rt) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); info!("Ok"); } @@ -109,17 +105,14 @@ fn remove(rt: &Runtime) { let delete = scmd.is_present("delete-annotation"); let mut entry = rt.store() - .get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit(1).unwrap()) - .map_err_trace_exit(1) - .unwrap() + .get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit_unwrap(1)) + .map_err_trace_exit_unwrap(1) .ok_or(AE::from("Entry does not exist".to_owned())) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); let annotation = entry .denotate(rt.store(), annotation_name) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); if delete { debug!("Deleting annotation object"); @@ -130,8 +123,7 @@ fn remove(rt: &Runtime) { let _ = rt .store() .delete(loc) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); } else { warn!("Not having annotation object, cannot delete!"); } @@ -149,17 +141,14 @@ fn list(rt: &Runtime) { Some(pb) => { let _ = rt .store() - .get(pb.into_storeid().map_err_trace_exit(1).unwrap()) - .map_err_trace_exit(1) - .unwrap() + .get(pb.into_storeid().map_err_trace_exit_unwrap(1)) + .map_err_trace_exit_unwrap(1) .ok_or(AE::from("Entry does not exist".to_owned())) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .annotations(rt.store()) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .enumerate() - .map(|(i, a)| list_annotation(i, a.map_err_trace_exit(1).unwrap(), with_text)) + .map(|(i, a)| list_annotation(i, a.map_err_trace_exit_unwrap(1), with_text)) .collect::>(); } @@ -168,10 +157,9 @@ fn list(rt: &Runtime) { let _ = rt .store() .all_annotations() - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .enumerate() - .map(|(i, a)| list_annotation(i, a.map_err_trace_exit(1).unwrap(), with_text)) + .map(|(i, a)| list_annotation(i, a.map_err_trace_exit_unwrap(1), with_text)) .collect::>(); } } diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs index b9d2ee8f..7d3b06da 100644 --- a/bin/core/imag-diagnostics/src/main.rs +++ b/bin/core/imag-diagnostics/src/main.rs @@ -100,8 +100,7 @@ fn main() { let diags = rt.store() .entries() - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .into_get_iter(rt.store()) .map(|e| { e.map_err_trace_exit_unwrap(1) diff --git a/bin/core/imag-gps/src/main.rs b/bin/core/imag-gps/src/main.rs index 301f37a5..d3f3e532 100644 --- a/bin/core/imag-gps/src/main.rs +++ b/bin/core/imag-gps/src/main.rs @@ -103,8 +103,7 @@ fn add(rt: &Runtime) { .get(sid) .map_err_trace_exit_unwrap(1) .map(|mut entry| { - let _ = entry.set_coordinates(c) - .map_err_trace_exit(1); + let _ = entry.set_coordinates(c).map_err_trace_exit_unwrap(1); }) .unwrap_or_else(|| { error!("No such entry: {}", entry_name); diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 76796a97..f775d030 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -106,7 +106,7 @@ fn main() { } }) .ok_or(LE::from("No commandline call".to_owned())) - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); } fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result>, StoreError> { diff --git a/bin/core/imag-mv/src/main.rs b/bin/core/imag-mv/src/main.rs index 7896e8f1..030763bf 100644 --- a/bin/core/imag-mv/src/main.rs +++ b/bin/core/imag-mv/src/main.rs @@ -75,7 +75,7 @@ fn main() { let _ = rt .store() .move_by_id(sourcename, destname) - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); info!("Ok."); } diff --git a/bin/core/imag-store/src/delete.rs b/bin/core/imag-store/src/delete.rs index f50cc9c0..766c1304 100644 --- a/bin/core/imag-store/src/delete.rs +++ b/bin/core/imag-store/src/delete.rs @@ -36,7 +36,7 @@ pub fn delete(rt: &Runtime) { let _ = rt.store() .delete(path) .map_warn_err(|e| format!("Error: {:?}", e)) - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); } #[cfg(test)] diff --git a/bin/core/imag-store/src/retrieve.rs b/bin/core/imag-store/src/retrieve.rs index 905fe98d..4c3b25c1 100644 --- a/bin/core/imag-store/src/retrieve.rs +++ b/bin/core/imag-store/src/retrieve.rs @@ -35,7 +35,7 @@ pub fn retrieve(rt: &Runtime) { let id = scmd.value_of("id").unwrap(); let path = PathBuf::from(id); let store = Some(rt.store().path().clone()); - let path = StoreId::new(store, path).map_err_trace_exit(1)?; + let path = StoreId::new(store, path).map_err_trace_exit_unwrap(1); debug!("path = {:?}", path); rt.store() diff --git a/bin/core/imag-view/src/main.rs b/bin/core/imag-view/src/main.rs index ef3b3304..eba2245a 100644 --- a/bin/core/imag-view/src/main.rs +++ b/bin/core/imag-view/src/main.rs @@ -163,7 +163,7 @@ fn main() { } else { let _ = StdoutViewer::new(view_header, view_content) .view_entry(&entry) - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); } } diff --git a/bin/domain/imag-contact/src/main.rs b/bin/domain/imag-contact/src/main.rs index fe8cddc5..2747b94c 100644 --- a/bin/domain/imag-contact/src/main.rs +++ b/bin/domain/imag-contact/src/main.rs @@ -107,28 +107,24 @@ fn list(rt: &Runtime) { let _ = rt .store() .all_contacts() - .map_err_trace_exit(1) - .unwrap() // safed by above call + .map_err_trace_exit_unwrap(1) .into_get_iter(rt.store()) .map(|fle| { let fle = fle - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .ok_or_else(|| CE::from("StoreId not found".to_owned())) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); fle .get_contact_data() .map(|cd| (fle, cd)) .map(|(fle, cd)| (fle, cd.into_inner())) .map(|(fle, cd)| (fle, Vcard::from_component(cd))) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) }) .enumerate() .map(|(i, (fle, vcard))| { - let hash = fle.get_path_hash().map_err_trace_exit(1).unwrap(); + let hash = fle.get_path_hash().map_err_trace_exit_unwrap(1); let vcard = vcard.unwrap_or_else(|e| { error!("Element is not a VCARD object: {:?}", e); exit(1) @@ -137,8 +133,7 @@ fn list(rt: &Runtime) { let data = build_data_object_for_handlebars(i, hash, &vcard); let s = list_format.render("format", &data) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); println!("{}", s); }) .collect::>(); @@ -157,18 +152,16 @@ fn import(rt: &Runtime) { let _ = rt .store() .create_from_path(&path) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); } else if path.is_dir() { for entry in WalkDir::new(path).min_depth(1).into_iter() { - let entry = entry.map_err_trace_exit(1).unwrap(); + let entry = entry.map_err_trace_exit_unwrap(1); if entry.file_type().is_file() { let pb = PathBuf::from(entry.path()); let _ = rt .store() .create_from_path(&pb) - .map_err_trace_exit(1) - .unwrap(); + .map_err_trace_exit_unwrap(1); info!("Imported: {}", entry.path().to_str().unwrap_or("")); } else { warn!("Ignoring non-file: {}", entry.path().to_str().unwrap_or("")); @@ -186,14 +179,11 @@ fn show(rt: &Runtime) { let contact_data = rt.store() .get_by_hash(hash.clone()) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .ok_or(CE::from(format!("No entry for hash {}", hash))) - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .get_contact_data() - .map_err_trace_exit(1) - .unwrap() + .map_err_trace_exit_unwrap(1) .into_inner(); let vcard = Vcard::from_component(contact_data) .unwrap_or_else(|e| { @@ -204,9 +194,7 @@ fn show(rt: &Runtime) { let show_format = get_contact_print_format("contact.show_format", rt, &scmd); let data = build_data_object_for_handlebars(0, hash, &vcard); - let s = show_format.render("format", &data) - .map_err_trace_exit(1) - .unwrap(); + let s = show_format.render("format", &data).map_err_trace_exit_unwrap(1); println!("{}", s); info!("Ok"); } @@ -226,10 +214,7 @@ fn get_contact_print_format(config_value_path: &'static str, rt: &Runtime, scmd: }); let mut hb = Handlebars::new(); - let _ = hb - .register_template_string("format", fmt) - .map_err_trace_exit(1) - .unwrap(); + let _ = hb.register_template_string("format", fmt).map_err_trace_exit_unwrap(1); hb.register_escape_fn(::handlebars::no_escape); ::libimaginteraction::format::register_all_color_helpers(&mut hb); diff --git a/bin/domain/imag-notes/src/main.rs b/bin/domain/imag-notes/src/main.rs index e3234393..d04208e6 100644 --- a/bin/domain/imag-notes/src/main.rs +++ b/bin/domain/imag-notes/src/main.rs @@ -79,7 +79,7 @@ fn create(rt: &Runtime) { let _ = note .edit_content(rt) .map_warn_err_str("Editing failed") - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); } } @@ -87,7 +87,7 @@ fn delete(rt: &Runtime) { let _ = rt.store() .delete_note(name_from_cli(rt, "delete")) .map_info_str("Ok") - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); } fn edit(rt: &Runtime) { @@ -100,7 +100,7 @@ fn edit(rt: &Runtime) { let _ = note .edit_content(rt) .map_warn_err_str("Editing failed") - .map_err_trace_exit(1); + .map_err_trace_exit_unwrap(1); }) .unwrap_or_else(|| { error!("Cannot find note with name '{}'", name); @@ -110,27 +110,22 @@ fn edit(rt: &Runtime) { fn list(rt: &Runtime) { use std::cmp::Ordering; - rt.store() + let _ = rt + .store() .all_notes() - .map_err_trace_exit(1) - .map(|iter| { - let notes = iter - .filter_map(|noteid| rt.store().get(noteid).map_err_trace_exit_unwrap(1)) - .sorted_by(|note_a, note_b| { - if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) { - return a.cmp(&b) - } else { - return Ordering::Greater; - } - }); - - for note in notes.iter() { - note.get_name() - .map(|name| println!("{}", name)) - .map_err_trace() - .ok(); - } + .map_err_trace_exit_unwrap(1) + .filter_map(|noteid| rt.store().get(noteid).map_err_trace_exit_unwrap(1)) + .sorted_by(|note_a, note_b| if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) { + return a.cmp(&b) + } else { + return Ordering::Greater; }) - .ok(); + .iter() + .for_each(|note| { + note.get_name() + .map(|name| println!("{}", name)) + .map_err_trace() + .ok(); + }); } diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md index 78a93816..29a4a066 100644 --- a/doc/src/09020-changelog.md +++ b/doc/src/09020-changelog.md @@ -40,6 +40,8 @@ This section contains the changelog from the last release to the next release. * `imag-timetrack list` lists with a table now * `imag-timetrack stop` now stops all runnings tags if none are specified * The `toml-query` dependency was updated to 0.6.0 + * `ResultExt::map_err_trace_exit()` was removed in favour of + `ResultExt::map_err_trace_exit_unwrap()`. * Bugfixes ## 0.5.0 diff --git a/lib/core/libimagerror/src/trace.rs b/lib/core/libimagerror/src/trace.rs index 2ec33a6a..7df6b288 100644 --- a/lib/core/libimagerror/src/trace.rs +++ b/lib/core/libimagerror/src/trace.rs @@ -118,7 +118,6 @@ pub trait MapErrTrace { fn map_err_trace(self) -> Self; fn map_err_dbg_trace(self) -> Self; - fn map_err_trace_exit(self, code: i32) -> Self; fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output; fn map_err_trace_maxdepth(self, max: u64) -> Self; } @@ -140,16 +139,9 @@ impl MapErrTrace for Result { self.map_err(|e| { trace_error_dbg(&e); e }) } - /// Simply call `trace_error_exit(code)` on the Err (if there is one). - /// - /// This does not return if there is an Err(e). - fn map_err_trace_exit(self, code: i32) -> Self { - self.map_err(|e| { trace_error_exit(&e, code) }) - } - - /// Helper for calling map_err_trace_exit(n).unwrap() in one call + /// Trace the error and exit or unwrap the Ok(_). fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output { - self.map_err_trace_exit(code).unwrap() + self.map_err(|e| { trace_error_exit(&e, code) }).unwrap() } /// Simply call `trace_error_maxdepth(max)` on the Err (if there is one) and return the error.