| 1 |
//! Rules defined in [RFC 2822]. |
| 2 |
//! |
| 3 |
//! [RFC 2822]: https://datatracker.ietf.org/doc/html/rfc2822 |
| 4 |
|
| 5 |
use crate::parsing::combinator::rfc::rfc2234::wsp; |
| 6 |
use crate::parsing::combinator::{ascii_char, one_or_more, zero_or_more}; |
| 7 |
use crate::parsing::ParsedItem; |
| 8 |
|
| 9 |
/// Consume the `fws` rule. |
| 10 |
// The full rule is equivalent to /\r\n[ \t]+|[ \t]+(?:\r\n[ \t]+)*/ |
| 11 |
pub(crate) fn fws(mut input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 12 |
if let [b'\r', b'\n', rest @ ..] = input { |
| 13 |
one_or_more(wsp)(rest) |
| 14 |
} else { |
| 15 |
input = one_or_more(wsp)(input)?.into_inner(); |
| 16 |
while let [b'\r', b'\n', rest @ ..] = input { |
| 17 |
input = one_or_more(wsp)(rest)?.into_inner(); |
| 18 |
} |
| 19 |
Some(ParsedItem(input, ())) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
/// Consume the `cfws` rule. |
| 24 |
// The full rule is equivalent to any combination of `fws` and `comment` so long as it is not empty. |
| 25 |
pub(crate) fn cfws(input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 26 |
one_or_more(|input| fws(input).or_else(|| comment(input)))(input) |
| 27 |
} |
| 28 |
|
| 29 |
/// Consume the `comment` rule. |
| 30 |
fn comment(mut input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 31 |
input = ascii_char::<b'('>(input)?.into_inner(); |
| 32 |
input = zero_or_more(fws)(input).into_inner(); |
| 33 |
while let Some(rest) = ccontent(input) { |
| 34 |
input = rest.into_inner(); |
| 35 |
input = zero_or_more(fws)(input).into_inner(); |
| 36 |
} |
| 37 |
input = ascii_char::<b')'>(input)?.into_inner(); |
| 38 |
|
| 39 |
Some(ParsedItem(input, ())) |
| 40 |
} |
| 41 |
|
| 42 |
/// Consume the `ccontent` rule. |
| 43 |
fn ccontent(input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 44 |
ctext(input) |
| 45 |
.or_else(|| quoted_pair(input)) |
| 46 |
.or_else(|| comment(input)) |
| 47 |
} |
| 48 |
|
| 49 |
/// Consume the `ctext` rule. |
| 50 |
#[allow(clippy::unnecessary_lazy_evaluations)] // rust-lang/rust-clippy#8522 |
| 51 |
fn ctext(input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 52 |
no_ws_ctl(input).or_else(|| match input { |
| 53 |
[33..=39 | 42..=91 | 93..=126, rest @ ..] => Some(ParsedItem(rest, ())), |
| 54 |
_ => None, |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
/// Consume the `quoted_pair` rule. |
| 59 |
fn quoted_pair(mut input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 60 |
input = ascii_char::<b'\\'>(input)?.into_inner(); |
| 61 |
|
| 62 |
let old_input_len = input.len(); |
| 63 |
|
| 64 |
input = text(input).into_inner(); |
| 65 |
|
| 66 |
// If nothing is parsed, this means we hit the `obs-text` rule and nothing matched. This is |
| 67 |
// technically a success, but we should still check the `obs-qp` rule to ensure we consume |
| 68 |
// everything possible. |
| 69 |
if input.len() == old_input_len { |
| 70 |
match input { |
| 71 |
[0..=127, rest @ ..] => Some(ParsedItem(rest, ())), |
| 72 |
_ => Some(ParsedItem(input, ())), |
| 73 |
} |
| 74 |
} else { |
| 75 |
Some(ParsedItem(input, ())) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/// Consume the `no_ws_ctl` rule. |
| 80 |
const fn no_ws_ctl(input: &[u8]) -> Option<ParsedItem<'_, ()>> { |
| 81 |
match input { |
| 82 |
[1..=8 | 11..=12 | 14..=31 | 127, rest @ ..] => Some(ParsedItem(rest, ())), |
| 83 |
_ => None, |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/// Consume the `text` rule. |
| 88 |
fn text<'a>(input: &'a [u8]) -> ParsedItem<'a, ()> { |
| 89 |
let new_text = |input: &'a [u8]| match input { |
| 90 |
[1..=9 | 11..=12 | 14..=127, rest @ ..] => Some(ParsedItem(rest, ())), |
| 91 |
_ => None, |
| 92 |
}; |
| 93 |
|
| 94 |
let obs_char = |input: &'a [u8]| match input { |
| 95 |
// This is technically allowed, but consuming this would mean the rest of the string is |
| 96 |
// eagerly consumed without consideration for where the comment actually ends. |
| 97 |
[b')', ..] => None, |
| 98 |
[0..=9 | 11..=12 | 14..=127, rest @ ..] => Some(rest), |
| 99 |
_ => None, |
| 100 |
}; |
| 101 |
|
| 102 |
let obs_text = |mut input| { |
| 103 |
input = zero_or_more(ascii_char::<b'\n'>)(input).into_inner(); |
| 104 |
input = zero_or_more(ascii_char::<b'\r'>)(input).into_inner(); |
| 105 |
while let Some(rest) = obs_char(input) { |
| 106 |
input = rest; |
| 107 |
input = zero_or_more(ascii_char::<b'\n'>)(input).into_inner(); |
| 108 |
input = zero_or_more(ascii_char::<b'\r'>)(input).into_inner(); |
| 109 |
} |
| 110 |
|
| 111 |
ParsedItem(input, ()) |
| 112 |
}; |
| 113 |
|
| 114 |
new_text(input).unwrap_or_else(|| obs_text(input)) |
| 115 |
} |