Skip to main content

mail_builder/headers/
mod.rs

1/*
2 * Copyright Stalwart Labs Ltd. See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12pub mod address;
13pub mod content_type;
14pub mod date;
15pub mod message_id;
16pub mod raw;
17pub mod text;
18pub mod url;
19
20use std::io::{self, Write};
21
22use self::{
23    address::Address, content_type::ContentType, date::Date, message_id::MessageId, raw::Raw, text::Text, url::URL,
24};
25
26pub trait Header {
27    fn write_header(&self, output: impl Write, bytes_written: usize) -> io::Result<usize>;
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
31pub enum HeaderType<'x> {
32    Address(Address<'x>),
33    Date(Date),
34    MessageId(MessageId<'x>),
35    Raw(Raw<'x>),
36    Text(Text<'x>),
37    URL(URL<'x>),
38    ContentType(ContentType<'x>),
39}
40
41impl<'x> From<Address<'x>> for HeaderType<'x> {
42    fn from(value: Address<'x>) -> Self {
43        HeaderType::Address(value)
44    }
45}
46
47impl<'x> From<ContentType<'x>> for HeaderType<'x> {
48    fn from(value: ContentType<'x>) -> Self {
49        HeaderType::ContentType(value)
50    }
51}
52
53impl<'x> From<Date> for HeaderType<'x> {
54    fn from(value: Date) -> Self {
55        HeaderType::Date(value)
56    }
57}
58impl<'x> From<MessageId<'x>> for HeaderType<'x> {
59    fn from(value: MessageId<'x>) -> Self {
60        HeaderType::MessageId(value)
61    }
62}
63impl<'x> From<Raw<'x>> for HeaderType<'x> {
64    fn from(value: Raw<'x>) -> Self {
65        HeaderType::Raw(value)
66    }
67}
68impl<'x> From<Text<'x>> for HeaderType<'x> {
69    fn from(value: Text<'x>) -> Self {
70        HeaderType::Text(value)
71    }
72}
73
74impl<'x> From<URL<'x>> for HeaderType<'x> {
75    fn from(value: URL<'x>) -> Self {
76        HeaderType::URL(value)
77    }
78}
79
80impl<'x> Header for HeaderType<'x> {
81    fn write_header(&self, output: impl Write, bytes_written: usize) -> io::Result<usize> {
82        match self {
83            HeaderType::Address(value) => value.write_header(output, bytes_written),
84            HeaderType::Date(value) => value.write_header(output, bytes_written),
85            HeaderType::MessageId(value) => value.write_header(output, bytes_written),
86            HeaderType::Raw(value) => value.write_header(output, bytes_written),
87            HeaderType::Text(value) => value.write_header(output, bytes_written),
88            HeaderType::URL(value) => value.write_header(output, bytes_written),
89            HeaderType::ContentType(value) => value.write_header(output, bytes_written),
90        }
91    }
92}
93
94impl<'x> HeaderType<'x> {
95    pub fn as_content_type(&self) -> Option<&ContentType> {
96        match self {
97            HeaderType::ContentType(value) => Some(value),
98            _ => None,
99        }
100    }
101}