mail_builder/headers/
raw.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use std::borrow::Cow;
8
9use super::Header;
10
11/// Raw e-mail header.
12/// Raw headers are not encoded, only line-wrapped.
13#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
14pub struct Raw<'x> {
15    pub raw: Cow<'x, str>,
16}
17
18impl<'x> Raw<'x> {
19    /// Create a new raw header
20    pub fn new(raw: impl Into<Cow<'x, str>>) -> Self {
21        Self { raw: raw.into() }
22    }
23}
24
25impl<'x, T> From<T> for Raw<'x>
26where
27    T: Into<Cow<'x, str>>,
28{
29    fn from(value: T) -> Self {
30        Self::new(value)
31    }
32}
33
34impl Header for Raw<'_> {
35    fn write_header(
36        &self,
37        mut output: impl std::io::Write,
38        mut bytes_written: usize,
39    ) -> std::io::Result<usize> {
40        for (pos, &ch) in self.raw.as_bytes().iter().enumerate() {
41            if bytes_written >= 76 && ch.is_ascii_whitespace() && pos < self.raw.len() - 1 {
42                output.write_all(b"\r\n\t")?;
43                bytes_written = 1;
44            }
45            output.write_all(&[ch])?;
46            bytes_written += 1;
47        }
48        output.write_all(b"\r\n")?;
49        Ok(0)
50    }
51}