mail_headers_ng/data/
simple_item.rs

1use std::ops::Deref;
2
3use soft_ascii_string::{ SoftAsciiStr, SoftAsciiString};
4
5use super::input::Input;
6use super::inner_item::{ InnerAscii, InnerUtf8 };
7
8#[cfg(feature="serde")]
9use serde::{Serialize, Deserialize};
10
11#[derive(Debug, Clone, Hash, Eq, PartialEq)]
12#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
13pub enum SimpleItem {
14    /// specifies that the Item is valid Ascii, nothing more
15    Ascii( InnerAscii ),
16    /// specifies that the Item is valid Utf8, nothing more
17    Utf8( InnerUtf8 )
18}
19
20impl SimpleItem {
21
22    pub fn as_str( &self ) -> &str {
23        use self::SimpleItem::*;
24        match *self {
25            Ascii( ref value ) => value.as_str(),
26            Utf8( ref value ) => value.as_str()
27        }
28    }
29
30    pub fn is_ascii( &self ) -> bool {
31        use self::SimpleItem::*;
32        match *self {
33            Ascii( .. ) => true,
34            Utf8( .. ) => false
35        }
36    }
37
38    pub fn from_utf8_input( s: Input ) -> Self {
39        SimpleItem::Utf8( s.0 )
40    }
41
42    pub fn from_utf8( s: String ) -> Self {
43        SimpleItem::Utf8( InnerUtf8::Owned( s ) )
44    }
45
46
47}
48
49impl Deref for SimpleItem {
50    type Target = str;
51
52    fn deref( &self ) -> &str {
53        use self::SimpleItem::*;
54        match *self {
55            Ascii( ref astr ) => astr.as_str(),
56            Utf8( ref utf8 ) => &**utf8
57        }
58    }
59}
60
61
62impl Into<String> for SimpleItem {
63    fn into(self) -> String {
64        use self::SimpleItem::*;
65        match self {
66            Ascii( aitem ) => {
67                let astring: SoftAsciiString = aitem.into();
68                astring.into()
69            },
70            Utf8( string ) => string.into()
71        }
72    }
73}
74
75impl<'a> From<&'a str> for SimpleItem {
76    fn from( string: &'a str ) -> Self {
77        Self::from( String::from( string ) )
78    }
79}
80
81impl From<String> for SimpleItem {
82    fn from( string: String ) -> Self {
83        match SoftAsciiString::from_string( string ) {
84            Ok( astring ) => SimpleItem::Ascii( InnerAscii::Owned( astring ) ),
85            Err( err ) => SimpleItem::Utf8( InnerUtf8::Owned( err.into_source() ) )
86        }
87    }
88}
89
90impl From<SoftAsciiString> for SimpleItem {
91    fn from( astring: SoftAsciiString ) -> Self {
92        SimpleItem::Ascii( InnerAscii::Owned( astring ) )
93    }
94}
95
96impl From<Input> for SimpleItem {
97    fn from(input: Input) -> Self {
98        match input {
99            Input( InnerUtf8::Owned( string ) ) => match SoftAsciiString::from_string( string ) {
100                Ok( ascii ) => SimpleItem::Ascii( InnerAscii::Owned( ascii ) ),
101                Err( err ) => SimpleItem::Utf8( InnerUtf8::Owned( err.into_source() ) )
102            },
103            Input( InnerUtf8::Shared( shared ) ) => {
104                if shared.is_ascii() {
105                    let a_shared = shared.map(|s| SoftAsciiStr::from_unchecked(s));
106                    SimpleItem::Ascii(InnerAscii::Shared(a_shared))
107                } else {
108                    SimpleItem::Utf8(InnerUtf8::Shared(shared))
109                }
110            }
111        }
112    }
113}