1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use soft_ascii_string::SoftAsciiChar;

use internals::error::EncodingError;
use internals::encoder::{EncodingWriter, EncodableInHeader};
use ::{HeaderTryFrom, HeaderTryInto};
use ::error::ComponentCreationError;
use super::Email;


#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Path(pub Option<Email>);

impl HeaderTryFrom<Option<Email>> for Path {
    fn try_from(opt_mail: Option<Email>) -> Result<Self, ComponentCreationError> {
        Ok( Path( opt_mail ) )
    }
}

impl<T> HeaderTryFrom<T> for Path
    where T: HeaderTryInto<Email>
{
    fn try_from(opt_mail: T) -> Result<Self, ComponentCreationError> {
        Ok( Path( Some( opt_mail.try_into()? ) ) )
    }
}

impl EncodableInHeader for  Path {

    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
        handle.mark_fws_pos();
        handle.write_char(SoftAsciiChar::from_unchecked('<'))?;
        if let Some( mail ) = self.0.as_ref() {
            mail.encode( handle )?;
        }
        handle.write_char(SoftAsciiChar::from_unchecked('>'))?;
        handle.mark_fws_pos();
        Ok( () )
    }

    fn boxed_clone(&self) -> Box<EncodableInHeader> {
        Box::new(self.clone())
    }
}
//NOTE for parsing we have to make sure to _require_ '<>' around the email

#[cfg(test)]
mod test {
    use super::*;

    ec_test!{empty_path, {
        Path( None )
    } => ascii => [
        MarkFWS,
        Text "<>",
        MarkFWS
    ]}

    ec_test!{simple_path, {
        Path( Some( Email::try_from( "abc@de.fg" )? ) )
    } => ascii => [
        MarkFWS,
        Text "<",
        MarkFWS,
        Text "abc",
        MarkFWS,
        Text "@",
        MarkFWS,
        Text "de.fg",
        MarkFWS,
        Text ">",
        MarkFWS
    ]}
}