Remove map_err_trace_exit() calls in favour of map_err_trace_exit_unwrap()

This commit is contained in:
Matthias Beyer 2018-01-22 12:48:28 +01:00
parent e43812d262
commit 7731b88c97
12 changed files with 60 additions and 100 deletions

View file

@ -82,22 +82,18 @@ fn add(rt: &Runtime) {
let entry_name = scmd let entry_name = scmd
.value_of("entry") .value_of("entry")
.map(PathBuf::from) .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 .unwrap(); // safed by clap
let _ = rt.store() let _ = rt.store()
.get(entry_name) .get(entry_name)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.ok_or(AE::from("Entry does not exist".to_owned())) .ok_or(AE::from("Entry does not exist".to_owned()))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.annotate(rt.store(), annotation_name) .annotate(rt.store(), annotation_name)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.edit_content(&rt) .edit_content(&rt)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
info!("Ok"); info!("Ok");
} }
@ -109,17 +105,14 @@ fn remove(rt: &Runtime) {
let delete = scmd.is_present("delete-annotation"); let delete = scmd.is_present("delete-annotation");
let mut entry = rt.store() let mut entry = rt.store()
.get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit(1).unwrap()) .get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit_unwrap(1))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.ok_or(AE::from("Entry does not exist".to_owned())) .ok_or(AE::from("Entry does not exist".to_owned()))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
let annotation = entry let annotation = entry
.denotate(rt.store(), annotation_name) .denotate(rt.store(), annotation_name)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
if delete { if delete {
debug!("Deleting annotation object"); debug!("Deleting annotation object");
@ -130,8 +123,7 @@ fn remove(rt: &Runtime) {
let _ = rt let _ = rt
.store() .store()
.delete(loc) .delete(loc)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
} else { } else {
warn!("Not having annotation object, cannot delete!"); warn!("Not having annotation object, cannot delete!");
} }
@ -149,17 +141,14 @@ fn list(rt: &Runtime) {
Some(pb) => { Some(pb) => {
let _ = rt let _ = rt
.store() .store()
.get(pb.into_storeid().map_err_trace_exit(1).unwrap()) .get(pb.into_storeid().map_err_trace_exit_unwrap(1))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.ok_or(AE::from("Entry does not exist".to_owned())) .ok_or(AE::from("Entry does not exist".to_owned()))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.annotations(rt.store()) .annotations(rt.store())
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.enumerate() .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::<Vec<_>>(); .collect::<Vec<_>>();
} }
@ -168,10 +157,9 @@ fn list(rt: &Runtime) {
let _ = rt let _ = rt
.store() .store()
.all_annotations() .all_annotations()
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.enumerate() .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::<Vec<_>>(); .collect::<Vec<_>>();
} }
} }

View file

