extract fields
This commit is contained in:
parent
a7efac53c2
commit
6c2ee3ee67
2 changed files with 35 additions and 14 deletions
|
@ -14,27 +14,24 @@ use crate::fragments::whitespace;
|
|||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ExtractFields<'a> {
|
||||
pub raw_header: Cow<'a, str>,
|
||||
pub fields: Vec<&'a str>,
|
||||
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>;
|
||||
|
||||
fn try_from(gcha: GuessCharset<'a>) -> Result<Self, Self::Error> {
|
||||
let mut ef = ExtractFields { fields: vec![], raw_header: gcha.header, body: gcha.body };
|
||||
let (_, fields) = all_consuming(many0(foldable_line))(ef.raw_header).map_err(|e| IMFError::ExtractFields(e))?;
|
||||
panic!();
|
||||
//ef.fields = fields;
|
||||
//Ok(ef)
|
||||
fn try_from(gcha: &'a GuessCharset<'a>) -> Result<Self, Self::Error> {
|
||||
all_consuming(many0(foldable_line))(&gcha.header)
|
||||
.map_err(|e| IMFError::ExtractFields(e))
|
||||
.map(|(_, fields)| ExtractFields { fields, body: gcha.body })
|
||||
}
|
||||
}
|
||||
|
||||
/// ```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((
|
||||
is_not("\r\n"),
|
||||
many0(pair(
|
||||
|
@ -43,3 +40,27 @@ fn foldable_line<'a>(input: Cow<'a, str>) -> IResult<Cow<'a, str>, Cow<'a, str>>
|
|||
whitespace::perm_crlf
|
||||
)))(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!",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,8 @@ const IS_LAST_BUFFER: bool = true;
|
|||
const ALLOW_UTF8: bool = true;
|
||||
const NO_TLD: Option<&[u8]> = None;
|
||||
|
||||
impl<'a> From<Segment<'a>> for GuessCharset<'a> {
|
||||
fn from(seg: Segment<'a>) -> Self {
|
||||
impl<'a> From<&'a Segment<'a>> for GuessCharset<'a> {
|
||||
fn from(seg: &'a Segment<'a>) -> Self {
|
||||
// Create detector
|
||||
let mut detector = EncodingDetector::new();
|
||||
detector.feed(&seg.header, IS_LAST_BUFFER);
|
||||
|
@ -37,12 +37,12 @@ mod tests {
|
|||
#[test]
|
||||
fn test_charset() {
|
||||
assert_eq!(
|
||||
GuessCharset::from(Segment {
|
||||
GuessCharset::from(&Segment {
|
||||
body: b"Hello world!",
|
||||
header: b"From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n",
|
||||
}),
|
||||
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,
|
||||
malformed: false,
|
||||
body: b"Hello world!",
|
||||
|
|
Loading…
Reference in a new issue