zerocbor 0.1.0

A zero-copy, zero-dependency, no_std-compatible, extremely fast CBOR (RFC 8949) serializer for Rust.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Tests for the `ToCbor` and `FromCbor` derives.
//!
//! These cover the shapes the derive has to get right: the two representations,
//! ignored and renamed fields, and each enum form. The `zerompk_parity` module
//! covers the attribute names and the strictness rules.

#![allow(clippy::redundant_field_names)]

use zerocbor::{Error, FromCbor, ToCbor};

mod array_repr {
    use super::*;

    #[derive(Debug, PartialEq, ToCbor, FromCbor)]
    struct Point {
        x: i32,
        y: i32,
    }

    #[test]
    fn a_struct_is_an_array_in_field_order() {
        let encoded = zerocbor::to_cbor_vec(&Point { x: 1, y: 2 }).unwrap();
        assert_eq!(encoded, vec![0x82, 0x01, 0x02]);
        assert_eq!(
            zerocbor::from_cbor::<Point>(&encoded).unwrap(),
            Point { x: 1, y: 2 }
        );
    }

    #[test]
    fn a_wrong_length_is_rejected() {
        let err = zerocbor::from_cbor::<Point>(&[0x83, 0x01, 0x02, 0x03]).unwrap_err();
        assert!(
            matches!(
                err,
                Error::ArrayLengthMismatch {
                    expected: 2,
                    actual: 3
                }
            ),
            "got {err:?}"
        );
    }

    #[test]
    fn a_nested_struct_round_trips() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        struct Nested {
            origin: Point,
            points: Vec<Point>,
        }

        let value = Nested {
            origin: Point { x: 0, y: 0 },
            points: vec![Point { x: 1, y: 2 }, Point { x: -3, y: -4 }],
        };
        let encoded = zerocbor::to_cbor_vec(&value).unwrap();
        assert_eq!(zerocbor::from_cbor::<Nested>(&encoded).unwrap(), value);
    }

    #[test]
    fn a_fieldless_enum_is_a_bare_index() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[repr(u8)]
        enum Color {
            Red,
            Green,
            Blue,
        }

        // One integer per variant, with no length head: the cheapest form.
        for (value, expected) in [
            (Color::Red, 0x00u8),
            (Color::Green, 0x01),
            (Color::Blue, 0x02),
        ] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(encoded, vec![expected]);
            assert_eq!(zerocbor::from_cbor::<Color>(&encoded).unwrap(), value);
        }

        // An index the type does not name is reported rather than clamped.
        let err = zerocbor::from_cbor::<Color>(&[0x07]).unwrap_err();
        assert!(matches!(err, Error::UnknownVariantIndex(7)), "got {err:?}");
    }

    #[test]
    fn a_data_carrying_variant_carries_its_index_first() {
        // Whether the fields are named or positional makes no difference to the
        // encoding: each variant is an array whose element 0 is the index.
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        enum Shape {
            Circle(u32),
            Rect(u32, u32),
            Moved { dx: i32, dy: i32 },
        }

        for (value, expected) in [
            (Shape::Circle(5), vec![0x82, 0x00, 0x05]),
            (Shape::Rect(1, 2), vec![0x83, 0x01, 0x01, 0x02]),
            (Shape::Moved { dx: 1, dy: -1 }, vec![0x83, 0x02, 0x01, 0x20]),
        ] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(encoded, expected, "{value:?}");
            assert_eq!(zerocbor::from_cbor::<Shape>(&encoded).unwrap(), value);
        }
    }
}

mod map_repr {
    use super::*;

    #[derive(Debug, PartialEq, ToCbor, FromCbor)]
    #[cbor(map)]
    struct Point {
        x: i32,
        y: i32,
    }

    #[test]
    fn a_struct_is_a_map_keyed_by_field_name() {
        let encoded = zerocbor::to_cbor_vec(&Point { x: 1, y: 2 }).unwrap();
        assert_eq!(encoded, vec![0xa2, 0x61, b'x', 0x01, 0x61, b'y', 0x02]);
        assert_eq!(
            zerocbor::from_cbor::<Point>(&encoded).unwrap(),
            Point { x: 1, y: 2 }
        );
    }

