This commit is contained in:
Quentin 2023-06-19 19:03:33 +02:00
parent 2bc62edba8
commit a7efac53c2
Signed by: quentin
GPG key ID: E9602264D639FF68
4 changed files with 48 additions and 1 deletions

View file

@ -3,4 +3,5 @@ use nom;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum IMFError<'a> { pub enum IMFError<'a> {
Segment(nom::Err<nom::error::Error<&'a [u8]>>), Segment(nom::Err<nom::error::Error<&'a [u8]>>),
ExtractFields(nom::Err<nom::error::Error<&'a str>>),
} }

View file

@ -2,7 +2,7 @@
pub mod model; pub mod model;
// Generic // Generic
mod whitespace; pub mod whitespace;
mod words; mod words;
mod quoted; mod quoted;
mod misc_token; mod misc_token;

View file

@ -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<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)
}
}
/// ```abnf
/// fold_line = !crlf *(1*(crlf WS) !crlf) crlf
/// ```
fn foldable_line<'a>(input: Cow<'a, str>) -> IResult<Cow<'a, str>, 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)
}

View file

@ -1,2 +1,3 @@
pub mod segment; pub mod segment;
pub mod guess_charset; pub mod guess_charset;
pub mod extract_fields;