parse mechanism

This commit is contained in:
Quentin 2023-07-20 17:07:18 +02:00
parent b444281729
commit 6eba9d4033
Signed by: quentin
GPG key ID: E9602264D639FF68
3 changed files with 72 additions and 52 deletions

View file

@ -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),
);
}
}

71
src/mime/mechanism.rs Normal file
View file

@ -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)),
);
}
}

View file

@ -1,2 +1,3 @@
pub mod charset;
pub mod mechanism;
//pub mod field;