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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#[macro_export]
macro_rules! probor_struct_encoder {
    ($name:ident { $( $item:ident => ( $($props:tt)* ), )* }
    ) => {
        impl $crate::Encodable for $name {
            fn encode<W: $crate::Output>(&self,
                e: &mut $crate::_cbor::Encoder<W>)
                -> Result<(), $crate::_cbor::EncodeError>
            {
                probor_enc_struct!(e, self, {
                    $( $item => ( $($props)* ), )*
                });
                Ok(())
            }
        }
    }
}

#[macro_export]
macro_rules! probor_struct_decoder {
    ($name:ident { $( $item:ident => ( $($props:tt)* ), )* }
    ) => {
        impl $crate::Decodable for $name {
            fn decode_opt<R: $crate::Input>(
                d: &mut $crate::_cbor::Decoder<R>)
                -> Result<Option<Self>, $crate::DecodeError>
            {
                probor_dec_struct!(d, {
                    $( $item => ( $($props)* ), )*
                });
                Ok(Some($name {
                    $( $item: $item, )*
                }))
            }
        }
    }
}
#[macro_export]
macro_rules! probor_struct_encoder_decoder {
    ($name:ident { $( $item:ident => ( $($props:tt)* ), )* }
    ) => {
        probor_struct_encoder!($name { $( $item => ( $($props)* ), )* } );
        probor_struct_decoder!($name { $( $item => ( $($props)* ), )* } );
    }
}

