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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use abnf_core::streaming::SP;
use nom::{
    branch::alt,
    bytes::streaming::tag,
    combinator::map,
    multi::many1,
    sequence::{delimited, tuple},
    IResult,
};

use crate::{
    parse::{
        address::address,
        core::{nil, nstring},
    },
    types::{address::Address, core::nstr, envelope::Envelope},
};

/// envelope = "("
///            env-date SP
///            env-subject SP
///            env-from SP
///            env-sender SP
///            env-reply-to SP
///            env-to SP
///            env-cc SP
///            env-bcc SP
///            env-in-reply-to SP
///            env-message-id
///            ")"
pub(crate) fn envelope(input: &[u8]) -> IResult<&[u8], Envelope> {
    let mut parser = delimited(
        tag(b"("),
        tuple((
            env_date,
            SP,
            env_subject,
            SP,
            env_from,
            SP,
            env_sender,
            SP,
            env_reply_to,
            SP,
            env_to,
            SP,
            env_cc,
            SP,
            env_bcc,
            SP,
            env_in_reply_to,
            SP,
            env_message_id,
        )),
        tag(b")"),
    );

    let (
        remaining,
        (
            date,
            _,
            subject,
            _,
            from,
            _,
            sender,
            _,
            reply_to,
            _,
            to,
            _,
            cc,
            _,
            bcc,
            _,
            in_reply_to,
            _,
            message_id,
        ),
    ) = parser(input)?;

    Ok((
        remaining,
        Envelope {
            date: date.to_owned(),
            subject: subject.to_owned(),
            from,
            sender,
            reply_to,
            to,
            cc,
            bcc,
            in_reply_to: in_reply_to.to_owned(),
            message_id: message_id.to_owned(),
        },
    ))
}

#[inline]
/// env-date = nstring
fn env_date(input: &[u8]) -> IResult<&[u8], nstr> {
    nstring(input)
}

#[inline]
/// env-subject = nstring
fn env_subject(input: &[u8]) -> IResult<&[u8], nstr> {
    nstring(input)
}

/// env-from = "(" 1*address ")" / nil
fn env_from(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

/// env-sender = "(" 1*address ")" / nil
fn env_sender(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

/// env-reply-to = "(" 1*address ")" / nil
fn env_reply_to(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

/// env-to = "(" 1*address ")" / nil
fn env_to(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

/// env-cc = "(" 1*address ")" / nil
fn env_cc(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

/// env-bcc = "(" 1*address ")" / nil
fn env_bcc(input: &[u8]) -> IResult<&[u8], Vec<Address>> {
    alt((
        delimited(tag(b"("), many1(address), tag(b")")),
        map(nil, |_| Vec::new()),
    ))(input)
}

#[inline]
/// env-in-reply-to = nstring
fn env_in_reply_to(input: &[u8]) -> IResult<&[u8], nstr> {
    nstring(input)
}

#[inline]
/// env-message-id = nstring
fn env_message_id(input: &[u8]) -> IResult<&[u8], nstr> {
    nstring(input)
}