mime section

This commit is contained in:
Quentin 2023-07-22 14:17:44 +02:00
parent 59f550b439
commit 39d6847da7
Signed by: quentin
GPG key ID: E9602264D639FF68
4 changed files with 30 additions and 26 deletions

View file

@ -8,7 +8,7 @@ use nom::{
use crate::text::whitespace::obs_crlf; use crate::text::whitespace::obs_crlf;
use crate::text::misc_token::{Unstructured, unstructured}; use crate::text::misc_token::{Unstructured, unstructured};
use crate::rfc5322::identification::{MessageID, msg_id}; use crate::rfc5322::identification::{MessageID, msg_id};
use crate::header::{field_name, header, CompFieldList}; use crate::header::{field_name};
use crate::mime::r#type::{NaiveType, naive_type}; use crate::mime::r#type::{NaiveType, naive_type};
use crate::mime::mechanism::{Mechanism, mechanism}; use crate::mime::mechanism::{Mechanism, mechanism};
//use crate::mime::mime::MIME; //use crate::mime::mime::MIME;
@ -24,7 +24,7 @@ pub enum Content<'a> {
pub fn to_mime(&self) -> MIME { self.into() } pub fn to_mime(&self) -> MIME { self.into() }
}*/ }*/
fn content(input: &[u8]) -> IResult<&[u8], Content> { pub fn content(input: &[u8]) -> IResult<&[u8], Content> {
terminated(alt(( terminated(alt((
preceded(field_name(b"content-type"), map(naive_type, Content::Type)), 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-transfer-encoding"), map(mechanism, Content::TransferEncoding)),
@ -40,6 +40,7 @@ mod tests {
use crate::mime::charset::EmailCharset; use crate::mime::charset::EmailCharset;
use crate::text::misc_token::MIMEWord; use crate::text::misc_token::MIMEWord;
use crate::text::quoted::QuotedString; use crate::text::quoted::QuotedString;
use crate::header::{header, CompFieldList};
#[test] #[test]
fn test_content_type() { fn test_content_type() {

View file

@ -1,28 +1,30 @@
use crate::mime::r#type::NaiveType;
use crate::mime::mechanism::Mechanism;
use crate::rfc5322::identification::MessageID;
use crate::text::misc_token::Unstructured;
use crate::mime::field::Content;
#[derive(Debug, PartialEq, Default)] #[derive(Debug, PartialEq, Default)]
pub struct MIMESection<'a> { pub struct MIME<'a> {
pub content_type: Option<&'a Type<'a>>, pub content_type: Option<&'a NaiveType<'a>>,
pub content_transfer_encoding: Option<&'a Mechanism<'a>>, pub content_transfer_encoding: Option<&'a Mechanism<'a>>,
pub content_id: Option<&'a MessageId<'a>>, pub content_id: Option<&'a MessageID<'a>>,
pub content_description: Option<&'a Unstructured>, pub content_description: Option<&'a Unstructured<'a>>,
pub optional: HashMap<&'a str, &'a Unstructured>,
pub unparsed: Vec<&'a str>,
} }
impl<'a> FromIterator<&'a Content<'a>> for MIME<'a> {
impl<'a> FromIterator<&'a MIMEField<'a>> for MIMESection<'a> { fn from_iter<I: IntoIterator<Item = &'a Content<'a>>>(source: I) -> Self {
fn from_iter<I: IntoIterator<Item = &'a MIMEField<'a>>>(iter: I) -> Self { source.into_iter().fold(
let mut section = MIMESection::default(); MIME::default(),
for field in iter { |mut section, field| {
match field { match field {
MIMEField::ContentType(v) => section.content_type = Some(v), Content::Type(v) => section.content_type = Some(v),
MIMEField::ContentTransferEncoding(v) => section.content_transfer_encoding = Some(v), Content::TransferEncoding(v) => section.content_transfer_encoding = Some(v),
MIMEField::ContentID(v) => section.content_id = Some(v), Content::ID(v) => section.content_id = Some(v),
MIMEField::ContentDescription(v) => section.content_description = Some(v), Content::Description(v) => section.content_description = Some(v),
MIMEField::Optional(k, v) => { section.optional.insert(k, v); },
MIMEField::Rescue(v) => section.unparsed.push(v),
}; };
}
section section
} }
)
}
} }

View file

@ -2,3 +2,4 @@ pub mod charset;
pub mod mechanism; pub mod mechanism;
pub mod r#type; pub mod r#type;
pub mod field; pub mod field;
pub mod mime;

View file

@ -3,11 +3,10 @@ use nom::{
IResult, IResult,
branch::alt, branch::alt,
combinator::map, combinator::map,
multi::many0,
sequence::{preceded, terminated}, sequence::{preceded, terminated},
}; };
use crate::text::whitespace::{obs_crlf, foldable_line}; use crate::text::whitespace::{obs_crlf};
use crate::rfc5322::address::{AddressList, address_list, nullable_address_list, mailbox_list}; use crate::rfc5322::address::{AddressList, address_list, nullable_address_list, mailbox_list};
use crate::rfc5322::datetime::section as date; use crate::rfc5322::datetime::section as date;
use crate::rfc5322::mailbox::{MailboxRef, MailboxList, AddrSpec, mailbox}; use crate::rfc5322::mailbox::{MailboxRef, MailboxList, AddrSpec, mailbox};
@ -15,7 +14,7 @@ use crate::rfc5322::identification::{MessageID, MessageIDList, msg_id, msg_list}
use crate::rfc5322::trace::{ReceivedLog, return_path, received_log}; use crate::rfc5322::trace::{ReceivedLog, return_path, received_log};
use crate::rfc5322::mime::{Version, version}; use crate::rfc5322::mime::{Version, version};
use crate::rfc5322::message::Message; use crate::rfc5322::message::Message;
use crate::header::{header, field_name, CompFieldList}; use crate::header::{field_name};
use crate::text::misc_token::{Unstructured, PhraseList, unstructured, phrase_list}; use crate::text::misc_token::{Unstructured, PhraseList, unstructured, phrase_list};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -94,6 +93,7 @@ mod tests {
use crate::rfc5322::mailbox::*; use crate::rfc5322::mailbox::*;
use crate::rfc5322::address::*; use crate::rfc5322::address::*;
use crate::text::misc_token::*; use crate::text::misc_token::*;
use crate::header::header;
#[test] #[test]
fn test_header() { fn test_header() {