diff --git a/src/mime/content_fields.rs b/src/mime/content_fields.rs index b499e19..24c3bc0 100644 --- a/src/mime/content_fields.rs +++ b/src/mime/content_fields.rs @@ -84,16 +84,6 @@ pub enum Parameter<'a> { Other(&'a str, String), } -#[derive(Debug, PartialEq)] -pub enum Mechanism<'a> { - _7Bit, - _8Bit, - Binary, - QuotedPrintable, - Base64, - Other(&'a str), -} - @@ -217,22 +207,6 @@ pub fn content_type(input: &str) -> IResult<&str, Type> { Ok((rest, parsed)) } -pub fn mechanism(input: &str) -> IResult<&str, Mechanism> { - use Mechanism::*; - - let (input, mecha) = token(input)?; - let parsed = match mecha.to_lowercase().as_ref() { - "7bit" => _7Bit, - "8bit" => _8Bit, - "binary" => Binary, - "quoted-printable" => QuotedPrintable, - "base64" => Base64, - _ => Other(mecha), - }; - - Ok((input, parsed)) -} - #[cfg(test)] mod tests { use super::*; @@ -313,31 +287,5 @@ mod tests { ); } - #[test] - fn test_mechanism() { - assert_eq!( - Mechanism::try_from(&lazy::Mechanism("7bit")), - Ok(Mechanism::_7Bit), - ); - assert_eq!( - Mechanism::try_from(&lazy::Mechanism("(youhou) 8bit")), - Ok(Mechanism::_8Bit), - ); - - assert_eq!( - Mechanism::try_from(&lazy::Mechanism("(blip) bInArY (blip blip)")), - Ok(Mechanism::Binary), - ); - - assert_eq!( - Mechanism::try_from(&lazy::Mechanism(" base64 ")), - Ok(Mechanism::Base64), - ); - - assert_eq!( - Mechanism::try_from(&lazy::Mechanism(" Quoted-Printable ")), - Ok(Mechanism::QuotedPrintable), - ); - } } diff --git a/src/mime/mechanism.rs b/src/mime/mechanism.rs new file mode 100644 index 0000000..acb29cf --- /dev/null +++ b/src/mime/mechanism.rs @@ -0,0 +1,71 @@ +use nom::{ + IResult, + branch::alt, + bytes::complete::tag_no_case, + combinator::{map, opt, value}, + sequence::delimited, +}; +use crate::text::whitespace::cfws; +use crate::text::words::mime_token as token; + +#[derive(Debug, Clone, PartialEq)] +pub enum Mechanism<'a> { + _7Bit, + _8Bit, + Binary, + QuotedPrintable, + Base64, + Other(&'a [u8]), +} + +pub fn mechanism(input: &[u8]) -> IResult<&[u8], Mechanism> { + use Mechanism::*; + + alt(( + delimited( + opt(cfws), + alt(( + value(_7Bit, tag_no_case("7bit")), + value(_8Bit, tag_no_case("8bit")), + value(Binary, tag_no_case("binary")), + value(QuotedPrintable, tag_no_case("quoted-printable")), + value(Base64, tag_no_case("base64")), + )), + opt(cfws), + ), + map(token, Other), + ))(input) +} + + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_mechanism() { + assert_eq!( + mechanism(b"7bit"), + Ok((&b""[..], Mechanism::_7Bit)), + ); + + assert_eq!( + mechanism(b"(youhou) 8bit"), + Ok((&b""[..], Mechanism::_8Bit)), + ); + + assert_eq!( + mechanism(b"(blip) bInArY (blip blip)"), + Ok((&b""[..], Mechanism::Binary)), + ); + + assert_eq!( + mechanism(b" base64 "), + Ok((&b""[..], Mechanism::Base64)), + ); + + assert_eq!( + mechanism(b" Quoted-Printable "), + Ok((&b""[..], Mechanism::QuotedPrintable)), + ); + } +} diff --git a/src/mime/mod.rs b/src/mime/mod.rs index cd4d1f3..fb8d367 100644 --- a/src/mime/mod.rs +++ b/src/mime/mod.rs @@ -1,2 +1,3 @@ pub mod charset; +pub mod mechanism; //pub mod field;