summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/config.rs
blob: a7a991d7d4b79d8c11410a7015680a11615c43d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// 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,

    #[serde(rename = "import_tag")]
    import_tag: Option<String>,

    #[serde(rename = "import_notmuch_tags")]
    import_notmuch_tags: bool,

    #[serde(rename = "edit_headers")]
    edit_headers: bool,

    #[serde(rename = "default_template")]
    default_template: String,

    #[serde(rename = "header_template")]
    header_template: String,

    #[serde(rename = "from_address")]
    from_address: String,

}

impl MailConfig {

    pub fn get_list_format_or_cli(&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)
    }

    /// Get the notmuch database path either from CLI or from config
    pub fn get_notmuch_database_path_or_cli(&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()
        }
    }

    pub fn get_import_tag(&self) -> Option<&String> {
        self.import_tag.as_ref()
    }

    pub fn get_import_notmuch_tags(&self) -> bool {
        self.import_notmuch_tags
    }

    pub fn get_edit_headers(&self) -> bool {
        self.edit_headers
    }

    pub fn get_default_template(&self) -> &String {
        &self.default_template
    }

    pub fn get_header_template(&self) -> &String {
        &self.header_template
    }

    pub fn get_from_address(&self) -> &String {
        &self.from_address
    }
}