use crate::prelude::*;
use std::borrow::Cow;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Clone)]
pub struct RawEntity<'a> {
pub mime_type: ContentType<'a>,
pub subtype: Cow<'a, str>,
pub description: Option<Cow<'a, str>>,
pub id: Option<(Cow<'a, str>, Cow<'a, str>)>,
pub parameters: HashMap<Cow<'a, str>, Cow<'a, str>>,
#[cfg(feature = "content-disposition")]
pub disposition: Option<Disposition<'a>>,
pub value: Cow<'a, [u8]>,
pub additional_headers: Vec<(Cow<'a, str>, Cow<'a, str>)>,
}
impl<'a> RawEntity<'a> {
pub fn parse(self) -> Result<Entity<'a>, Error> {
crate::parsing::mime::entity::entity(self)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum Entity<'a> {
Multipart {
subtype: Cow<'a, str>,
content: Vec<RawEntity<'a>>,
},
Text {
subtype: Cow<'a, str>,
value: Cow<'a, str>,
},
Unknown(Box<RawEntity<'a>>),
}
#[derive(Debug, PartialEq, Clone)]
pub enum ContentType<'a> {
Text,
Image,
Audio,
Video,
Application,
Message,
Multipart,
Unknown(Cow<'a, str>),
}
impl<'a> ContentType<'a> {
pub fn into_owned(self) -> ContentType<'static> {
match self {
ContentType::Text => ContentType::Text,
ContentType::Image => ContentType::Image,
ContentType::Audio => ContentType::Audio,
ContentType::Video => ContentType::Video,
ContentType::Application => ContentType::Application,
ContentType::Message => ContentType::Message,
ContentType::Multipart => ContentType::Multipart,
ContentType::Unknown(Cow::Owned(value)) => ContentType::Unknown(Cow::Owned(value)),
ContentType::Unknown(Cow::Borrowed(value)) => {
ContentType::Unknown(Cow::Owned(value.to_owned()))
}
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum DispositionType<'a> {
Inline,
Attachment,
Unknown(Cow<'a, str>),
}
impl<'a> DispositionType<'a> {
pub fn into_owned(self) -> DispositionType<'static> {
match self {
DispositionType::Inline => DispositionType::Inline,
DispositionType::Attachment => DispositionType::Attachment,
DispositionType::Unknown(Cow::Owned(value)) => {
DispositionType::Unknown(Cow::Owned(value))
}
DispositionType::Unknown(Cow::Borrowed(value)) => {
DispositionType::Unknown(Cow::Owned(value.to_owned()))
}
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Disposition<'a> {
pub disposition_type: DispositionType<'a>,
pub filename: Option<Cow<'a, str>>,
pub creation_date: Option<DateTime>,
pub modification_date: Option<DateTime>,
pub read_date: Option<DateTime>,
pub unstructured: HashMap<Cow<'a, str>, Cow<'a, str>>,
}
impl<'a> Disposition<'a> {
pub fn into_owned(self) -> Disposition<'static> {
Disposition {
disposition_type: self.disposition_type.into_owned(),
filename: self
.filename
.map(|filename| Cow::Owned(filename.into_owned())),
creation_date: self.creation_date,
modification_date: self.modification_date,
read_date: self.read_date,
unstructured: self
.unstructured
.into_iter()
.map(|(n, v)| (Cow::Owned(n.into_owned()), Cow::Owned(v.into_owned())))
.collect(),
}
}
}
impl<'a> ContentType<'a> {
pub fn is_composite_type(&self) -> bool {
match self {
ContentType::Message => true,
ContentType::Multipart => true,
ContentType::Text => false,
ContentType::Image => false,
ContentType::Audio => false,
ContentType::Video => false,
ContentType::Application => false,
ContentType::Unknown(_) => false,
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum ContentTransferEncoding<'a> {
SevenBit,
HeightBit,
Binary,
QuotedPrintable,
Base64,
Unknown(Cow<'a, str>),
}
impl<'a> ContentTransferEncoding<'a> {
pub fn into_owned(self) -> ContentTransferEncoding<'static> {
match self {
ContentTransferEncoding::SevenBit => ContentTransferEncoding::SevenBit,
ContentTransferEncoding::HeightBit => ContentTransferEncoding::HeightBit,
ContentTransferEncoding::Binary => ContentTransferEncoding::Binary,
ContentTransferEncoding::QuotedPrintable => ContentTransferEncoding::QuotedPrintable,
ContentTransferEncoding::Base64 => ContentTransferEncoding::Base64,
ContentTransferEncoding::Unknown(Cow::Owned(value)) => {
ContentTransferEncoding::Unknown(Cow::Owned(value))
}
ContentTransferEncoding::Unknown(Cow::Borrowed(value)) => {
ContentTransferEncoding::Unknown(Cow::Owned(value.to_owned()))
}
}
}
}