brotopuf 0.1.1

A library for (de)serializing structs in protocol buffer wire format
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
pub use brotopuf_core::deserialize::read_tag;
pub use brotopuf_core::deserialize::read_uvarint;
pub use brotopuf_core::deserialize::Deserialize;
pub use brotopuf_core::deserialize::DeserializeError;
pub use brotopuf_core::deserialize::DeserializeField;
pub use brotopuf_core::proto_type::ProtoType;
pub use brotopuf_core::proto_type::WireType;
pub use brotopuf_core::serialize::write_tag;
pub use brotopuf_core::serialize::write_uvarint;
pub use brotopuf_core::serialize::Serialize;
pub use brotopuf_macro::Deserialize;
pub use brotopuf_macro::Serialize;

#[cfg(test)]
mod tests {
    mod brotopuf {
        pub use super::super::*;
    }
    use brotopuf::Deserialize;
    use brotopuf::Serialize;

    #[derive(Copy, Clone, Serialize, Deserialize)]
    enum TestEnum {
        VariantZero = 0,
        VariantOne = 1,
        VariantTwo = 2,
    }

    impl TryFrom<u64> for TestEnum {
        type Error = std::io::Error;

        fn try_from(value: u64) -> Result<Self, Self::Error> {
            match value {
                0 => Ok(TestEnum::VariantZero),
                1 => Ok(TestEnum::VariantOne),
                2 => Ok(TestEnum::VariantTwo),
                _ => Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("invalid TestEnum value: {}", value),
                )),
            }
        }
    }

    #[derive(Serialize, Deserialize)]
    struct SubMessage {
        #[id(1)]
        int32: i32,
    }

    #[derive(Serialize, Deserialize)]
    struct TestMessage {
        #[id(1)]
        int32: i32,

        #[id(2)]
        int64: i64,

        #[id(3)]
        uint32: u32,

        #[id(4)]
        uint64: u64,

        #[id(5)]
        #[pbtype(sint32)]
        sint32: i32,

        #[id(6)]
        #[pbtype(sint64)]
        sint64: i64,

        #[id(7)]
        boolean: bool,

        #[id(8)]
        #[pbtype(fixed64)]
        fixed64: u64,

        #[id(9)]
        #[pbtype(sfixed64)]
        sfixed64: i64,

        #[id(10)]
        #[pbtype(fixed32)]
        fixed32: u32,

        #[id(11)]
        #[pbtype(sfixed32)]
        sfixed32: i32,

        #[id(12)]
        double: f64,

        #[id(13)]
        float: f32,

        #[id(14)]
        string: String,

        #[id(16)]
        bytes: Vec<u8>,

        #[id(18)]
        enumeration: TestEnum,

        #[id(19)]
        submessage: SubMessage,

        #[id(20)]
        repeated: Vec<u32>,
    }

    #[derive(Serialize)]
    struct TestOptionalMessage {
        #[id(1)]
        int32: Option<i32>,

        #[id(2)]
        int64: Option<i64>,

        #[id(3)]
        uint32: Option<u32>,

        #[id(4)]
        uint64: Option<u64>,

        #[id(5)]
        #[pbtype(sint32)]
        sint32: Option<i32>,

        #[id(6)]
        #[pbtype(sint64)]
        sint64: Option<i64>,

        #[id(7)]
        boolean: Option<bool>,

        #[id(8)]
        #[pbtype(fixed64)]
        fixed64: Option<u64>,

        #[id(9)]
        #[pbtype(sfixed64)]
        sfixed64: Option<i64>,

        #[id(10)]
        #[pbtype(fixed32)]
        fixed32: Option<u32>,

        #[id(11)]
        #[pbtype(sfixed32)]
        sfixed32: Option<i32>,

        #[id(12)]
        double: Option<f64>,

        #[id(13)]
        float: Option<f32>,

        #[id(14)]
        string: Option<String>,

        #[id(16)]
        bytes: Option<Vec<u8>>,

        #[id(18)]
        enumeration: Option<TestEnum>,

        #[id(19)]
        submessage: Option<SubMessage>,
    }

    #[test]
    fn test_derive_types() {
        let s = TestMessage {
            int32: 150,
            int64: 151,
            uint32: 152,
            uint64: 153,
            sint32: -1,
            sint64: -2,
            boolean: true,
            fixed64: 154,
            sfixed64: -3,
            fixed32: 155,
            sfixed32: -4,
            double: 3.14159,
            float: -1234.0,
            string: "hello".to_owned(),
            bytes: vec![1, 2, 3],
            enumeration: TestEnum::VariantTwo,
            submessage: SubMessage { int32: 150 },
            repeated: vec![156, 157, 158],
        };
        let mut v = Vec::new();
        s.serialize(&mut v).unwrap();
        assert_eq!(
            v,
            vec![
                0x08, 0x96, 0x01, // int32
                0x10, 0x97, 0x01, // int64
                0x18, 0x98, 0x01, // uint32
                0x20, 0x99, 0x01, // uint64
                0x28, 0x01, // sint32
                0x30, 0x03, // sint64
                0x38, 0x01, // bool
                0x41, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // fixed64
                0x49, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // sfixed64
                0x55, 0x9b, 0x00, 0x00, 0x00, // fixed32
                0x5d, 0xfc, 0xff, 0xff, 0xff, // sfixed32
                0x61, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40, // double
                0x6d, 0x00, 0x40, 0x9a, 0xc4, // float
                0x72, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // string
                0x82, 0x01, 0x03, 0x01, 0x02, 0x03, // Vec<u8>
                0x90, 0x01, 0x02, // enum
                0x9a, 0x01, 0x03, 0x08, 0x96, 0x01, // submessage
                0xa0, 0x01, 0x9c, 0x01, 0xa0, 0x01, 0x9d, 0x01, 0xa0, 0x01, 0x9e,
                0x01, // repeated
            ]
        );
    }

    #[test]
    fn test_derive_optional_types() {
        let s = TestOptionalMessage {
            int32: Some(150),
            int64: Some(151),
            uint32: Some(152),
            uint64: Some(153),
            sint32: Some(-1),
            sint64: Some(-2),
            boolean: Some(true),
            fixed64: Some(154),
            sfixed64: Some(-3),
            fixed32: Some(155),
            sfixed32: Some(-4),
            double: Some(3.14159),
            float: Some(-1234.0),
            string: Some("hello".to_owned()),
            bytes: Some(vec![1, 2, 3]),
            enumeration: Some(TestEnum::VariantTwo),
            submessage: Some(SubMessage { int32: 150 }),
        };
        let mut v = Vec::new();
        s.serialize(&mut v).unwrap();
        assert_eq!(
            v,
            vec![
                0x08, 0x96, 0x01, // int32
                0x10, 0x97, 0x01, // int64
                0x18, 0x98, 0x01, // uint32
                0x20, 0x99, 0x01, // uint64
                0x28, 0x01, // sint32
                0x30, 0x03, // sint64
                0x38, 0x01, // bool
                0x41, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // fixed64
                0x49, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // sfixed64
                0x55, 0x9b, 0x00, 0x00, 0x00, // fixed32
                0x5d, 0xfc, 0xff, 0xff, 0xff, // sfixed32
                0x61, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40, // double
                0x6d, 0x00, 0x40, 0x9a, 0xc4, // float
                0x72, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // string
                0x82, 0x01, 0x03, 0x01, 0x02, 0x03, // Vec<u8>
                0x90, 0x01, 0x02, // enum
                0x9a, 0x01, 0x03, 0x08, 0x96, 0x01, // submessage
            ]
        );
    }

    #[test]
    fn test_derive_optional_types_empty() {
        let s = TestOptionalMessage {
            int32: None,
            int64: None,
            uint32: None,
            uint64: None,
            sint32: None,
            sint64: None,
            boolean: None,
            fixed64: None,
            sfixed64: None,
            fixed32: None,
            sfixed32: None,
            double: None,

            float: None,
            string: None,
            bytes: None,
            enumeration: None,
            submessage: None,
        };
        let mut v = Vec::new();
        s.serialize(&mut v).unwrap();
        assert_eq!(v, vec![]);
    }

    #[test]
    fn test_derive_deserialize_types() {
        let v = vec![
            0x08, 0x96, 0x01, // int32
            0x10, 0x97, 0x01, // int64
            0x18, 0x98, 0x01, // uint32
            0x20, 0x99, 0x01, // uint64
            0x28, 0x01, // sint32
            0x30, 0x03, // sint64
            0x38, 0x01, // bool
            0x41, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // fixed64
            0x49, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // sfixed64
            0x55, 0x9b, 0x00, 0x00, 0x00, // fixed32
            0x5d, 0xfc, 0xff, 0xff, 0xff, // sfixed32
            0x61, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40, // double
            0x6d, 0x00, 0x40, 0x9a, 0xc4, // float
            0x72, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // string
            0x82, 0x01, 0x03, 0x01, 0x02, 0x03, // Vec<u8>
            0x90, 0x01, 0x02, // enum
            0x9a, 0x01, 0x03, 0x08, 0x96, 0x01, // submessage
            0xa0, 0x01, 0x9c, 0x01, 0xa0, 0x01, 0x9d, 0x01, 0xa0, 0x01, 0x9e,
            0x01, // repeated
        ];

        let mut actual = TestMessage {
            int32: 0,
            int64: 0,
            uint32: 0,
            uint64: 0,
            sint32: 0,
            sint64: 0,
            boolean: false,
            fixed64: 0,
            sfixed64: 0,
            fixed32: 0,
            sfixed32: 0,
            double: 0.0,
            float: 0.0,
            string: "".to_owned(),
            bytes: vec![],
            enumeration: TestEnum::VariantTwo,
            submessage: SubMessage { int32: 0 },
            repeated: vec![],
        };

        actual.deserialize(&mut &v[..]).unwrap();

        let expected = TestMessage {
            int32: 150,
            int64: 151,
            uint32: 152,
            uint64: 153,
            sint32: -1,
            sint64: -2,
            boolean: true,
            fixed64: 154,
            sfixed64: -3,
            fixed32: 155,
            sfixed32: -4,
            double: 3.14159,
            float: -1234.0,
            string: "hello".to_owned(),
            bytes: vec![1, 2, 3],
            enumeration: TestEnum::VariantTwo,
            submessage: SubMessage { int32: 150 },
            repeated: vec![156, 157, 158],
        };

        assert_eq!(expected.int32, actual.int32);
        assert_eq!(expected.int64, actual.int64);
        assert_eq!(expected.uint32, actual.uint32);
        assert_eq!(expected.uint64, actual.uint64);
        assert_eq!(expected.sint32, actual.sint32);
        assert_eq!(expected.sint64, actual.sint64);
        assert_eq!(expected.double, actual.double);
        assert_eq!(expected.float, actual.float);
        assert_eq!(expected.string, actual.string);
        assert_eq!(expected.bytes, actual.bytes);
        assert_eq!(expected.submessage.int32, actual.submessage.int32);
        assert_eq!(expected.repeated, actual.repeated);

        match actual.enumeration {
            TestEnum::VariantTwo => {}
            _ => panic!("incorrect variant"),
        }
    }

    #[test]
    fn test_derive_deserialize_types_from_cursor() {
        let v = vec![
            0x08, 0x96, 0x01, // int32
            0x10, 0x97, 0x01, // int64
            0x18, 0x98, 0x01, // uint32
            0x20, 0x99, 0x01, // uint64
            0x28, 0x01, // sint32
            0x30, 0x03, // sint64
            0x38, 0x01, // bool
            0x41, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // fixed64
            0x49, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // sfixed64
            0x55, 0x9b, 0x00, 0x00, 0x00, // fixed32
            0x5d, 0xfc, 0xff, 0xff, 0xff, // sfixed32
            0x61, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40, // double
            0x6d, 0x00, 0x40, 0x9a, 0xc4, // float
            0x72, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // string
            0x82, 0x01, 0x03, 0x01, 0x02, 0x03, // Vec<u8>
            0x90, 0x01, 0x02, // enum
            0x9a, 0x01, 0x03, 0x08, 0x96, 0x01, // submessage
            0xa0, 0x01, 0x9c, 0x01, 0xa0, 0x01, 0x9d, 0x01, 0xa0, 0x01, 0x9e,
            0x01, // repeated
        ];

        let mut actual = TestMessage {
            int32: 0,
            int64: 0,
            uint32: 0,
            uint64: 0,
            sint32: 0,
            sint64: 0,
            boolean: false,
            fixed64: 0,
            sfixed64: 0,
            fixed32: 0,
            sfixed32: 0,
            double: 0.0,
            float: 0.0,
            string: "".to_owned(),
            bytes: vec![],
            enumeration: TestEnum::VariantTwo,
            submessage: SubMessage { int32: 0 },
            repeated: vec![],
        };

        let mut cursor = std::io::Cursor::new(&v[..]);
        actual.deserialize(cursor.get_mut()).unwrap();

        let expected = TestMessage {
            int32: 150,
            int64: 151,
            uint32: 152,
            uint64: 153,
            sint32: -1,
            sint64: -2,
            boolean: true,
            fixed64: 154,
            sfixed64: -3,
            fixed32: 155,
            sfixed32: -4,
            double: 3.14159,
            float: -1234.0,
            string: "hello".to_owned(),
            bytes: vec![1, 2, 3],
            enumeration: TestEnum::VariantTwo,
            submessage: SubMessage { int32: 150 },
            repeated: vec![156, 157, 158],
        };

        assert_eq!(expected.int32, actual.int32);
        assert_eq!(expected.int64, actual.int64);
        assert_eq!(expected.uint32, actual.uint32);
        assert_eq!(expected.uint64, actual.uint64);
        assert_eq!(expected.sint32, actual.sint32);
        assert_eq!(expected.sint64, actual.sint64);
        assert_eq!(expected.double, actual.double);
        assert_eq!(expected.float, actual.float);
        assert_eq!(expected.string, actual.string);
        assert_eq!(expected.bytes, actual.bytes);
        assert_eq!(expected.submessage.int32, actual.submessage.int32);
        assert_eq!(expected.repeated, actual.repeated);

        match actual.enumeration {
            TestEnum::VariantTwo => {}
            _ => panic!("incorrect variant"),
        }
    }
}