summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/util.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-02 21:05:28 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-03 13:04:59 +0100
commit0ad852f6c1f1386ebdc5d59076279abade1c539d (patch)
tree3f0a8203963d41b71093f9e4a97641aecbf74e52 /bin/domain/imag-mail/src/util.rs
parentd80ef3bb7906a3adef8a3318a0051e6ebb742284 (diff)
Add support for format in configuration for imag-mail-list
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'bin/domain/imag-mail/src/util.rs')
-rw-r--r--bin/domain/imag-mail/src/util.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/bin/domain/imag-mail/src/util.rs b/bin/domain/imag-mail/src/util.rs
new file mode 100644
index 00000000..77d6b5a5
--- /dev/null
+++ b/bin/domain/imag-mail/src/util.rs
@@ -0,0 +1,87 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use std::collections::BTreeMap;
+
+use clap::ArgMatches;
+use failure::Error;
+use failure::Fallible as Result;
+use failure::err_msg;
+use handlebars::Handlebars;
+use toml_query::read::TomlValueReadTypeExt;
+
+use libimagmail::mail::Mail;
+use libimagrt::runtime::Runtime;
+use libimagstore::store::FileLockEntry;
+use libimagentryref::reference::Config as RefConfig;
+
+pub fn get_mail_print_format(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches) -> Result<Handlebars> {
+ let fmt = match scmd.value_of("format").map(String::from) {
+ Some(s) => Ok(s),
+ None => {
+ rt.config()
+ .ok_or_else(|| err_msg("No configuration file"))?
+ .read_string(config_value_path)
+ .map_err(Error::from)?
+ .ok_or_else(|| format_err!("Configuration '{}' does not exist", config_value_path))
+ }
+ }?;
+
+ let mut hb = Handlebars::new();
+ hb.register_template_string("format", fmt)?;
+
+ hb.register_escape_fn(::handlebars::no_escape);
+ ::libimaginteraction::format::register_all_color_helpers(&mut hb);
+ ::libimaginteraction::format::register_all_format_helpers(&mut hb);
+ Ok(hb)
+}
+
+pub fn build_data_object_for_handlebars(i: usize, m: &FileLockEntry, refconfig: &RefConfig) -> Result<BTreeMap<&'static str, String>> {
+ let mut data = BTreeMap::new();
+
+ data.insert("i", format!("{}", i));
+
+ 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(),
+ };
+
+ data.insert("id" , id);
+ data.insert("from" , from);
+ data.insert("to" , to);
+ data.insert("subject" , subject);
+
+ Ok(data)
+}
+