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
171
172
173
use anyhow::Result;
use nom::{
error::{ErrorKind, ParseError, VerboseError},
CompareResult, Err, IResult, InputLength, Needed, Parser, ToUsize,
};
use super::bytes::{ShitCompare, ShitNeededForParsing};
use super::rfc2234::is_digit;
pub type VResult<I, O> = IResult<I, O, VerboseError<I>>;
macro_rules! sep_list {
($t:expr) => {
map(
pair($t, many0(preceded(crate::proto::rfc2234::SP, $t))),
|(hd, mut tl)| {
tl.insert(0, hd);
tl
},
)
};
($t:expr, $d:expr) => {
map(pair($t, many0(preceded($d, $t))), |(hd, mut tl)| {
tl.insert(0, hd);
tl
})
};
(? $t:expr) => {
map(
opt(pair($t, many0(preceded(crate::proto::rfc2234::SP, $t)))),
|opt| {
opt.map(|(hd, mut tl)| {
tl.insert(0, hd);
tl
})
.unwrap_or_else(Vec::new)
},
)
};
(? $t:expr, $d:expr) => {
map(opt(pair($t, many0(preceded($d, $t)))), |opt| {
opt.map(|(hd, mut tl)| {
tl.insert(0, hd);
tl
})
.unwrap_or_else(Vec::new)
})
};
}
macro_rules! opt_nil {
($t:expr) => {
alt((map($t, Some), map(crate::proto::rfc3501::nil, |_| None)))
};
}
macro_rules! paren {
($t:expr) => {
delimited(byte(b'('), $t, byte(b')'))
};
}
pub fn parse_u32(s: impl AsRef<[u8]>) -> Result<u32> {
let mut total = 0u32;
let s = s.as_ref();
for digit in s.iter() {
total *= 10;
if !is_digit(*digit) {
bail!("invalid digit {}", digit)
}
total += (*digit - b'\x30') as u32;
}
Ok(total)
}
pub fn never<I, O, E>(i: I) -> IResult<I, O, E>
where
E: ParseError<I>,
{
Err(Err::Error(E::from_error_kind(i, ErrorKind::Not)))
}
pub fn skip<E, F, I, O>(mut f: F) -> impl FnMut(I) -> IResult<I, (), E>
where
I: Clone,
F: Parser<I, O, E>,
{
move |i: I| match f.parse(i.clone()) {
Ok(_) => Ok((i, ())),
Err(err) => Err(err),
}
}
pub fn take<C, I, E>(count: C) -> impl Fn(I) -> IResult<I, I, E>
where
I: ShitNeededForParsing + InputLength,
E: ParseError<I>,
C: ToUsize,
{
let c = count.to_usize();
move |i: I| match i.slice_index(c) {
Err(i) => Err(Err::Incomplete(i)),
Ok(index) => Ok(i.take_split(index)),
}
}
pub fn take_while1<F, I, E, T>(cond: F) -> impl Fn(I) -> IResult<I, I, E>
where
I: ShitNeededForParsing<Item = T>,
E: ParseError<I>,
F: Fn(T) -> bool,
{
move |i: I| {
let e: ErrorKind = ErrorKind::TakeWhile1;
i.split_at_position1(|c| !cond(c), e)
}
}
pub fn tagi<T, I, E>(tag: T) -> impl Fn(I) -> IResult<I, I, E>
where
I: ShitNeededForParsing<Sliced = I> + InputLength + ShitCompare<T>,
T: InputLength + Clone,
E: ParseError<I>,
{
let tag_len = tag.input_len();
move |i: I| match i.compare_no_case(tag.clone()) {
CompareResult::Ok => {
let fst = i.slice(0..tag_len);
let snd = i.slice(tag_len..);
Ok((snd, fst))
}
CompareResult::Incomplete => Err(Err::Incomplete(Needed::new(tag_len - i.input_len()))),
CompareResult::Error => {
let e: ErrorKind = ErrorKind::Tag;
Err(Err::Error(E::from_error_kind(i, e)))
}
}
}
pub fn satisfy<F, I, E, T>(f: F) -> impl Fn(I) -> IResult<I, T, E>
where
I: ShitNeededForParsing<Item = T, Sliced = I>,
F: Fn(T) -> bool,
E: ParseError<I>,
T: Copy,
{
move |i: I| match i.first().map(|t| (f(t), t)) {
Some((true, ft)) => Ok((i.slice(1..), ft)),
Some((false, _)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Satisfy))),
None => Err(Err::Incomplete(Needed::Unknown)),
}
}
pub fn byte<I, E: ParseError<I>>(b: u8) -> impl Fn(I) -> IResult<I, u8, E>
where
I: ShitNeededForParsing<Item = u8, Sliced = I>,
{
satisfy(move |c| c == b)
}