Skip to main content

mail_builder/headers/
mod.rs

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