parse mime fields

This commit is contained in:
Quentin 2023-07-22 12:55:27 +02:00
parent d8363d70e8
commit 4baa976283
Signed by: quentin
GPG key ID: E9602264D639FF68
5 changed files with 49 additions and 19 deletions

View file

@ -1,25 +1,55 @@
use nom::{
IResult,
branch::alt,
combinator::map,
sequence::{preceded, terminated},
};
use crate::text::whitespace::obs_crlf;
use crate::text::misc_token::{Unstructured, unstructured};
use crate::rfc5322::identification::{MessageID, msg_id};
use crate::header::field_name;
use crate::mime::r#type::{NaiveType, naive_type};
use crate::mime::mechanism::{Mechanism, mechanism};
#[derive(Debug, PartialEq)]
pub enum Content<'a> {
Type(Type<'a>),
Type(NaiveType<'a>),
TransferEncoding(Mechanism<'a>),
ID(MessageId<'a>),
Description(Unstructured),
ID(MessageID<'a>),
Description(Unstructured<'a>),
}
fn field(input: &str) -> IResult<&str, Content> {
fn field(input: &[u8]) -> IResult<&[u8], Content> {
terminated(alt((
preceded(field_name(b"content-type"), map(date, Field::Date)),
preceded(field_name(b"content-type"), map(naive_type, Content::Type)),
preceded(field_name(b"content-transfer-encoding"), map(mechanism, Content::TransferEncoding)),
preceded(field_name(b"content-id"), map(msg_id, Content::ID)),
preceded(field_name(b"content-description"), map(unstructured, Content::Description)),
)), obs_crlf)(input)
}
field_name(input).map(|(rest, name)| {
(
"",
match name.to_lowercase().as_ref() {
"" => ContentType(Type(rest)),
"content-transfer-encoding" => ContentTransferEncoding(Mechanism(rest)),
"content-id" => ContentID(Identifier(rest)),
"content-description" => ContentDescription(Unstructured(rest)),
_ => Optional(name, Unstructured(rest)),
#[cfg(test)]
mod tests {
use super::*;
use crate::mime::r#type::*;
use crate::mime::charset::EmailCharset;
#[test]
fn test_content_type() {
let (rest, content) = field(b"Content-Type: text/plain; charset=UTF-8; format=flowed\r\n").unwrap();
assert_eq!(&b""[..], rest);
if let Content::Type(nt) = content {
assert_eq!(
nt.to_type(),
Type::Text(TextDesc {
charset: EmailCharset::UTF_8,
subtype: TextSubtype::Plain,
}),
);
} else {
panic!("Expected Content::Type, got {:?}", content);
}
}
)
})
}

View file

@ -1,4 +1,4 @@
pub mod charset;
pub mod mechanism;
pub mod r#type;
//pub mod field;
pub mod field;

0
src/part/field.rs Normal file
View file