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