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 mod message_id;
11pub mod raw;
12pub mod text;
13pub mod url;
14
15use std::io::{self, Write};
16
17use self::{
18    address::Address, content_type::ContentType, date::Date, message_id::MessageId, raw::Raw,
19    text::Text, url::URL,
20};
21
22pub trait Header {
23    fn write_header(&self, output: impl Write, bytes_written: usize) -> io::Result<usize>;
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
27pub enum HeaderType<'x> {
28    Address(Address<'x>),
29    Date(Date),
30    MessageId(MessageId<'x>),
31    Raw(Raw<'x>),
32    Text(Text<'x>),
33    URL(URL<'x>),
34    ContentType(ContentType<'x>),
35}
36
37impl<'x> From<Address<'x>> for HeaderType<'x> {
38    fn from(value: Address<'x>) -> Self {
39        HeaderType::Address(value)
40    }
41}
42
43impl<'x> From<ContentType<'x>> for HeaderType<'x> {
44    fn from(value: ContentType<'x>) -> Self {
45        HeaderType::ContentType(value)
46    }
47}
48
49impl From<Date> for HeaderType<'_> {
50    fn from(value: Date) -> Self {
51        HeaderType::Date(value)
52    }
53}
54impl<'x> From<MessageId<'x>> for HeaderType<'x> {
55    fn from(value: MessageId<'x>) -> Self {
56        HeaderType::MessageId(value)
57    }
58}
59impl<'x> From<Raw<'x>> for HeaderType<'x> {
60    fn from(value: Raw<'x>) -> Self {
61        HeaderType::Raw(value)
62    }
63}
64impl<'x> From<Text<'x>> for HeaderType<'x> {
65    fn from(value: Text<'x>) -> Self {
66        HeaderType::Text(value)
67    }
68}
69
70impl<'x> From<URL<'x>> for HeaderType<'x> {
71    fn from(value: URL<'x>) -> Self {
72        HeaderType::URL(value)
73    }
74}
75
76impl Header for HeaderType<'_> {
77    fn write_header(&self, output: impl Write, bytes_written: usize) -> io::Result<usize> {
78        match self {
79            HeaderType::Address(value) => value.write_header(output, bytes_written),
80            HeaderType::Date(value) => value.write_header(output, bytes_written),
81            HeaderType::MessageId(value) => value.write_header(output, bytes_written),
82            HeaderType::Raw(value) => value.write_header(output, bytes_written),
83            HeaderType::Text(value) => value.write_header(output, bytes_written),
84            HeaderType::URL(value) => value.write_header(output, bytes_written),
85            HeaderType::ContentType(value) => value.write_header(output, bytes_written),
86        }
87    }
88}
89
90impl HeaderType<'_> {
91    pub fn as_content_type(&self) -> Option<&ContentType<'_>> {
92        match self {
93            HeaderType::ContentType(value) => Some(value),
94            _ => None,
95        }
96    }
97}