summaryrefslogtreecommitdiffstats
path: root/headers/src/header_components/transfer_encoding.rs
blob: 4e5c4f12b6739913f0b55d152408f95603d87a7f (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use soft_ascii_string::SoftAsciiStr;

use internals::error::EncodingError;
use internals::encoder::{EncodingWriter, EncodableInHeader};

#[cfg(feature="serde")]
use serde::{Serialize, Deserialize};


/// The TransferEnecoding header component mainly used by the ContentTransferEncodingHeader.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub enum TransferEncoding {
    #[cfg_attr(feature="serde", serde(rename="7bit"))]
    _7Bit,
    #[cfg_attr(feature="serde", serde(rename="8bit"))]
    _8Bit,
    #[cfg_attr(feature="serde", serde(rename="binary"))]
    Binary,
    #[cfg_attr(feature="serde", serde(rename="quoted-printable"))]
    QuotedPrintable,
    #[cfg_attr(feature="serde", serde(rename="base64"))]
    Base64
}

impl TransferEncoding {
    pub fn repr(&self ) -> &SoftAsciiStr {
        use self::TransferEncoding::*;
        match *self {
            _7Bit => SoftAsciiStr::from_unchecked("7bit"),
            _8Bit => SoftAsciiStr::from_unchecked("8bit"),
            Binary =>  SoftAsciiStr::from_unchecked("binary"),
            QuotedPrintable => SoftAsciiStr::from_unchecked("quoted-printable"),
            Base64 =>  SoftAsciiStr::from_unchecked("base64"),
        }
    }
}


impl EncodableInHeader for  TransferEncoding {

    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
        handle.write_str( self.repr() )?;
        Ok( () )
    }

    fn boxed_clone(&self) -> Box<EncodableInHeader> {
        Box::new(self.clone())
    }
}


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

    ec_test! {_7bit, {
        TransferEncoding::_7Bit
    } => ascii => [
        Text "7bit"
    ]}

    ec_test! {_8bit, {
        TransferEncoding::_8Bit
    } => ascii => [
        Text "8bit"
    ]}

    ec_test!{binary, {
        TransferEncoding::Binary
    } => ascii => [
        Text "binary"
    ]}

    ec_test!{base64, {
        TransferEncoding::Base64
    } => ascii => [
        Text "base64"
    ]}

    ec_test!{quoted_printable, {
        TransferEncoding::QuotedPrintable
    } => ascii => [
        Text "quoted-printable"
    ]}

    #[cfg(feature="serde")]
    mod serde {
        use serde_test::{Token, assert_tokens};
        use super::TransferEncoding;

        macro_rules! serde_token_tests {
            ($([$lname:ident, $hname:ident, $s:tt]),*) => ($(
                #[test]
                fn $lname() {
                    assert_tokens(&TransferEncoding::$hname, &[
                        Token::UnitVariant {
                            name: "TransferEncoding",
                            variant: $s
                        }
                    ])
                }
            )*);
        }

        serde_token_tests! {
            [_7bit, _7Bit, "7bit"],
            [_8bit, _8Bit, "8bit"],
            [binary, Binary, "binary"],
            [quoted_printable, QuotedPrintable, "quoted-printable"],
            [base64, Base64, "base64"]
        }

    }
}