From 9945cb38b7ba068531f0619343f704c3f63e7e22 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Apr 2019 19:40:13 +0200 Subject: [PATCH] Refactor: Move contact-by-hash finding to utils Signed-off-by: Matthias Beyer --- bin/domain/imag-contact/src/main.rs | 28 ++------------------ bin/domain/imag-contact/src/util.rs | 40 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/bin/domain/imag-contact/src/main.rs b/bin/domain/imag-contact/src/main.rs index bc950fa1..bd429bd5 100644 --- a/bin/domain/imag-contact/src/main.rs +++ b/bin/domain/imag-contact/src/main.rs @@ -217,34 +217,10 @@ fn show(rt: &Runtime) { let out = rt.stdout(); let mut outlock = out.lock(); - rt.store() - .all_contacts() - .map_err_trace_exit_unwrap() - .into_get_iter() - .trace_unwrap_exit() - .map(|o| o.unwrap_or_else(|| { - error!("Failed to get entry"); - exit(1) - })) - .filter_map(|entry| { - let deser = entry.deser().map_err_trace_exit_unwrap(); - - if deser.uid() - .ok_or_else(|| { - error!("Could not get StoreId from Store::all_contacts(). This is a BUG!"); - ::std::process::exit(1) - }) - .unwrap() // exited above - .starts_with(&hash) - { - let _ = rt.report_touched(entry.get_location()).unwrap_or_exit(); - Some(deser) - } else { - None - } - }) + util::find_contact_by_hash(rt, hash) .enumerate() .for_each(|(i, elem)| { + let elem = elem.deser().map_err_trace_exit_unwrap(); let data = build_data_object_for_handlebars(i, &elem); let s = show_format diff --git a/bin/domain/imag-contact/src/util.rs b/bin/domain/imag-contact/src/util.rs index ccd13ed6..0ed8be05 100644 --- a/bin/domain/imag-contact/src/util.rs +++ b/bin/domain/imag-contact/src/util.rs @@ -18,8 +18,17 @@ // use std::collections::BTreeMap; +use std::process::exit; use libimagcontact::deser::DeserVcard; +use libimagcontact::store::ContactStore; +use libimagcontact::contact::Contact; +use libimagerror::exit::ExitUnwrap; +use libimagerror::iter::TraceIterator; +use libimagerror::trace::MapErrTrace; +use libimagrt::runtime::Runtime; +use libimagstore::store::FileLockEntry; + pub fn build_data_object_for_handlebars<'a>(i: usize, vcard: &DeserVcard) -> BTreeMap<&'static str, String> { let mut data = BTreeMap::new(); @@ -75,3 +84,34 @@ pub fn build_data_object_for_handlebars<'a>(i: usize, vcard: &DeserVcard) -> BTr data } +pub fn find_contact_by_hash<'a, H: AsRef>(rt: &'a Runtime, hash: H) + -> impl Iterator> +{ + rt.store() + .all_contacts() + .map_err_trace_exit_unwrap() + .into_get_iter() + .trace_unwrap_exit() + .map(|o| o.unwrap_or_else(|| { + error!("Failed to get entry"); + exit(1) + })) + .filter_map(move |entry| { + let deser = entry.deser().map_err_trace_exit_unwrap(); + + if deser.uid() + .ok_or_else(|| { + error!("Could not get StoreId from Store::all_contacts(). This is a BUG!"); + ::std::process::exit(1) + }) + .unwrap() // exited above + .starts_with(hash.as_ref()) + { + let _ = rt.report_touched(entry.get_location()).unwrap_or_exit(); + Some(entry) + } else { + None + } + }) +} +