Skip to main content

mail_builder/headers/
raw.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/// Raw e-mail header.
17/// Raw headers are not encoded, only line-wrapped.
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
19pub struct Raw<'x> {
20    pub raw: Cow<'x, str>,
21}
22
23impl<'x> Raw<'x> {
24    /// Create a new raw header
25    pub fn new(raw: impl Into<Cow<'x, str>>) -> Self {
26        Self {
27            raw: raw.into(),
28        }
29    }
30}
31
32impl<'x, T> From<T> for Raw<'x>
33where
34    T: Into<Cow<'x, str>>,
35{
36    fn from(value: T) -> Self {
37        Self::new(value)
38    }
39}
40
41impl<'x> Header for Raw<'x> {
42    fn write_header(&self, mut output: impl std::io::Write, mut bytes_written: usize) -> std::io::Result<usize> {
43        for (pos, &ch) in self.raw.as_bytes().iter().enumerate() {
44            if bytes_written >= 76 && ch.is_ascii_whitespace() && pos < self.raw.len() - 1 {
45                output.write_all(b"\r\n\t")?;
46                bytes_written = 1;
47            }
48            output.write_all(&[ch])?;
49            bytes_written += 1;
50        }
51        output.write_all(b"\r\n")?;
52        Ok(0)
53    }
54}