diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-03-01 10:41:31 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-06-01 13:59:36 +0200 |
commit | 18b12c698ca8a4fd10d0b68d9c8c657eacf491f2 (patch) | |
tree | 853c880330789eee000588e3ea7539e4815b3b08 | |
parent | 3646d8ab9dfce0b1486ef3c57d94a1f3a6844607 (diff) |
Move configuration handling to dedicated type
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | bin/domain/imag-mail/Cargo.toml | 5 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/config.rs | 66 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/lib.rs | 22 | ||||
-rw-r--r-- | bin/domain/imag-mail/src/util.rs | 45 |
4 files changed, 91 insertions, 47 deletions
diff --git a/bin/domain/imag-mail/Cargo.toml b/bin/domain/imag-mail/Cargo.toml index 1a195a3b..8e5e5a4d 100644 --- a/bin/domain/imag-mail/Cargo.toml +++ b/bin/domain/imag-mail/Cargo.toml @@ -25,6 +25,8 @@ anyhow = "1" resiter = "0.4" handlebars = "2" itertools = "0.8" +serde = "1" +serde_derive = "1" libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } @@ -44,6 +46,9 @@ version = "0.10" default-features = false features = ["typed"] +[dependencies.toml-query_derive] +version = "0.9.2" + [lib] name = "libimagmailfrontend" path = "src/lib.rs" diff --git a/bin/domain/imag-mail/src/config.rs b/bin/domain/imag-mail/src/config.rs new file mode 100644 index 00000000..39cee237 --- /dev/null +++ b/bin/domain/imag-mail/src/config.rs @@ -0,0 +1,66 @@ +// +// 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::path::PathBuf; + +use handlebars::Handlebars; +use failure::Fallible as Result; +use clap::ArgMatches; + +use libimagrt::runtime::Runtime; + +#[derive(Debug, Serialize, Deserialize, Partial)] +#[location = "mail"] +pub struct MailConfig { + #[serde(rename = "list_format")] + list_format: String, + + #[serde(rename = "notmuch_database")] + notmuch_database_path: PathBuf, +} + +impl MailConfig { + + pub fn get_list_format(&self, scmd: &ArgMatches) -> Result<Handlebars> { + let fmt = scmd + .value_of("format") + .map(String::from) + .unwrap_or_else(|| self.list_format.clone()); + + 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 get_notmuch_database_path(&self, rt: &Runtime) -> PathBuf { + if let Some(pb) = rt.cli() + .value_of("database_path") + .map(String::from) + .map(PathBuf::from) + { + pb + } else { + self.notmuch_database_path.clone() + } + } +} diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs index fe7459e8..f0a9eec5 100644 --- a/bin/domain/imag-mail/src/lib.rs +++ b/bin/domain/imag-mail/src/lib.rs @@ -38,9 +38,12 @@ extern crate clap; #[macro_use] extern crate log; #[macro_use] extern crate anyhow; extern crate toml_query; +#[macro_use] extern crate toml_query_derive; extern crate resiter; extern crate handlebars; extern crate itertools; +extern crate serde; +#[macro_use] extern crate serde_derive; extern crate libimagrt; extern crate libimagmail; @@ -62,6 +65,7 @@ use resiter::IterInnerOkOrElse; use resiter::Filter; use resiter::Map; use handlebars::Handlebars; +use toml_query::read::TomlValueReadExt; use libimagmail::store::MailStore; use libimagmail::store::Sorting; @@ -72,9 +76,12 @@ use libimagrt::application::ImagApplication; use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::store::FileLockEntry; +mod config; mod ui; mod util; +use config::MailConfig; + /// Marker enum for implementing ImagApplication on /// /// This is used by binaries crates to execute business logic @@ -115,10 +122,14 @@ impl ImagApplication for ImagMail { } fn import(rt: &Runtime) -> Result<()> { + let config = rt.config() + .ok_or_else(|| err_msg("Configuration missing"))? + .read_partial::<MailConfig>()? + .ok_or_else(|| err_msg("Configuration for \"mail\" missing"))?; let scmd = rt.cli().subcommand_matches("import").unwrap(); let query = scmd.value_of("query").unwrap(); let store = rt.store(); - let notmuch_path = crate::util::get_notmuch_database_path(rt)?; + let notmuch_path = config.get_notmuch_database_path(rt); let notmuch_connection = NotmuchConnection::open(notmuch_path)?; debug!("Importing mail with query = '{}'", query); @@ -135,13 +146,18 @@ fn import(rt: &Runtime) -> Result<()> { } fn list(rt: &Runtime) -> Result<()> { + let config = rt.config() + .ok_or_else(|| err_msg("Configuration missing"))? + .read_partial::<MailConfig>()? + .ok_or_else(|| err_msg("Configuration for \"mail\" missing"))?; + debug!("Listing mail"); let scmd = rt.cli().subcommand_matches("list").unwrap(); let store = rt.store(); - let notmuch_path = crate::util::get_notmuch_database_path(rt)?; + let notmuch_path = config.get_notmuch_database_path(rt); debug!("notmuch path: {:?}", notmuch_path); - let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?; + let list_format = config.get_list_format(&scmd)?; debug!("List-format: {:?}", list_format); let notmuch_connection = NotmuchConnection::open(notmuch_path)?; diff --git a/bin/domain/imag-mail/src/util.rs b/bin/domain/imag-mail/src/util.rs index f7079849..506bfc7a 100644 --- a/bin/domain/imag-mail/src/util.rs +++ b/bin/domain/imag-mail/src/util.rs @@ -19,39 +19,12 @@ use std::collections::BTreeMap; use std::io::Write; -use std::path::PathBuf; - -use clap::ArgMatches; -use anyhow::Error; -use anyhow::Result; +use failure::Fallible as Result; use handlebars::Handlebars; -use toml_query::read::TomlValueReadTypeExt; use libimaginteraction::format::HandlebarsData; use libimagmail::mail::ParsedMail; -use libimagrt::runtime::Runtime; - -pub fn get_mail_print_format<'rc>(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches) -> Result<Handlebars<'rc>> { - let fmt = match scmd.value_of("format").map(String::from) { - Some(s) => Ok(s), - None => { - rt.config() - .ok_or_else(|| anyhow!("No configuration file"))? - .read_string(config_value_path) - .map_err(Error::from)? - .ok_or_else(|| anyhow!("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: &ParsedMail) -> Result<BTreeMap<&'static str, HandlebarsData>> { let mut data = BTreeMap::new(); @@ -95,19 +68,3 @@ pub fn list_mail(m: &ParsedMail, i: usize, indent: usize, list_format: &Handleba Ok(()) } -pub fn get_notmuch_database_path(rt: &Runtime) -> Result<PathBuf> { - if let Some(pb) = rt.cli() - .value_of("database_path") - .map(String::from) - .map(PathBuf::from) - { - return Ok(pb) - } else { - rt.config() - .ok_or_else(|| err_msg("No configuration file"))? - .read_string("mail.notmuch_database") - .map_err(Error::from)? - .map(PathBuf::from) - .ok_or_else(|| format_err!("Configuration 'mail.notmuch_database' does not exist")) - } -} |