    /// A three-entry map, so an unknown-key path can be exercised.
    fn wider_than_point() -> Vec<u8> {
        let mut wider = vec![0xa3];
        // x = 1
        wider.extend_from_slice(&[0x61, b'x', 0x01]);
        // y = 2
        wider.extend_from_slice(&[0x61, b'y', 0x02]);
        // z = "abc", an entry this type does not name
        wider.extend_from_slice(&[0x61, b'z', 0x63, b'a', b'b', b'c']);
        wider
    }

    /// Strict by default, so schema evolution is an explicit decision.
    #[test]
    fn a_wrong_map_is_reported() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map, allow_unknown_fields)]
        struct Tolerant {
            x: i32,
            y: i32,
        }

        // The count is checked before any key, so the keys never matter here.
        for (label, encoded, actual) in [
            ("one key too many", wider_than_point(), 3),
            ("one key too few", vec![0xa1, 0x61, b'x', 0x01], 1),
        ] {
            let err = zerocbor::from_cbor::<Point>(&encoded).unwrap_err();
            assert!(
                matches!(
                    err,
                    Error::MapLengthMismatch {
                        expected: 2,
                        actual: n
                    } if n == actual
                ),
                "{label}: got {err:?}"
            );
        }

        // With the count matching, the unrecognized key is named. Strict, so
        // nothing is skipped: `Renamed` is spelled so "z" is outside it.
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        struct Renamed {
            #[cbor(key = "a")]
            x: i32,
            #[cbor(key = "b")]
            y: i32,
        }
        let err = zerocbor::from_cbor::<Renamed>(&[0xa2, 0x61, b'a', 0x01, 0x61, b'z', 0x02])
            .unwrap_err();
        assert!(matches!(err, Error::UnknownVariant(_)), "got {err:?}");

        // Twice is a contradiction, not last-one-wins.
        let mut encoded = zerocbor::to_cbor_vec(&Tolerant { x: 1, y: 2 }).unwrap();
        encoded[0] = 0xa3;
        encoded.extend_from_slice(&[0x61, b'x', 0x09]);
        let err = zerocbor::from_cbor::<Tolerant>(&encoded).unwrap_err();
        assert!(matches!(err, Error::KeyDuplicated(_)), "got {err:?}");
    }

    #[test]
    fn allow_unknown_fields_opts_into_skipping_them() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map, allow_unknown_fields)]
        struct Tolerant {
            x: i32,
            y: i32,
        }

        // A newer writer's field does not break it, because it said so.
        assert_eq!(
            zerocbor::from_cbor::<Tolerant>(&wider_than_point()).unwrap(),
            Tolerant { x: 1, y: 2 },
        );
    }

    #[test]
    fn a_missing_key_is_reported() {
        // The length check fires before any key is read.
        let err = zerocbor::from_cbor::<Point>(&[0xa1, 0x61, b'x', 0x01]).unwrap_err();
        assert!(
            matches!(
                err,
                Error::MapLengthMismatch {
                    expected: 2,
                    actual: 1
                }
            ),
            "got {err:?}",
        );
    }

    #[test]
    fn default_fills_a_missing_key() {
        fn default_y() -> i32 {
            42
        }

        // The length is read, not checked, so `y` is filled rather than failing.
        // A bare `default` takes the type's own zero; a named one calls a function.
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        struct Zero {
            x: i32,
            #[cbor(default)]
            y: i32,
        }
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        struct Computed {
            x: i32,
            #[cbor(default = "default_y")]
            y: i32,
        }

        let encoded = [0xa1, 0x61, b'x', 0x07];
        assert_eq!(
            zerocbor::from_cbor::<Zero>(&encoded).unwrap(),
            Zero { x: 7, y: 0 },
        );
        assert_eq!(
            zerocbor::from_cbor::<Computed>(&encoded).unwrap(),
            Computed { x: 7, y: 42 },
        );
    }

    #[test]
    fn a_field_can_be_ignored() {
        #[derive(Debug, Default, PartialEq, ToCbor, FromCbor)]
        struct Skipped {
            kept: u8,
            #[cbor(ignore)]
            dropped: u8,
        }

        let value = Skipped {
            kept: 1,
            dropped: 2,
        };
        let encoded = zerocbor::to_cbor_vec(&value).unwrap();
        assert_eq!(encoded, vec![0x81, 0x01], "the ignored field is absent");
        assert_eq!(
            zerocbor::from_cbor::<Skipped>(&encoded).unwrap(),
            Skipped {
                kept: 1,
                dropped: 0,
            },
            "an ignored field falls back to Default"
        );
    }

    #[test]
    fn a_fieldless_enum_is_named() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        enum Color {
            Red,
            Green,
        }

        assert_eq!(
            zerocbor::to_cbor_vec(&Color::Green).unwrap(),
            vec![0x65, b'G', b'r', b'e', b'e', b'n'],
        );
        for value in [Color::Red, Color::Green] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(zerocbor::from_cbor::<Color>(&encoded).unwrap(), value);
        }
    }

    #[test]
    fn a_data_carrying_variant_is_wrapped_in_a_one_entry_map() {
        // The name keys, the fields are its value: tellable apart from a
        // fieldless variant's bare name.
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        enum Event {
            Nothing,
            Moved { x: i32, y: i32 },
            Renamed(String),
        }

        assert_eq!(
            zerocbor::to_cbor_vec(&Event::Moved { x: 1, y: 2 }).unwrap(),
            vec![
                0xa1, 0x65, b'M', b'o', b'v', b'e', b'd', 0xa2, 0x61, b'x', 0x01, 0x61, b'y', 0x02
            ],
        );
        // A tuple variant has no names, so an array under the name.
        assert_eq!(
            zerocbor::to_cbor_vec(&Event::Renamed("hi".into())).unwrap(),
            vec![
                0xa1, 0x67, b'R', b'e', b'n', b'a', b'm', b'e', b'd', 0x81, 0x62, b'h', b'i'
            ],
        );
        for value in [
            Event::Nothing,
            Event::Moved { x: 1, y: 2 },
            Event::Renamed("hi".into()),
        ] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(
                zerocbor::from_cbor::<Event>(&encoded).unwrap(),
                value,
                "round trip of {value:?}"
            );
        }
    }

    #[test]
    fn a_variant_name_that_does_not_resolve_is_reported() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        enum Event {
            Nothing,
            Moved { x: i32 },
        }

        for (label, encoded, expected) in [
            // A name the enum does not have.
            ("no such variant", vec![0x63, b'B', b'l', b'u'], "Blu"),
            // The name arrived bare, so there is nowhere for the fields to be.
            // The error names the variant rather than reporting a missing byte.
            (
                "a bare name with fields",
                vec![0x65, b'M', b'o', b'v', b'e', b'd'],
                "Moved",
            ),
        ] {
            let err = zerocbor::from_cbor::<Event>(&encoded).unwrap_err();
            assert!(
                matches!(err, Error::UnknownVariant(ref n) if n == expected),
                "{label}: got {err:?}"
            );
        }
    }
}

