change how mechanism work

This commit is contained in:
Quentin 2023-07-20 17:21:35 +02:00
parent 6eba9d4033
commit 42a5f928f6
Signed by: quentin
GPG key ID: E9602264D639FF68

View file

@ -9,7 +9,7 @@ use crate::text::whitespace::cfws;
use crate::text::words::mime_token as token; use crate::text::words::mime_token as token;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Mechanism<'a> { pub enum DecodedMechanism<'a> {
_7Bit, _7Bit,
_8Bit, _8Bit,
Binary, Binary,
@ -18,23 +18,23 @@ pub enum Mechanism<'a> {
Other(&'a [u8]), Other(&'a [u8]),
} }
pub fn mechanism(input: &[u8]) -> IResult<&[u8], Mechanism> { pub struct Mechanism<'a>(pub &'a [u8]);
use Mechanism::*; impl<'a> Mechanism<'a> {
pub fn decode(&self) -> DecodedMechanism {
use DecodedMechanism::*;
match self.0.to_ascii_lowercase().as_slice() {
b"7bit" => _7Bit,
b"8bit" => _8Bit,
b"binary" => Binary,
b"quoted-printable" => QuotedPrintable,
b"base64" => Base64,
_ => Other(self.0),
}
}
}
alt(( pub fn mechanism(input: &[u8]) -> IResult<&[u8], Mechanism> {
delimited( map(token, Mechanism)(input)
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)
} }
@ -44,28 +44,28 @@ mod tests {
#[test] #[test]
fn test_mechanism() { fn test_mechanism() {
assert_eq!( assert_eq!(
mechanism(b"7bit"), mechanism(b"7bit").unwrap().1.decode(),
Ok((&b""[..], Mechanism::_7Bit)), DecodedMechanism::_7Bit,
); );
assert_eq!( assert_eq!(
mechanism(b"(youhou) 8bit"), mechanism(b"(youhou) 8bit").unwrap().1.decode(),
Ok((&b""[..], Mechanism::_8Bit)), DecodedMechanism::_8Bit,
); );
assert_eq!( assert_eq!(
mechanism(b"(blip) bInArY (blip blip)"), mechanism(b"(blip) bInArY (blip blip)").unwrap().1.decode(),
Ok((&b""[..], Mechanism::Binary)), DecodedMechanism::Binary,
); );
assert_eq!( assert_eq!(
mechanism(b" base64 "), mechanism(b" base64 ").unwrap().1.decode(),
Ok((&b""[..], Mechanism::Base64)), DecodedMechanism::Base64,
); );
assert_eq!( assert_eq!(
mechanism(b" Quoted-Printable "), mechanism(b" Quoted-Printable ").unwrap().1.decode(),
Ok((&b""[..], Mechanism::QuotedPrintable)), DecodedMechanism::QuotedPrintable,
); );
} }
} }