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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Typed binary arrays of specific sizes.

macro_rules! B {
    ($name:ident, $size:expr) => {
        #[derive(Copy)]
        pub struct $name(pub [u8;$size]);

        impl $name {

            fn to_base64(&self) -> String {
                ::data_encoding::base64::encode(&self.0)
            }

            fn from_base64(s:&str) -> ::std::result::Result<$name, String> {
                let mut data = [0; $size];
                let decode = &s.as_bytes()[0..::data_encoding::base64::encode_nopad_len($size)];
                match ::data_encoding::base64::decode_nopad_mut(decode, &mut data) {
                    Ok(_) => Ok($name(data)),
                    Err(e) => Err(format!("{:?}", e)),
                }
            }

        }

        impl ::serde::Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where S: ::serde::Serializer
            {
                serializer.serialize_str(&*self.to_base64())
            }
        }

        impl ::serde::Deserialize for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                where D: ::serde::Deserializer
            {
                struct Visitor;
                impl ::serde::de::Visitor for Visitor {
                    type Value = $name;

                    fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                        formatter.write_str("a base64 string")
                    }

                    fn visit_str<E>(self, v: &str) -> ::std::result::Result<Self::Value, E>
                        where E: ::serde::de::Error
                    {
                        $name::from_base64(v).map_err(|s| ::serde::de::Error::custom(s))
                    }

                }
                deserializer.deserialize_str(Visitor)
            }
        }

        impl Clone for $name {
            fn clone(&self) -> $name {
                *self
            }
        }

        impl ::std::fmt::Debug for $name {
            fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
                write!(fmt, "{:?}", &self.0 as &[u8])
            }
        }

        impl ::std::default::Default for $name {
            fn default() -> $name {
                $name([0; $size])
            }
        }

        impl ::std::cmp::PartialEq for $name {
            fn eq(&self, other: &$name) -> bool {
                (0..$size).all(|i| self.0[i] == other.0[i])
            }
        }

        impl ::std::cmp::Eq for $name { }

        impl ::std::ops::Deref for $name {
            type Target = [u8; $size];
            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl ::std::convert::From<[u8; $size]> for $name {
            fn from(data: [u8; $size]) -> $name {
                $name(data)
            }
        }
    }
}

B!(B8, 8);
B!(B32, 32);
B!(B64, 64);

#[cfg(test)]
mod tests {
    use serde_test::{Token, assert_tokens};

    #[test]
    fn test_b64_raw() {
        let a = [0u8; 8];
        let a64 = ::data_encoding::base64::encode(&a);
        assert_eq!("AAAAAAAAAAA=", a64);

        let mut b = [0u8; 8];
        let decode = &a64.as_bytes()[0..::data_encoding::base64::encode_nopad_len(8)];
        ::data_encoding::base64::decode_nopad_mut(decode, &mut b).unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn test_b64() {
        let a = super::B8::default();
        assert_eq!("AAAAAAAAAAA=", a.to_base64());
        let clone = super::B8::from_base64(&*a.to_base64()).unwrap();
        assert_eq!(a, clone);
    }

    #[test]
    fn test_serde() {
        #[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
        struct T {
            a: super::B8,
            b: super::B32,
            c: super::B64,
        };
        let mut t = T::default();
        assert_tokens(&t,
                      &[Token::StructStart("T", 3),

                        Token::StructSep,
                        Token::Str("a"),
                        Token::Str("AAAAAAAAAAA="),

                        Token::StructSep,
                        Token::Str("b"),
                        Token::Str("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        Token::StructSep,
                        Token::Str("c"),
                        Token::Str("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="),

                        Token::StructEnd]);
    }
}