mail_headers/header_components/
transfer_encoding.rs

1use soft_ascii_string::SoftAsciiStr;
2
3use internals::error::EncodingError;
4use internals::encoder::{EncodingWriter, EncodableInHeader};
5
6#[cfg(feature="serde")]
7use serde::{Serialize, Deserialize};
8
9
10/// The TransferEnecoding header component mainly used by the ContentTransferEncodingHeader.
11#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
12#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
13pub enum TransferEncoding {
14    #[cfg_attr(feature="serde", serde(rename="7bit"))]
15    _7Bit,
16    #[cfg_attr(feature="serde", serde(rename="8bit"))]
17    _8Bit,
18    #[cfg_attr(feature="serde", serde(rename="binary"))]
19    Binary,
20    #[cfg_attr(feature="serde", serde(rename="quoted-printable"))]
21    QuotedPrintable,
22    #[cfg_attr(feature="serde", serde(rename="base64"))]
23    Base64
24}
25
26impl TransferEncoding {
27    pub fn repr(&self ) -> &SoftAsciiStr {
28        use self::TransferEncoding::*;
29        match *self {
30            _7Bit => SoftAsciiStr::from_unchecked("7bit"),
31            _8Bit => SoftAsciiStr::from_unchecked("8bit"),
32            Binary =>  SoftAsciiStr::from_unchecked("binary"),
33            QuotedPrintable => SoftAsciiStr::from_unchecked("quoted-printable"),
34            Base64 =>  SoftAsciiStr::from_unchecked("base64"),
35        }
36    }
37}
38
39
40impl EncodableInHeader for  TransferEncoding {
41
42    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
43        handle.write_str( self.repr() )?;
44        Ok( () )
45    }
46
47    fn boxed_clone(&self) -> Box<EncodableInHeader> {
48        Box::new(self.clone())
49    }
50}
51
52
53#[cfg(test)]
54mod test {
55    use super::TransferEncoding;
56
57    ec_test! {_7bit, {
58        TransferEncoding::_7Bit
59    } => ascii => [
60        Text "7bit"
61    ]}
62
63    ec_test! {_8bit, {
64        TransferEncoding::_8Bit
65    } => ascii => [
66        Text "8bit"
67    ]}
68
69    ec_test!{binary, {
70        TransferEncoding::Binary
71    } => ascii => [
72        Text "binary"
73    ]}
74
75    ec_test!{base64, {
76        TransferEncoding::Base64
77    } => ascii => [
78        Text "base64"
79    ]}
80
81    ec_test!{quoted_printable, {
82        TransferEncoding::QuotedPrintable
83    } => ascii => [
84        Text "quoted-printable"
85    ]}
86
87    #[cfg(feature="serde")]
88    mod serde {
89        use serde_test::{Token, assert_tokens};
90        use super::TransferEncoding;
91
92        macro_rules! serde_token_tests {
93            ($([$lname:ident, $hname:ident, $s:tt]),*) => ($(
94                #[test]
95                fn $lname() {
96                    assert_tokens(&TransferEncoding::$hname, &[
97                        Token::UnitVariant {
98                            name: "TransferEncoding",
99                            variant: $s
100                        }
101                    ])
102                }
103            )*);
104        }
105
106        serde_token_tests! {
107            [_7bit, _7Bit, "7bit"],
108            [_8bit, _8Bit, "8bit"],
109            [binary, Binary, "binary"],
110            [quoted_printable, QuotedPrintable, "quoted-printable"],
111            [base64, Base64, "base64"]
112        }
113
114    }
115}
116
117