Skip to main content

mail_builder/headers/
url.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
12use std::borrow::Cow;
13
14use super::Header;
15
16/// URL header, used mostly on List-* headers
17#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
18pub struct URL<'x> {
19    pub url: Vec<Cow<'x, str>>,
20}
21
22impl<'x> URL<'x> {
23    /// Create a new URL header
24    pub fn new(url: impl Into<Cow<'x, str>>) -> Self {
25        Self {
26            url: vec![url.into()],
27        }
28    }
29
30    /// Create a new multi-value URL header
31    pub fn new_list<T, U>(urls: T) -> Self
32    where
33        T: Iterator<Item = U>,
34        U: Into<Cow<'x, str>>,
35    {
36        Self {
37            url: urls.map(|s| s.into()).collect(),
38        }
39    }
40}
41
42impl<'x> From<&'x str> for URL<'x> {
43    fn from(value: &'x str) -> Self {
44        Self::new(value)
45    }
46}
47
48impl<'x> From<String> for URL<'x> {
49    fn from(value: String) -> Self {
50        Self::new(value)
51    }
52}
53
54impl<'x> From<&[&'x str]> for URL<'x> {
55    fn from(value: &[&'x str]) -> Self {
56        URL {
57            url: value.iter().map(|&s| s.into()).collect(),
58        }
59    }
60}
61
62impl<'x> From<&'x [String]> for URL<'x> {
63    fn from(value: &'x [String]) -> Self {
64        URL {
65            url: value.iter().map(|s| s.into()).collect(),
66        }
67    }
68}
69
70impl<'x, T> From<Vec<T>> for URL<'x>
71where
72    T: Into<Cow<'x, str>>,
73{
74    fn from(value: Vec<T>) -> Self {
75        URL {
76            url: value.into_iter().map(|s| s.into()).collect(),
77        }
78    }
79}
80
81impl<'x> Header for URL<'x> {
82    fn write_header(&self, mut output: impl std::io::Write, mut bytes_written: usize) -> std::io::Result<usize> {
83        for (pos, url) in self.url.iter().enumerate() {
84            if pos > 0 {
85                if bytes_written + url.len() + 2 >= 76 {
86                    output.write_all(b"\r\n\t")?;
87                    bytes_written = 1;
88                } else {
89                    output.write_all(b" ")?;
90                    bytes_written += 1;
91                }
92            }
93            output.write_all(b"<")?;
94            output.write_all(url.as_bytes())?;
95            if pos < self.url.len() - 1 {
96                output.write_all(b">,")?;
97                bytes_written += url.len() + 3;
98            } else {
99                output.write_all(b">")?;
100                bytes_written += url.len() + 2;
101            }
102        }
103
104        if bytes_written > 0 {
105            output.write_all(b"\r\n")?;
106        }
107
108        Ok(0)
109    }
110}