mail_headers_ng/header_components/
path.rs

1use soft_ascii_string::SoftAsciiChar;
2
3use internals::error::EncodingError;
4use internals::encoder::{EncodingWriter, EncodableInHeader};
5use ::{HeaderTryFrom, HeaderTryInto};
6use ::error::ComponentCreationError;
7use super::Email;
8
9
10#[derive(Debug, Clone, Hash, Eq, PartialEq)]
11pub struct Path(pub Option<Email>);
12
13impl HeaderTryFrom<Option<Email>> for Path {
14    fn try_from(opt_mail: Option<Email>) -> Result<Self, ComponentCreationError> {
15        Ok( Path( opt_mail ) )
16    }
17}
18
19impl<T> HeaderTryFrom<T> for Path
20    where T: HeaderTryInto<Email>
21{
22    fn try_from(opt_mail: T) -> Result<Self, ComponentCreationError> {
23        Ok( Path( Some( opt_mail.try_into()? ) ) )
24    }
25}
26
27impl EncodableInHeader for  Path {
28
29    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
30        handle.mark_fws_pos();
31        handle.write_char(SoftAsciiChar::from_unchecked('<'))?;
32        if let Some( mail ) = self.0.as_ref() {
33            mail.encode( handle )?;
34        }
35        handle.write_char(SoftAsciiChar::from_unchecked('>'))?;
36        handle.mark_fws_pos();
37        Ok( () )
38    }
39
40    fn boxed_clone(&self) -> Box<EncodableInHeader> {
41        Box::new(self.clone())
42    }
43}
44//NOTE for parsing we have to make sure to _require_ '<>' around the email
45
46#[cfg(test)]
47mod test {
48    use super::*;
49
50    ec_test!{empty_path, {
51        Path( None )
52    } => ascii => [
53        MarkFWS,
54        Text "<>",
55        MarkFWS
56    ]}
57
58    ec_test!{simple_path, {
59        Path( Some( Email::try_from( "abc@de.fg" )? ) )
60    } => ascii => [
61        MarkFWS,
62        Text "<",
63        MarkFWS,
64        Text "abc",
65        MarkFWS,
66        Text "@",
67        MarkFWS,
68        Text "de.fg",
69        MarkFWS,
70        Text ">",
71        MarkFWS
72    ]}
73}