Optimize implementation

This does optimize the implementation of the list command implementation
(code-structure wise, not performance wise).

Functionality not altered.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2018-11-04 22:08:05 +01:00
parent e043304767
commit 5210985bf4

View file

@ -119,14 +119,11 @@ fn list(rt: &Runtime) {
.all_contacts() .all_contacts()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.map(|fle| { .trace_unwrap_exit(1)
let fle = fle .map(|fle| fle.ok_or_else(|| Error::from(err_msg("StoreId not found".to_owned()))))
.map_err_trace_exit_unwrap(1) .trace_unwrap_exit(1)
.ok_or_else(|| Error::from(err_msg("StoreId not found".to_owned()))) .map(|e| e.deser())
.map_err_trace_exit_unwrap(1); .trace_unwrap_exit(1)
fle.deser().map_err_trace_exit_unwrap(1)
})
.enumerate(); .enumerate();
if scmd.is_present("json") { if scmd.is_present("json") {
@ -140,20 +137,18 @@ fn list(rt: &Runtime) {
} }
} }
} else { } else {
iterator let rendered = iterator
.map(|(i, deservcard)| { .map(|(i, dvcard)| build_data_object_for_handlebars(i, &dvcard))
let data = build_data_object_for_handlebars(i, &deservcard); .map(|data| list_format.render("format", &data).map_err(Error::from))
.trace_unwrap_exit(1)
list_format.render("format", &data) .collect::<Vec<String>>();
.map_err(Error::from)
.map_err_trace_exit_unwrap(1)
})
// collect, so that we can have rendered all the things and printing is faster. // collect, so that we can have rendered all the things and printing is faster.
.collect::<Vec<String>>()
.into_iter() let output = rt.stdout();
.for_each(|s| { let mut output = output.lock();
writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit()
rendered.into_iter().for_each(|s| {
writeln!(output, "{}", s).to_exit_code().unwrap_or_exit()
}); });
} }
} }