diff --git a/src/error.rs b/src/error.rs index 0ad4707..2bd721a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,4 +3,5 @@ use nom; #[derive(Debug, PartialEq)] pub enum IMFError<'a> { Segment(nom::Err>), + ExtractFields(nom::Err>), } diff --git a/src/fragments/mod.rs b/src/fragments/mod.rs index 0d773c1..3724153 100644 --- a/src/fragments/mod.rs +++ b/src/fragments/mod.rs @@ -2,7 +2,7 @@ pub mod model; // Generic -mod whitespace; +pub mod whitespace; mod words; mod quoted; mod misc_token; diff --git a/src/multipass/extract_fields.rs b/src/multipass/extract_fields.rs index e69de29..7bcb585 100644 --- a/src/multipass/extract_fields.rs +++ b/src/multipass/extract_fields.rs @@ -0,0 +1,45 @@ +use std::borrow::Cow; +use nom::{ + IResult, + character::complete::space1, + bytes::complete::is_not, + combinator::{all_consuming, recognize}, + multi::{fold_many0, many0, many1}, + sequence::{pair, tuple}, +}; + +use crate::multipass::guess_charset::GuessCharset; +use crate::error::IMFError; +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> for ExtractFields<'a> { + type Error = IMFError<'a>; + + fn try_from(gcha: GuessCharset<'a>) -> Result { + 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) + } +} + +/// ```abnf +/// fold_line = !crlf *(1*(crlf WS) !crlf) crlf +/// ``` +fn foldable_line<'a>(input: Cow<'a, str>) -> IResult, Cow<'a, str>> { + recognize(tuple(( + is_not("\r\n"), + many0(pair( + many1(pair(whitespace::perm_crlf, space1)), + is_not("\r\n"))), + whitespace::perm_crlf + )))(input) +} diff --git a/src/multipass/mod.rs b/src/multipass/mod.rs index e3fa3b1..7d24bda 100644 --- a/src/multipass/mod.rs +++ b/src/multipass/mod.rs @@ -1,2 +1,3 @@ pub mod segment; pub mod guess_charset; +pub mod extract_fields;