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
use std::{convert::TryFrom, num::NonZeroU32};
use abnf_core::streaming::SP;
use nom::{
branch::alt,
bytes::streaming::{tag, tag_no_case},
combinator::{map, opt, value},
multi::separated_list1,
sequence::{delimited, tuple},
IResult,
};
use crate::{
parse::core::{astring, nz_number},
types::{
core::{astr, AString, NonEmptyVec},
fetch_attributes::{Part, PartSpecifier, Section},
},
};
pub(crate) fn section(input: &[u8]) -> IResult<&[u8], Option<Section>> {
delimited(tag(b"["), opt(section_spec), tag(b"]"))(input)
}
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)
}
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(
NonEmptyVec::try_from(
header_list
.iter()
.map(|item| item.to_owned())
.collect::<Vec<AString>>(),
)
.unwrap(),
)
},
),
map(
tuple((tag_no_case(b"HEADER.FIELDS"), SP, header_list)),
|(_, _, header_list)| {
PartSpecifier::HeaderFields(
NonEmptyVec::try_from(
header_list
.iter()
.map(|item| item.to_owned())
.collect::<Vec<AString>>(),
)
.unwrap(),
)
},
),
value(PartSpecifier::Header, tag_no_case(b"HEADER")),
value(PartSpecifier::Text, tag_no_case(b"TEXT")),
))(input)
}
#[inline]
fn section_part(input: &[u8]) -> IResult<&[u8], NonEmptyVec<NonZeroU32>> {
map(separated_list1(tag(b"."), nz_number), |vec| {
NonEmptyVec::try_from(vec).unwrap()
})(input)
}
fn section_text(input: &[u8]) -> IResult<&[u8], PartSpecifier> {
alt((
section_msgtext,
value(PartSpecifier::Mime, tag_no_case(b"MIME")),
))(input)
}
fn header_list(input: &[u8]) -> IResult<&[u8], NonEmptyVec<astr>> {
map(
delimited(tag(b"("), separated_list1(SP, header_fld_name), tag(b")")),
|vec| NonEmptyVec::try_from(vec).unwrap(),
)(input)
}
#[inline]
pub(crate) fn header_fld_name(input: &[u8]) -> IResult<&[u8], astr> {
astring(input)
}