summaryrefslogtreecommitdiff
path: root/melib/src/addressbook/mutt.rs
blob: 805c35463e1314069a967b95718a93a45a9d281b (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
/*
 * meli - addressbook module
 *
 * Copyright 2019 Manos Pitsidianakis
 *
 * This file is part of meli.
 *
 * meli is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * meli 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with meli. If not, see <http://www.gnu.org/licenses/>.
 */

//! # Mutt contact formats
//!

use super::*;
use crate::parsec::{is_not, map_res, match_literal_anycase, prefix, Parser};
use std::collections::VecDeque;

//alias <nickname> [ <long name> ] <address>
// From mutt doc:
//
// ```text
// Since the name can consist of several whitespace-separated words, the
// last word is considered the address, and it can be optionally enclosed
// between angle brackets.
// For example: alias mumon My dear pupil Mumon foobar@example.com
// will be parsed in this way:
//
// alias mumon      My dear pupil Mumon foobar@example.com
//       ^          ^                   ^
//       nickname   long name           email address
// The nickname (or alias) will be used to select a corresponding long name
// and email address when specifying the To field of an outgoing message,
// e.g. when using the  function in the browser or index context.
// The long name is optional, so you can specify an alias command in this
// way:
//
// alias mumon      foobar@example.com
//       ^          ^
//       nickname   email address
// ```
pub fn parse_mutt_contact<'a>() -> impl Parser<'a, Card> {
    move |input| {
        map_res(
            prefix(match_literal_anycase("alias "), is_not(b"\r\n")),
            |l| {
                let mut tokens = l.split_whitespace().collect::<VecDeque<&str>>();

                let mut ret = Card::new();
                let title = tokens.pop_front().ok_or(l)?.to_string();
                let mut email = tokens.pop_back().ok_or(l)?.to_string();
                if email.starts_with('<') && email.ends_with('>') {
                    email.pop();
                    email.remove(0);
                }
                let mut name = tokens.into_iter().fold(String::new(), |mut acc, el| {
                    acc.push_str(el);
                    acc.push(' ');
                    acc
                });
                name.pop();
                if name.trim().is_empty() {
                    name = title.clone();
                }
                ret.set_title(title).set_email(email).set_name(name);
                Ok::<Card, &'a str>(ret)
            },
        )
        .parse(input)
    }
}

#[test]
fn test_mutt_contacts() {
    let a = "alias mumon      My dear pupil Mumon foobar@example.com";
    let b = "alias mumon      foobar@example.com";
    let c = "alias <nickname> <long name> <address>";

    let (other, a_card) = parse_mutt_contact().parse(a).unwrap();
    assert!(other.is_empty());
    assert_eq!(a_card.name(), "My dear pupil Mumon");
    assert_eq!(a_card.title(), "mumon");
    assert_eq!(a_card.email(), "foobar@example.com");

    let (other, b_card) = parse_mutt_contact().parse(b).unwrap();
    assert!(other.is_empty());
    assert_eq!(b_card.name(), "mumon");
    assert_eq!(b_card.title(), "mumon");
    assert_eq!(b_card.email(), "foobar@example.com");

    let (other, c_card) = parse_mutt_contact().parse(c).unwrap();
    assert!(other.is_empty());
    assert_eq!(c_card.name(), "<long name>");
    assert_eq!(c_card.title(), "<nickname>");
    assert_eq!(c_card.email(), "address");
}