parse mime fields
This commit is contained in:
parent
d8363d70e8
commit
4baa976283
5 changed files with 49 additions and 19 deletions
|
@ -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)),
|
||||
|
||||
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)),
|
||||
}
|
||||
)
|
||||
})
|
||||
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)
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
0
src/part/field.rs
Normal file
Loading…
Reference in a new issue