Skip to main content

mail_builder/headers/
mod.rs

1pub mod address;
2pub mod content_type;
3pub mod date;
4pub(crate) mod fold;
5pub mod message_id;
6pub mod raw;
7pub(crate) mod rfc2047;
8pub mod text;
9pub mod url;
10
11use self::{
12    address::Address, content_type::ContentType, date::Date, message_id::MessageId, raw::Raw, text::Text, url::URL,
13};
14use crate::writer::Writer;
15
16pub trait Header {
17    /// Writes the header value followed by CRLF. `column` is the number of
18    /// bytes already on the current line, normally the header name plus ": ".
19    fn write_header(&self, output: &mut impl Writer, column: usize);
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
23pub enum HeaderType<'x> {
24    Address(Address<'x>),
25    Date(Date),
26    MessageId(MessageId<'x>),
27    Raw(Raw<'x>),
28    Text(Text<'x>),
29    URL(URL<'x>),
30    ContentType(ContentType<'x>),
31}
32
33impl<'x> From<Address<'x>> for HeaderType<'x> {
34    fn from(value: Address<'x>) -> Self {
35        HeaderType::Address(value)
36    }
37}
38
39impl<'x> From<ContentType<'x>> for HeaderType<'x> {
40    fn from(value: ContentType<'x>) -> Self {
41        HeaderType::ContentType(value)
42    }
43}
44
45impl From<Date> for HeaderType<'_> {
46    fn from(value: Date) -> Self {
47        HeaderType::Date(value)
48    }
49}
50impl<'x> From<MessageId<'x>> for HeaderType<'x> {
51    fn from(value: MessageId<'x>) -> Self {
52        HeaderType::MessageId(value)
53    }
54}
55impl<'x> From<Raw<'x>> for HeaderType<'x> {
56    fn from(value: Raw<'x>) -> Self {
57        HeaderType::Raw(value)
58    }
59}
60impl<'x> From<Text<'x>> for HeaderType<'x> {
61    fn from(value: Text<'x>) -> Self {
62        HeaderType::Text(value)
63    }
64}
65
66impl<'x> From<URL<'x>> for HeaderType<'x> {
67    fn from(value: URL<'x>) -> Self {
68        HeaderType::URL(value)
69    }
70}
71
72impl Header for HeaderType<'_> {
73    fn write_header(&self, output: &mut impl Writer, column: usize) {
74        match self {
75            HeaderType::Address(value) => value.write_header(output, column),
76            HeaderType::Date(value) => value.write_header(output, column),
77            HeaderType::MessageId(value) => value.write_header(output, column),
78            HeaderType::Raw(value) => value.write_header(output, column),
79            HeaderType::Text(value) => value.write_header(output, column),
80            HeaderType::URL(value) => value.write_header(output, column),
81            HeaderType::ContentType(value) => value.write_header(output, column),
82        }
83    }
84}
85
86impl HeaderType<'_> {
87    pub fn as_content_type(&self) -> Option<&ContentType<'_>> {
88        match self {
89            HeaderType::ContentType(value) => Some(value),
90            _ => None,
91        }
92    }
93}