summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bin/domain/imag-mail/src/lib.rs')
-rw-r--r--bin/domain/imag-mail/src/lib.rs92
1 files changed, 15 insertions, 77 deletions
diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs
index 0dd7f2bb..f428bf3f 100644
--- a/bin/domain/imag-mail/src/lib.rs
+++ b/bin/domain/imag-mail/src/lib.rs
@@ -38,8 +38,8 @@ extern crate clap;
#[macro_use] extern crate log;
#[macro_use] extern crate failure;
extern crate toml_query;
-#[macro_use] extern crate indoc;
extern crate resiter;
+extern crate handlebars;
extern crate libimagrt;
extern crate libimagmail;
@@ -47,6 +47,7 @@ extern crate libimagerror;
extern crate libimagstore;
extern crate libimagutil;
extern crate libimagentryref;
+extern crate libimaginteraction;
use std::io::Write;
use std::path::PathBuf;
@@ -59,19 +60,16 @@ use clap::App;
use resiter::AndThen;
use resiter::IterInnerOkOrElse;
-use libimagmail::mail::Mail;
use libimagmail::store::MailStore;
-use libimagmail::util;
-use libimagentryref::reference::{Ref, RefFassade};
use libimagentryref::util::get_ref_config;
use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;
use libimagutil::info_result::*;
-use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
mod ui;
+mod util;
/// Marker enum for implementing ImagApplication on
///
@@ -141,77 +139,10 @@ fn import_mail(rt: &Runtime) -> Result<()> {
}
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 print_content = scmd.is_present("list-read");
-
- if print_content {
- // TODO: Check whether workaround with "{}" is still necessary when updating "indoc"
- warn!("{}", indoc!(r#"You requested to print the content of the mail as well.
- We use the 'mailparse' crate underneath, but its implementation is nonoptimal.
- Thus, the content might be printed as empty (no text in the email)
- This is not reliable and might be wrong."#));
-
- // TODO: Fix above.
- }
-
- // TODO: Implement lister type in libimagmail for this
- //
- // Optimization: Pass refconfig here instead of call get_ref_config() in lister function. This
- // way we do not call get_ref_config() multiple times.
- fn list_mail<'a>(rt: &Runtime,
- refconfig: &::libimagentryref::reference::Config,
- m: &FileLockEntry<'a>,
- print_content: bool) -> Result<()> {
-
- let id = match m.get_message_id(&refconfig)? {
- Some(f) => f.into(),
- None => "<no id>".to_owned(),
- };
-
- let from = match m.get_from(&refconfig)? {
- Some(f) => f,
- None => "<no from>".to_owned(),
- };
-
- let to = match m.get_to(&refconfig)? {
- Some(f) => f,
- None => "<no to>".to_owned(),
- };
-
- let subject = match m.get_subject(&refconfig)? {
- Some(f) => f,
- None => "<no subject>".to_owned(),
- };
-
- if print_content {
- use libimagmail::hasher::MailHasher;
-
- let content = m.as_ref_with_hasher::<MailHasher>()
- .get_path(&refconfig)
- .and_then(util::get_mail_text_content)?;
-
- writeln!(rt.stdout(),
- "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n---\n{content}\n---\n",
- from = from,
- id = id,
- subj = subject,
- to = to,
- content = content
- )?;
- } else {
- writeln!(rt.stdout(),
- "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n",
- from = from,
- id = id,
- subj = subject,
- to = to
- )?;
- }
-
- rt.report_touched(m.get_location())?;
- Ok(())
- }
+ 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 i = 0;
if rt.ids_from_stdin() {
let iter = rt
@@ -229,7 +160,14 @@ fn list(rt: &Runtime) -> Result<()> {
.inspect(|id| debug!("Found: {:?}", id))
.into_get_iter(rt.store())
.map_inner_ok_or_else(|| err_msg("Did not find one entry"))
- .and_then_ok(|m| list_mail(&rt, &refconfig, &m, print_content))
+ .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)?;
+ rt.report_touched(m.get_location())?;
+ i += 1;
+ Ok(())
+ })
.collect::<Result<Vec<_>>>()
.map(|_| ())
}