mod generics {
    use super::*;

    #[derive(Debug, PartialEq, ToCbor, FromCbor)]
    struct Wrapper<T> {
        value: T,
        count: u32,
    }

    #[test]
    fn a_generic_struct_round_trips() {
        let value = Wrapper {
            value: "hello".to_string(),
            count: 3,
        };
        let encoded = zerocbor::to_cbor_vec(&value).unwrap();
        assert_eq!(
            zerocbor::from_cbor::<Wrapper<String>>(&encoded).unwrap(),
            value
        );
    }

    #[derive(Debug, PartialEq, ToCbor, FromCbor)]
    struct Borrowed<'a> {
        text: &'a str,
        number: u32,
    }

    #[test]
    fn a_borrowing_struct_reads_without_copying() {
        let encoded = zerocbor::to_cbor_vec(&Borrowed {
            text: "borrowed",
            number: 5,
        })
        .unwrap();

        let decoded: Borrowed<'_> = zerocbor::from_cbor(&encoded).unwrap();
        assert_eq!(decoded.text, "borrowed");
        assert_eq!(decoded.number, 5);

        // The decoded string shares the input buffer rather than allocating.
        let base = encoded.as_ptr() as usize;
        let text = decoded.text.as_ptr() as usize;
        assert!(
            (base..base + encoded.len()).contains(&text),
            "the decoded str should point into the input slice",
        );
    }
}

