extract fields

This commit is contained in:
Quentin 2023-06-19 21:18:23 +02:00
parent a7efac53c2
commit 6c2ee3ee67
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 35 additions and 14 deletions

View file

@ -14,27 +14,24 @@ use crate::fragments::whitespace;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct ExtractFields<'a> { pub struct ExtractFields<'a> {
pub raw_header: Cow<'a, str>,
pub fields: Vec<&'a str>, pub fields: Vec<&'a str>,
pub body: &'a [u8], pub body: &'a [u8],
} }
impl<'a> TryFrom<GuessCharset<'a>> for ExtractFields<'a> { impl<'a> TryFrom<&'a GuessCharset<'a>> for ExtractFields<'a> {
type Error = IMFError<'a>; type Error = IMFError<'a>;
fn try_from(gcha: GuessCharset<'a>) -> Result<Self, Self::Error> { fn try_from(gcha: &'a GuessCharset<'a>) -> Result<Self, Self::Error> {
let mut ef = ExtractFields { fields: vec![], raw_header: gcha.header, body: gcha.body }; all_consuming(many0(foldable_line))(&gcha.header)
let (_, fields) = all_consuming(many0(foldable_line))(ef.raw_header).map_err(|e| IMFError::ExtractFields(e))?; .map_err(|e| IMFError::ExtractFields(e))
panic!(); .map(|(_, fields)| ExtractFields { fields, body: gcha.body })
//ef.fields = fields;
//Ok(ef)
} }
} }
/// ```abnf /// ```abnf
/// fold_line = !crlf *(1*(crlf WS) !crlf) crlf /// fold_line = any *(1*(crlf WS) any) crlf
/// ``` /// ```
fn foldable_line<'a>(input: Cow<'a, str>) -> IResult<Cow<'a, str>, Cow<'a, str>> { fn foldable_line(input: &str) -> IResult<&str, &str> {
recognize(tuple(( recognize(tuple((
is_not("\r\n"), is_not("\r\n"),
many0(pair( many0(pair(
@ -43,3 +40,27 @@ fn foldable_line<'a>(input: Cow<'a, str>) -> IResult<Cow<'a, str>, Cow<'a, str>>
whitespace::perm_crlf whitespace::perm_crlf
)))(input) )))(input)
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract() {
assert_eq!(
ExtractFields::try_from(&GuessCharset {
header: "From: hello@world.com,\r\n\talice@wonderlands.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n".into(),
encoding: encoding_rs::UTF_8,
malformed: false,
body: b"Hello world!",
}),
Ok(ExtractFields {
fields: vec![
"From: hello@world.com,\r\n\talice@wonderlands.com\r\n",
"Date: 12 Mar 1997 07:33:25 Z\r\n",
],
body: b"Hello world!",
})
);
}
}

View file

@ -16,8 +16,8 @@ const IS_LAST_BUFFER: bool = true;
const ALLOW_UTF8: bool = true; const ALLOW_UTF8: bool = true;
const NO_TLD: Option<&[u8]> = None; const NO_TLD: Option<&[u8]> = None;
impl<'a> From<Segment<'a>> for GuessCharset<'a> { impl<'a> From<&'a Segment<'a>> for GuessCharset<'a> {
fn from(seg: Segment<'a>) -> Self { fn from(seg: &'a Segment<'a>) -> Self {
// Create detector // Create detector
let mut detector = EncodingDetector::new(); let mut detector = EncodingDetector::new();
detector.feed(&seg.header, IS_LAST_BUFFER); detector.feed(&seg.header, IS_LAST_BUFFER);
@ -37,12 +37,12 @@ mod tests {
#[test] #[test]
fn test_charset() { fn test_charset() {
assert_eq!( assert_eq!(
GuessCharset::from(Segment { GuessCharset::from(&Segment {
body: b"Hello world!", body: b"Hello world!",
header: b"From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n", header: b"From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n",
}), }),
GuessCharset { GuessCharset {
header: Cow::Borrowed("From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n"), header: "From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n".into(),
encoding: encoding_rs::UTF_8, encoding: encoding_rs::UTF_8,
malformed: false, malformed: false,
body: b"Hello world!", body: b"Hello world!",