mail_headers_ng/header_components/
raw_unstructured.rs

1//! mail-internals does not ship with any predefined headers and components
2//! except `RawUnstructured`, `TransferEncoding` and `DateTime`
3
4use soft_ascii_string::SoftAsciiStr;
5
6use internals::grammar::is_vchar;
7use internals::error::{EncodingError, EncodingErrorKind};
8use internals::encoder::{EncodingWriter, EncodableInHeader};
9use ::{HeaderTryFrom, HeaderTryInto};
10use ::error::ComponentCreationError;
11use ::data::Input;
12
13
14/// A unstructured header field implementation which validates the given input
15/// but does not encode any utf8 even if it would have been necessary (it will
16/// error in that case) nor does it support breaking longer lines in multiple
17/// ones (no FWS marked for the encoder)
18#[derive(Debug, Clone, Eq, PartialEq, Hash)]
19pub struct RawUnstructured {
20    text: Input
21}
22
23impl RawUnstructured {
24    pub fn as_str(&self) -> &str {
25        self.text.as_str()
26    }
27}
28
29impl<T> From<T> for RawUnstructured
30    where Input: From<T>
31{
32    fn from(val: T) -> Self {
33        RawUnstructured { text: val.into() }
34    }
35}
36
37impl<T> HeaderTryFrom<T> for RawUnstructured
38    where T: HeaderTryInto<Input>
39{
40    fn try_from(val: T) -> Result<Self, ComponentCreationError> {
41        let input: Input = val.try_into()?;
42        Ok( input.into() )
43    }
44}
45
46impl Into<Input> for RawUnstructured {
47    fn into(self) -> Input {
48        self.text
49    }
50}
51
52impl Into<String> for RawUnstructured {
53    fn into(self) -> String {
54        self.text.into()
55    }
56}
57
58impl AsRef<str> for RawUnstructured {
59    fn as_ref(&self) -> &str {
60        self.as_str()
61    }
62}
63
64impl EncodableInHeader for RawUnstructured {
65    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
66        let mail_type = handle.mail_type();
67
68        if !self.text.chars().all(|ch| is_vchar(ch, mail_type)) {
69            return Err(
70                EncodingError::from(EncodingErrorKind::Malformed)
71                    .with_str_context(self.text.as_str())
72            );
73        }
74
75        if handle.mail_type().is_internationalized() {
76            handle.write_utf8(self.text.as_str())
77        } else {
78            handle.write_str(SoftAsciiStr::from_unchecked(self.text.as_str()))
79        }
80    }
81
82    fn boxed_clone(&self) -> Box<EncodableInHeader> {
83        Box::new(self.clone())
84    }
85}
86
87