mod option_fields {
    use super::*;

    #[derive(Debug, PartialEq, ToCbor, FromCbor)]
    struct Maybe {
        required: u32,
        optional: Option<u32>,
    }

    #[test]
    fn an_option_round_trips_in_both_states() {
        for value in [
            Maybe {
                required: 1,
                optional: None,
            },
            Maybe {
                required: 1,
                optional: Some(2),
            },
        ] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(zerocbor::from_cbor::<Maybe>(&encoded).unwrap(), value);
        }
    }
}

mod zerompk_parity {
    //! The attributes and their names follow the sibling crate, so code written
    //! for one reads the same in the other.
    use super::*;

    #[test]
    fn a_numeric_key_places_a_field_in_an_array() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        struct Sparse {
            #[cbor(key = 0)]
            a: u8,
            // `key = 2` leaves position 1 to be filled with `null`, so the
            // positions on the wire are the ones asked for.
            #[cbor(key = 2)]
            b: u8,
        }

        let encoded = zerocbor::to_cbor_vec(&Sparse { a: 1, b: 2 }).unwrap();
        assert_eq!(encoded, vec![0x83, 0x01, 0xf6, 0x02]);

        let decoded: Sparse = zerocbor::from_cbor(&encoded).unwrap();
        assert_eq!(decoded, Sparse { a: 1, b: 2 });
    }

    #[test]
    fn a_map_key_is_the_given_name() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(map)]
        struct Renamed {
            #[cbor(key = "the-x")]
            x: i32,
            y: i32,
        }

        // The keys are written in the order RFC 8949 Section 4.2.1 gives a
        // map's, which is length first, so the one-byte "y" leads even though
        // the field is declared second.
        let encoded = zerocbor::to_cbor_vec(&Renamed { x: 1, y: 2 }).unwrap();
        assert_eq!(
            encoded,
            vec![
                0xa2, 0x61, b'y', 0x02, 0x65, b't', b'h', b'e', b'-', b'x', 0x01
            ],
        );
        assert_eq!(
            zerocbor::from_cbor::<Renamed>(&encoded).unwrap(),
            Renamed { x: 1, y: 2 },
        );
    }

    #[test]
    fn c_enum_is_accepted_on_a_fieldless_enum() {
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        #[cbor(c_enum)]
        #[repr(u8)]
        enum Status {
            Ok = 0,
            NotFound = 1,
        }

        // A bare discriminant, which is what the attribute states.
        assert_eq!(
            zerocbor::to_cbor_vec(&Status::NotFound).unwrap(),
            vec![0x01]
        );
        for value in [Status::Ok, Status::NotFound] {
            let encoded = zerocbor::to_cbor_vec(&value).unwrap();
            assert_eq!(zerocbor::from_cbor::<Status>(&encoded).unwrap(), value);
        }
    }

    #[test]
    fn as_bytes_selects_a_byte_string_or_an_array_of_integers() {
        // The default follows the sibling crate: a byte-slice-shaped field is a
        // byte string unless it asks otherwise.
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        struct AsBlob {
            data: Vec<u8>,
        }
        #[derive(Debug, PartialEq, ToCbor, FromCbor)]
        struct AsArray {
            #[cbor(as_bytes = false)]
            data: Vec<u8>,
        }

        assert_eq!(
            zerocbor::to_cbor_vec(&AsBlob { data: vec![1, 2] }).unwrap(),
            vec![0x81, 0x42, 0x01, 0x02],
        );
        let encoded = zerocbor::to_cbor_vec(&AsArray { data: vec![1, 2] }).unwrap();
        assert_eq!(encoded, vec![0x81, 0x82, 0x01, 0x02]);
        assert_eq!(
            zerocbor::from_cbor::<AsArray>(&encoded).unwrap(),
            AsArray { data: vec![1, 2] },
        );
    }
}