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;
#[derive(Debug, Clone, PartialEq)]
pub enum Mechanism<'a> {
pub enum DecodedMechanism<'a> {
_7Bit,
_8Bit,
Binary,
@ -18,23 +18,23 @@ pub enum Mechanism<'a> {
Other(&'a [u8]),
}
pub fn mechanism(input: &[u8]) -> IResult<&[u8], Mechanism> {
use Mechanism::*;
pub struct Mechanism<'a>(pub &'a [u8]);
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((
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)
pub fn mechanism(input: &[u8]) -> IResult<&[u8], Mechanism> {
map(token, Mechanism)(input)
}
@ -44,28 +44,28 @@ mod tests {
#[test]
fn test_mechanism() {
assert_eq!(
mechanism(b"7bit"),
Ok((&b""[..], Mechanism::_7Bit)),
mechanism(b"7bit").unwrap().1.decode(),
DecodedMechanism::_7Bit,
);
assert_eq!(
mechanism(b"(youhou) 8bit"),
Ok((&b""[..], Mechanism::_8Bit)),
mechanism(b"(youhou) 8bit").unwrap().1.decode(),
DecodedMechanism::_8Bit,
);
assert_eq!(
mechanism(b"(blip) bInArY (blip blip)"),
Ok((&b""[..], Mechanism::Binary)),
mechanism(b"(blip) bInArY (blip blip)").unwrap().1.decode(),
DecodedMechanism::Binary,
);
assert_eq!(
mechanism(b" base64 "),
Ok((&b""[..], Mechanism::Base64)),
mechanism(b" base64 ").unwrap().1.decode(),
DecodedMechanism::Base64,
);
assert_eq!(
mechanism(b" Quoted-Printable "),
Ok((&b""[..], Mechanism::QuotedPrintable)),
mechanism(b" Quoted-Printable ").unwrap().1.decode(),
DecodedMechanism::QuotedPrintable,
);
}
}