summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/util.rs
blob: 71ca1b2359882abe01f9371686954e973261140b (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
//
// 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 std::io::Write;

use anyhow::Result;
use handlebars::Handlebars;

use libimaginteraction::format::HandlebarsData;
use libimagmail::mail::ParsedMail;

pub fn build_data_object_for_handlebars(i: usize, m: &ParsedMail) -> Result<BTreeMap<&'static str, HandlebarsData>> {
    let mut data = BTreeMap::new();

    data.insert("i", HandlebarsData::Int(i));

    let id = match m.message_id()? {
        Some(f) => f,
        None => "<no id>".to_owned(),
    };

    let from = match m.from()? {
        Some(f) => f,
        None => "<no from>".to_owned(),
    };

    let to = match m.to()? {
        Some(f) => f,
        None => "<no to>".to_owned(),
    };

    let subject = match m.subject()? {
        Some(f) => f,
        None => "<no subject>".to_owned(),
    };

    data.insert("id"      , HandlebarsData::Str(id));
    data.insert("from"    , HandlebarsData::Str(from));
    data.insert("to"      , HandlebarsData::Str(to));
    data.insert("subject" , HandlebarsData::Str(subject));

    Ok(data)
}

pub fn list_mail(m: &ParsedMail, i: usize, indent: usize, list_format: &Handlebars, out: &mut dyn Write) -> Result<()> {
    trace!("Listing mail no. {} ({:?}) with indent {}", i, m.message_id(), indent);
    let mut data = build_data_object_for_handlebars(i, m)?;
    data.insert("indent", HandlebarsData::Int(indent));
    let s    = list_format.render("format", &data)?;
    writeln!(out, "{}", s)?;
    Ok(())
}