Refactor: Move contact-by-hash finding to utils

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-04-09 19:40:13 +02:00
parent 26664a5aec
commit 9945cb38b7
2 changed files with 42 additions and 26 deletions

View file

@ -217,34 +217,10 @@ fn show(rt: &Runtime) {
let out = rt.stdout(); let out = rt.stdout();
let mut outlock = out.lock(); let mut outlock = out.lock();
rt.store() util::find_contact_by_hash(rt, hash)
.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
}
})
.enumerate() .enumerate()
.for_each(|(i, elem)| { .for_each(|(i, elem)| {
let elem = elem.deser().map_err_trace_exit_unwrap();
let data = build_data_object_for_handlebars(i, &elem); let data = build_data_object_for_handlebars(i, &elem);
let s = show_format let s = show_format

View file

@ -18,8 +18,17 @@
// //
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::process::exit;
use libimagcontact::deser::DeserVcard; 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> { pub fn build_data_object_for_handlebars<'a>(i: usize, vcard: &DeserVcard) -> BTreeMap<&'static str, String> {
let mut data = BTreeMap::new(); let mut data = BTreeMap::new();
@ -75,3 +84,34 @@ pub fn build_data_object_for_handlebars<'a>(i: usize, vcard: &DeserVcard) -> BTr
data data
} }
pub fn find_contact_by_hash<'a, H: AsRef<str>>(rt: &'a Runtime, hash: H)
-> impl Iterator<Item = FileLockEntry<'a>>
{
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
}
})
}