add a preamble test

This commit is contained in:
Quentin 2023-07-17 11:44:55 +02:00
parent 337d1a47c4
commit aa2c741921
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 60 additions and 5 deletions

View file

@ -1,14 +1,15 @@
use nom::{
IResult,
bytes::complete::tag,
sequence::tuple,
combinator::opt,
bytes::complete::{is_not, tag},
multi::many0,
sequence::{pair, tuple},
combinator::{not, opt, recognize},
};
use crate::fragments::mime::{Mechanism, Type};
use crate::fragments::model::MessageId;
use crate::fragments::misc_token::Unstructured;
use crate::fragments::whitespace::obs_crlf;
use crate::fragments::whitespace::{CRLF, obs_crlf};
#[derive(Debug, PartialEq, Default)]
pub struct PartHeader<'a> {
@ -40,6 +41,36 @@ pub fn boundary<'a>(boundary: &'a [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8]
}
}
pub fn part(input: &[u8]) -> IResult<&[u8], (PartNode, Delimiter)> {
todo!();
// parse headers up to CRLF
// parse body up to boundary
// returns (PartNode + Delimiter)
}
pub fn preamble<'a>(bound: &'a [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8]> {
move |input: &[u8]| {
recognize(many0(tuple((
is_not(CRLF),
many0(pair(not(boundary(bound)), obs_crlf)),
))))(input)
}
}
pub fn multipart<'a>(bound: &'a [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], Vec<PartNode<'a>>> {
move |input: &[u8]| {
todo!();
}
// skip to boundary
// if boundary last stop
// do
// --parse part (return PartNode + Delimiter)
// while boundary not last
}
#[cfg(test)]
mod tests {
use super::*;
@ -59,4 +90,25 @@ mod tests {
Ok((&b""[..], Delimiter::Last))
);
}
#[test]
fn test_preamble() {
assert_eq!(
preamble(b"hello")(b"blip
bloup
blip
bloup--
--bim
--bim--
--hello
Field: Body
"),
Ok((
&b"\n--hello\nField: Body\n"[..],
&b"blip\nbloup\n\nblip\nbloup--\n--bim\n--bim--\n"[..],
))
);
}
}

View file

@ -13,9 +13,12 @@ use crate::fragments::encoding::encoded_word;
// Bytes CRLF
const CR: u8 = 0x0D;
const LF: u8 = 0x0A;
const CRLF: &[u8] = &[CR, LF];
pub const CRLF: &[u8] = &[CR, LF];
pub fn line(input: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
// is_not(CRLF) is a hack, it means "is not CR or LF"
// and not "is not CRLF". In other words, it continues while
// it does not encounter 0x0D or 0x0A.
pair(is_not(CRLF), obs_crlf)(input)
}