@ -100,8 +100,7 @@ fn main() {
let diags = rt.store() let diags = rt.store()
.entries() .entries()
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.map(|e| { .map(|e| {
e.map_err_trace_exit_unwrap(1) e.map_err_trace_exit_unwrap(1)

View file

@ -103,8 +103,7 @@ fn add(rt: &Runtime) {
.get(sid) .get(sid)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.map(|mut entry| { .map(|mut entry| {
let _ = entry.set_coordinates(c) let _ = entry.set_coordinates(c).map_err_trace_exit_unwrap(1);
.map_err_trace_exit(1);
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
error!("No such entry: {}", entry_name); error!("No such entry: {}", entry_name);

View file

@ -106,7 +106,7 @@ fn main() {
} }
}) })
.ok_or(LE::from("No commandline call".to_owned())) .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<Option<FileLockEntry<'a>>, StoreError> { fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>, StoreError> {

View file

@ -75,7 +75,7 @@ fn main() {
let _ = rt let _ = rt
.store() .store()
.move_by_id(sourcename, destname) .move_by_id(sourcename, destname)
.map_err_trace_exit(1); .map_err_trace_exit_unwrap(1);
info!("Ok."); info!("Ok.");
} }

View file

@ -36,7 +36,7 @@ pub fn delete(rt: &Runtime) {
let _ = rt.store() let _ = rt.store()
.delete(path) .delete(path)
.map_warn_err(|e| format!("Error: {:?}", e)) .map_warn_err(|e| format!("Error: {:?}", e))
.map_err_trace_exit(1); .map_err_trace_exit_unwrap(1);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -35,7 +35,7 @@ pub fn retrieve(rt: &Runtime) {
let id = scmd.value_of("id").unwrap(); let id = scmd.value_of("id").unwrap();
let path = PathBuf::from(id); let path = PathBuf::from(id);
let store = Some(rt.store().path().clone()); 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); debug!("path = {:?}", path);
rt.store() rt.store()

View file

@ -163,7 +163,7 @@ fn main() {
} else { } else {
let _ = StdoutViewer::new(view_header, view_content) let _ = StdoutViewer::new(view_header, view_content)
.view_entry(&entry) .view_entry(&entry)
.map_err_trace_exit(1); .map_err_trace_exit_unwrap(1);
} }
} }

View file

@ -107,28 +107,24 @@ fn list(rt: &Runtime) {
let _ = rt let _ = rt
.store() .store()
.all_contacts() .all_contacts()
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap() // safed by above call
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.map(|fle| { .map(|fle| {
let fle = fle let fle = fle
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.ok_or_else(|| CE::from("StoreId not found".to_owned())) .ok_or_else(|| CE::from("StoreId not found".to_owned()))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
fle fle
.get_contact_data() .get_contact_data()
.map(|cd| (fle, cd)) .map(|cd| (fle, cd))
.map(|(fle, cd)| (fle, cd.into_inner())) .map(|(fle, cd)| (fle, cd.into_inner()))
.map(|(fle, cd)| (fle, Vcard::from_component(cd))) .map(|(fle, cd)| (fle, Vcard::from_component(cd)))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
}) })
.enumerate() .enumerate()
.map(|(i, (fle, vcard))| { .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| { let vcard = vcard.unwrap_or_else(|e| {
error!("Element is not a VCARD object: {:?}", e); error!("Element is not a VCARD object: {:?}", e);
exit(1) exit(1)
@ -137,8 +133,7 @@ fn list(rt: &Runtime) {
let data = build_data_object_for_handlebars(i, hash, &vcard); let data = build_data_object_for_handlebars(i, hash, &vcard);
let s = list_format.render("format", &data) let s = list_format.render("format", &data)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
println!("{}", s); println!("{}", s);
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -157,18 +152,16 @@ fn import(rt: &Runtime) {
let _ = rt let _ = rt
.store() .store()
.create_from_path(&path) .create_from_path(&path)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
} else if path.is_dir() { } else if path.is_dir() {
for entry in WalkDir::new(path).min_depth(1).into_iter() { 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() { if entry.file_type().is_file() {
let pb = PathBuf::from(entry.path()); let pb = PathBuf::from(entry.path());
let _ = rt let _ = rt
.store() .store()
.create_from_path(&pb) .create_from_path(&pb)
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1);
.unwrap();
info!("Imported: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>")); info!("Imported: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
} else { } else {
warn!("Ignoring non-file: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>")); warn!("Ignoring non-file: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
@ -186,14 +179,11 @@ fn show(rt: &Runtime) {
let contact_data = rt.store() let contact_data = rt.store()
.get_by_hash(hash.clone()) .get_by_hash(hash.clone())
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.ok_or(CE::from(format!("No entry for hash {}", hash))) .ok_or(CE::from(format!("No entry for hash {}", hash)))
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.get_contact_data() .get_contact_data()
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.unwrap()
.into_inner(); .into_inner();
let vcard = Vcard::from_component(contact_data) let vcard = Vcard::from_component(contact_data)
.unwrap_or_else(|e| { .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 show_format = get_contact_print_format("contact.show_format", rt, &scmd);
let data = build_data_object_for_handlebars(0, hash, &vcard); let data = build_data_object_for_handlebars(0, hash, &vcard);
let s = show_format.render("format", &data) let s = show_format.render("format", &data).map_err_trace_exit_unwrap(1);
.map_err_trace_exit(1)
.unwrap();
println!("{}", s); println!("{}", s);
info!("Ok"); 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 mut hb = Handlebars::new();
let _ = hb let _ = hb.register_template_string("format", fmt).map_err_trace_exit_unwrap(1);
.register_template_string("format", fmt)
.map_err_trace_exit(1)
.unwrap();
hb.register_escape_fn(::handlebars::no_escape); hb.register_escape_fn(::handlebars::no_escape);
::libimaginteraction::format::register_all_color_helpers(&mut hb); ::libimaginteraction::format::register_all_color_helpers(&mut hb);

View file

@ -79,7 +79,7 @@ fn create(rt: &Runtime) {
let _ = note let _ = note
.edit_content(rt) .edit_content(rt)
.map_warn_err_str("Editing failed") .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() let _ = rt.store()
.delete_note(name_from_cli(rt, "delete")) .delete_note(name_from_cli(rt, "delete"))
.map_info_str("Ok") .map_info_str("Ok")
.map_err_trace_exit(1); .map_err_trace_exit_unwrap(1);
} }
fn edit(rt: &Runtime) { fn edit(rt: &Runtime) {
@ -100,7 +100,7 @@ fn edit(rt: &Runtime) {
let _ = note let _ = note
.edit_content(rt) .edit_content(rt)
.map_warn_err_str("Editing failed") .map_warn_err_str("Editing failed")
.map_err_trace_exit(1); .map_err_trace_exit_unwrap(1);
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
error!("Cannot find note with name '{}'", name); error!("Cannot find note with name '{}'", name);
@ -110,27 +110,22 @@ fn edit(rt: &Runtime) {
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
use std::cmp::Ordering; use std::cmp::Ordering;
rt.store() let _ = rt
.store()
.all_notes() .all_notes()
.map_err_trace_exit(1) .map_err_trace_exit_unwrap(1)
.map(|iter| {
let notes = iter
.filter_map(|noteid| rt.store().get(noteid).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| { .sorted_by(|note_a, note_b| if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
return a.cmp(&b) return a.cmp(&b)
} else { } else {
return Ordering::Greater; return Ordering::Greater;
} })
}); .iter()
.for_each(|note| {
for note in notes.iter() {
note.get_name() note.get_name()
.map(|name| println!("{}", name)) .map(|name| println!("{}", name))
.map_err_trace() .map_err_trace()
.ok(); .ok();
} });
})
.ok();
} }

View file

@ -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 list` lists with a table now
* `imag-timetrack stop` now stops all runnings tags if none are specified * `imag-timetrack stop` now stops all runnings tags if none are specified
* The `toml-query` dependency was updated to 0.6.0 * 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 * Bugfixes
## 0.5.0 ## 0.5.0

View file

@ -118,7 +118,6 @@ pub trait MapErrTrace {
fn map_err_trace(self) -> Self; fn map_err_trace(self) -> Self;
fn map_err_dbg_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_exit_unwrap(self, code: i32) -> Self::Output;
fn map_err_trace_maxdepth(self, max: u64) -> Self; fn map_err_trace_maxdepth(self, max: u64) -> Self;
} }
@ -140,16 +139,9 @@ impl<U, E: Error> MapErrTrace for Result<U, E> {
self.map_err(|e| { trace_error_dbg(&e); e }) self.map_err(|e| { trace_error_dbg(&e); e })
} }
/// Simply call `trace_error_exit(code)` on the Err (if there is one). /// Trace the error and exit or unwrap the Ok(_).
///
/// 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
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output { 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. /// Simply call `trace_error_maxdepth(max)` on the Err (if there is one) and return the error.