From 74d19e1a4bd066d124f0e5c8649c0d699c6bb76a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 3 Jan 2020 18:27:59 +0100 Subject: Outsource listing of a single mail to helper function Signed-off-by: Matthias Beyer --- bin/domain/imag-mail/src/lib.rs | 11 ++++------- bin/domain/imag-mail/src/util.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs index d069ee0e..10d6b850 100644 --- a/bin/domain/imag-mail/src/lib.rs +++ b/bin/domain/imag-mail/src/lib.rs @@ -51,7 +51,6 @@ extern crate libimagutil; extern crate libimagentryref; extern crate libimaginteraction; -use std::io::Write; use std::path::PathBuf; use failure::Fallible as Result; @@ -209,6 +208,7 @@ fn list(rt: &Runtime) -> Result<()> { let refconfig = get_ref_config(rt, "imag-mail")?; let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?; + let mut out = rt.stdout(); let mut i = 0; if rt.ids_from_stdin() { @@ -228,9 +228,7 @@ fn list(rt: &Runtime) -> Result<()> { .into_get_iter(rt.store()) .map_inner_ok_or_else(|| err_msg("Did not find one entry")) .and_then_ok(|m| { - let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?; - let s = list_format.render("format", &data)?; - writeln!(rt.stdout(), "{}", s)?; + crate::util::list_mail(&m, i, &refconfig, &list_format, &mut out)?; rt.report_touched(m.get_location())?; i += 1; Ok(()) @@ -243,6 +241,7 @@ fn unread(rt: &Runtime) -> Result<()> { let refconfig = get_ref_config(rt, "imag-mail")?; let scmd = rt.cli().subcommand_matches("unread").unwrap(); // safe via clap let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?; + let mut out = rt.stdout(); let mut i = 0; if rt.ids_from_stdin() { @@ -261,9 +260,7 @@ fn unread(rt: &Runtime) -> Result<()> { .map_inner_ok_or_else(|| err_msg("Did not find one entry")) .and_then_ok(|m| { if !m.is_seen(&refconfig)? { - let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?; - let s = list_format.render("format", &data)?; - writeln!(rt.stdout(), "{}", s)?; + crate::util::list_mail(&m, i, &refconfig, &list_format, &mut out)?; rt.report_touched(m.get_location())?; i += 1; } diff --git a/bin/domain/imag-mail/src/util.rs b/bin/domain/imag-mail/src/util.rs index 77d6b5a5..7f116649 100644 --- a/bin/domain/imag-mail/src/util.rs +++ b/bin/domain/imag-mail/src/util.rs @@ -18,6 +18,7 @@ // use std::collections::BTreeMap; +use std::io::Write; use clap::ArgMatches; use failure::Error; @@ -85,3 +86,10 @@ pub fn build_data_object_for_handlebars(i: usize, m: &FileLockEntry, refconfig: Ok(data) } +pub fn list_mail(m: &FileLockEntry, i: usize, refconfig: &RefConfig, list_format: &Handlebars, out: &mut dyn Write) -> Result<()> { + let data = build_data_object_for_handlebars(i, m, refconfig)?; + let s = list_format.render("format", &data)?; + writeln!(out, "{}", s)?; + Ok(()) +} + -- cgit v1.2.3