/// Declares structure
#[macro_export]
macro_rules! probor_struct {
    // Public struct with public fields
    ( $( #[$tag:meta] )* pub struct $name:ident
      { $(pub $item:ident: $typ:ty => ( $($props:tt)* ), )* }
    ) => {
        $( #[$tag] )*
        pub struct $name {
            $(pub $item: $typ, )*
        }

        probor_struct_encoder_decoder!($name
            { $( $item => ( $($props)* ), )* });
    };
    // Public struct with private fields
    ( $( #[$tag:meta] )* pub struct $name:ident
      { $( $item:ident: $typ:ty => ( $($props:tt)* ), )* }
    ) => {
        $( #[$tag] )*
        pub struct $name {
            $( $item: $typ, )*
        }

        probor_struct_encoder_decoder!($name
            { $( $item => ( $($props)* ), )* });
    };
    // Private struct with public fields
    ( $( #[$tag:meta] )* struct $name:ident
      { $(pub $item:ident: $typ:ty => ( $($props:tt)* ), )* }
    ) => {
        $( #[$tag] )*
        struct $name {
            $(pub $item: $typ, )*
        }

        probor_struct_encoder_decoder!($name
            { $( $item => ( $($props)* ), )* });
    };
    // Private struct with private fields
    ( $( #[$tag:meta] )* struct $name:ident
      { $( $item:ident: $typ:ty => ( $($props:tt)* ), )* }
    ) => {
        $( #[$tag] )*
        struct $name {
            $( $item: $typ, )*
        }

        probor_struct_encoder_decoder!($name
            { $( $item => ( $($props)* ), )* });
    };
}

#[cfg(test)]
mod test_search_results {
    use {Encodable, Decodable, Encoder, Decoder, Config};
    use std::io::Cursor;
    use {decode, to_buf, from_slice};

    fn roundtrip<A:Encodable, B:Decodable>(v: A) -> B {
        let v = to_buf(&v);
        println!("Data {:?} {:?}", String::from_utf8_lossy(&v), v);
        from_slice(&v).unwrap()
    }

    probor_struct!(
    #[derive(PartialEq, Eq, Debug)]
    struct Page {
        url: String => (#0),
        title: String => (#1),
        snippet: Option<String> => (#2 optional),
    });

    probor_struct!(
    #[derive(PartialEq, Eq, Debug)]
    struct SearchResults {
        total_results: u64 => (#0),
        results: Vec<Page> => (#1),
    });


    #[test]
    fn test_encode() {
        let buf = Vec::new();
        let mut enc = Encoder::new(buf);
        SearchResults {
            total_results: 112,
            results: vec![Page {
                url: "http://url1.example.com".to_string(),
                title: "One example".to_string(),
                snippet: None,
            }, Page {
                url: "http://url2.example.com".to_string(),
                title: "Two example".to_string(),
                snippet: Some("Example Two".to_string()),
            }],
        }.encode(&mut enc).unwrap();
        let val = &enc.into_writer()[..];
        let orig = &b"\x82\x18\x70\x82\
            \x83\x77http://url1.example.com\x6bOne example\xf6\
            \x83\x77http://url2.example.com\x6bTwo example\x6bExample Two"[..];
        assert_eq!(val.len(), orig.len());
        assert_eq!(&val, &orig);
    }

    #[test]
    fn test_decode() {
        let orig = &b"\x82\x18\x70\x82\
            \x83\x77http://url1.example.com\x6bOne example\xf6\
            \x83\x77http://url2.example.com\x6bTwo example\x6bExample Two"[..];
        let mut dec = Decoder::new(Config::default(), Cursor::new(orig));
        let val = decode(&mut dec).unwrap();
        assert_eq!(dec.into_reader().position() as usize, orig.len());
        assert_eq!(SearchResults {
            total_results: 112,
            results: vec![Page {
                url: "http://url1.example.com".to_string(),
                title: "One example".to_string(),
                snippet: None,
            }, Page {
                url: "http://url2.example.com".to_string(),
                title: "Two example".to_string(),
                snippet: Some("Example Two".to_string()),
            }],
        }, val);
    }

    probor_struct!(
    pub struct VersionInfo {
        pub version: u8 => (),
    });


    #[test]
    fn one_field() {
        let v = VersionInfo { version: 1 };
        let mut e = Encoder::new(Vec::new());
        v.encode(&mut e).unwrap();
        let vec = e.into_writer();
        assert_eq!(&vec[..], b"\xa1\x67version\x01");
        let mut d = &mut Decoder::new(Config::default(), Cursor::new(vec));
        let ver: VersionInfo = decode(d).unwrap();
        assert_eq!(ver.version, 1);
    }

    probor_struct!(
    #[derive(Debug, PartialEq)]
    struct Three {
        one: Option<u8> => (#0 optional),
        two: Option<u8> => (#1 optional),
        three: Option<u8> => (#2 optional),
    });

    probor_struct!(
    struct TwoThree {
        two: Option<u8> => (#1 optional),
        three: Option<u8> => (#2 optional),
    });

    probor_struct!(
    struct OneThree {
        one: Option<u8> => (#0 optional),
        three: Option<u8> => (#2 optional),
    });

    probor_struct!(
    struct OneTwo {
        one: Option<u8> => (#0 optional),
        two: Option<u8> => (#1 optional),
    });

    probor_struct!(
    struct Named {
        one: u8 => (),
        two: u8 => (),
        three: u8 => (),
    });

    #[test]
    fn big_to_small() {
        let t = Three { one: Some(10), two: Some(20), three: Some(30) };
        let mut e = Encoder::new(Vec::new());
        t.encode(&mut e).unwrap();
        let vec = e.into_writer();
        assert_eq!(&vec[..], b"\x83\x0a\x14\x18\x1e");
        let v: Three = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(10));
        assert_eq!(v.two, Some(20));
        assert_eq!(v.three, Some(30));
        let v: TwoThree = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.two, Some(20));
        assert_eq!(v.three, Some(30));
        let v: OneTwo = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(10));
        assert_eq!(v.two, Some(20));
        let v: OneThree = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(10));
        assert_eq!(v.three, Some(30));
    }


    #[test]
    fn small_to_big() {
        assert_eq!(roundtrip::<_, Three>(OneTwo { one: Some(11), two: Some(22) }),
            Three { one: Some(11), two: Some(22), three: None });
        assert_eq!(roundtrip::<_, Three>(TwoThree { two: Some(32), three: Some(33) }),
            Three { one: None, two: Some(32), three: Some(33) });
        assert_eq!(roundtrip::<_, Three>(OneThree { one: Some(41), three: Some(43) }),
            Three { one: Some(41), two: None, three: Some(43) });
    }

    #[test]
    fn named_to_small() {
        let t = Named { one: 11, two: 21, three: 31 };
        let mut e = Encoder::new(Vec::new());
        t.encode(&mut e).unwrap();
        let vec = e.into_writer();
        assert_eq!(&vec[..], b"\xa3\x63one\x0b\x63two\x15\x65three\x18\x1f");
        let v: Three = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(11));
        assert_eq!(v.two, Some(21));
        assert_eq!(v.three, Some(31));
        let v: TwoThree = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.two, Some(21));
        assert_eq!(v.three, Some(31));
        let v: OneTwo = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(11));
        assert_eq!(v.two, Some(21));
        let v: OneThree = decode(&mut Decoder::new(
            Config::default(), Cursor::new(&vec[..]))).unwrap();
        assert_eq!(v.one, Some(11));
        assert_eq!(v.three, Some(31));
    }

}