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
use std::num::NonZeroU32;

use abnf_core::streaming::SP;
use imap_types::{
    core::{AString, NonEmptyVec},
    section::{Part, PartSpecifier, Section},
};
use nom::{
    branch::alt,
    bytes::streaming::{tag, tag_no_case},
    combinator::{map, opt, value},
    multi::separated_list1,
    sequence::{delimited, tuple},
    IResult,
};

use crate::rfc3501::core::{astring, nz_number};

/// `section = "[" [section-spec] "]"`
pub fn section(input: &[u8]) -> IResult<&[u8], Option<Section>> {
    delimited(tag(b"["), opt(section_spec), tag(b"]"))(input)
}

/// `section-spec = section-msgtext / (section-part ["." section-text])`
pub fn section_spec(input: &[u8]) -> IResult<&[u8], Section> {
    alt((
        map(section_msgtext, |part_specifier| match part_specifier {
            PartSpecifier::PartNumber(_) => unreachable!(),
            PartSpecifier::Header => Section::Header(None),
            PartSpecifier::HeaderFields(fields) => Section::HeaderFields(None, fields),
            PartSpecifier::HeaderFieldsNot(fields) => Section::HeaderFieldsNot(None, fields),
            PartSpecifier::Text => Section::Text(None),
            PartSpecifier::Mime => unreachable!(),
        }),
        map(
            tuple((section_part, opt(tuple((tag(b"."), section_text))))),
            |(part_number, maybe_part_specifier)| {
                if let Some((_, part_specifier)) = maybe_part_specifier {
                    match part_specifier {
                        PartSpecifier::PartNumber(_) => unreachable!(),
                        PartSpecifier::Header => Section::Header(Some(Part(part_number))),
                        PartSpecifier::HeaderFields(fields) => {
                            Section::HeaderFields(Some(Part(part_number)), fields)
                        }
                        PartSpecifier::HeaderFieldsNot(fields) => {
                            Section::HeaderFieldsNot(Some(Part(part_number)), fields)
                        }
                        PartSpecifier::Text => Section::Text(Some(Part(part_number))),
                        PartSpecifier::Mime => Section::Mime(Part(part_number)),
                    }
                } else {
                    Section::Part(Part(part_number))
                }
            },
        ),
    ))(input)
}

/// `section-msgtext = "HEADER" / "HEADER.FIELDS" [".NOT"] SP header-list / "TEXT"`
///
/// Top-level or MESSAGE/RFC822 part
pub fn section_msgtext(input: &[u8]) -> IResult<&[u8], PartSpecifier> {
    alt((
        map(
            tuple((tag_no_case(b"HEADER.FIELDS.NOT"), SP, header_list)),
            |(_, _, header_list)| PartSpecifier::HeaderFieldsNot(header_list),
        ),
        map(
            tuple((tag_no_case(b"HEADER.FIELDS"), SP, header_list)),
            |(_, _, header_list)| PartSpecifier::HeaderFields(header_list),
        ),
        value(PartSpecifier::Header, tag_no_case(b"HEADER")),
        value(PartSpecifier::Text, tag_no_case(b"TEXT")),
    ))(input)
}

#[inline]
/// `section-part = nz-number *("." nz-number)`
///
/// Body part nesting
pub fn section_part(input: &[u8]) -> IResult<&[u8], NonEmptyVec<NonZeroU32>> {
    map(separated_list1(tag(b"."), nz_number), |vec| unsafe {
        NonEmptyVec::new_unchecked(vec)
    })(input)
}

/// `section-text = section-msgtext / "MIME"`
///
/// Text other than actual body part (headers, etc.)
pub fn section_text(input: &[u8]) -> IResult<&[u8], PartSpecifier> {
    alt((
        section_msgtext,
        value(PartSpecifier::Mime, tag_no_case(b"MIME")),
    ))(input)
}

/// `header-list = "(" header-fld-name *(SP header-fld-name) ")"`
pub fn header_list(input: &[u8]) -> IResult<&[u8], NonEmptyVec<AString>> {
    map(
        delimited(tag(b"("), separated_list1(SP, header_fld_name), tag(b")")),
        |vec| unsafe { NonEmptyVec::new_unchecked(vec) },
    )(input)
}

#[inline]
/// `header-fld-name = astring`
pub fn header_fld_name(input: &[u8]) -> IResult<&[u8], AString> {
    astring(input)
}