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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*!
This crate provides an implementation of [RFC
8949](https://www.rfc-editor.org/rfc/rfc8949) — the Concise Binary Object
Representation (CBOR) — built on [serde](https://serde.rs).
CBOR adopts and modestly builds on the *data model* used by JSON, except the
encoding is in binary form. Its primary goals include a balance of
implementation size, message size and extensibility.
# Quick start
Use [`to_vec`]/[`to_writer`] to encode any [`serde::Serialize`] type and
[`from_slice`]/[`from_reader`] to decode any [`serde::Deserialize`] type:
```rust
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Photo {
title: String,
pixels: (u32, u32),
tags: Vec<String>,
}
let photo = Photo {
title: "Sunrise".into(),
pixels: (1920, 1080),
tags: vec!["morning".into(), "gradient".into()],
};
let bytes = cbor2::to_vec(&photo).unwrap();
let back: Photo = cbor2::from_slice(&bytes).unwrap();
assert_eq!(photo, back);
```
`from_slice` and `from_reader` deserialize one leading CBOR item. Use
[`validate`] first when a byte buffer must contain exactly one item, or use
[`de::Deserializer::into_iter`] for a CBOR sequence.
# Byte strings and `serde_bytes`
Serde's default data model treats `Vec<u8>` and `&[u8]` as sequences, so
they serialize as CBOR arrays, not byte strings. Use
[`serde_bytes`](https://docs.rs/serde_bytes/latest/serde_bytes/) when the
wire type should be major type 2.
```rust
let bytes = vec![1u8, 2, 3, 4];
// Bare Vec<u8>: [1, 2, 3, 4]
assert_eq!(hex::encode(cbor2::to_vec(&bytes).unwrap()), "8401020304");
// serde_bytes::ByteBuf: h'01020304'
let bytes = serde_bytes::ByteBuf::from(bytes);
assert_eq!(hex::encode(cbor2::to_vec(&bytes).unwrap()), "4401020304");
```
For struct fields, use serde's field adapter:
```rust
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Packet {
#[serde(with = "serde_bytes")]
payload: Vec<u8>,
}
let packet = Packet { payload: vec![0xde, 0xad, 0xbe, 0xef] };
assert_eq!(
hex::encode(cbor2::to_vec(&packet).unwrap()),
"a1677061796c6f616444deadbeef"
);
```
When building dynamic data directly, [`Value::Bytes`] already represents a
CBOR byte string:
```rust
let value = cbor2::Value::Bytes(vec![0xde, 0xad]);
assert_eq!(hex::encode(cbor2::to_vec(&value).unwrap()), "42dead");
```
# Dynamic values
When the shape of the data is not known in advance, decode into a
[`Value`], the CBOR equivalent of `serde_json::Value`. The [`cbor!`] macro
builds `Value`s with a JSON-like syntax:
```rust
use cbor2::{cbor, Value};
let value = cbor!({
"code": 415,
"message": null,
"tags": ["legacy", 1.5],
}).unwrap();
let bytes = cbor2::to_vec(&value).unwrap();
let back: Value = cbor2::from_slice(&bytes).unwrap();
assert_eq!(value, back);
```
`Value::serialized` and `Value::deserialized` convert between `Value` and
any type implementing the serde traits.
```rust
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Point {
x: i64,
y: i64,
}
let value = cbor2::Value::serialized(&Point { x: -2, y: 5 }).unwrap();
assert_eq!(value.to_string(), r#"{"x": -2, "y": 5}"#);
let point: Point = value.deserialized().unwrap();
assert_eq!(point, Point { x: -2, y: 5 });
```
# Raw values
A [`RawValue`] keeps one CBOR item as its raw encoded bytes — validated
for well-formedness, but never decoded. Serializing splices the bytes
into the stream untouched and deserializing captures them byte for byte,
which preserves the exact wire encoding for signature payloads,
pass-through items and deferred decoding:
```rust
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Signed {
#[serde(with = "serde_bytes")]
signature: Vec<u8>,
payload: cbor2::RawValue,
}
let bytes = cbor2::to_vec(&Signed {
signature: vec![0xde, 0xad],
payload: cbor2::RawValue::serialized(&("untouched", 42)).unwrap(),
}).unwrap();
let signed: Signed = cbor2::from_slice(&bytes).unwrap();
// Verify `signed.signature` over `signed.payload.as_bytes()`, then:
let (text, n): (String, u8) = signed.payload.deserialized().unwrap();
assert_eq!((text.as_str(), n), ("untouched", 42));
```
`TryFrom` converts in both directions between `RawValue` and [`Value`]:
decoding one way, encoding the other.
# CBOR sequences
CBOR sequences (RFC 8742) are streams of adjacent complete CBOR items.
Write them by calling [`to_writer`] repeatedly, and read them with
[`de::Deserializer::into_iter`]:
```rust
let mut stream = Vec::new();
cbor2::to_writer(&"hello", &mut stream).unwrap();
cbor2::to_writer(&42u64, &mut stream).unwrap();
let items: Vec<cbor2::Value> = cbor2::de::Deserializer::from_reader(&stream[..])
.into_iter()
.collect::<Result<_, _>>()
.unwrap();
assert_eq!(items, vec![cbor2::Value::from("hello"), cbor2::Value::from(42)]);
assert!(cbor2::validate(&stream[..]).is_err()); // not exactly one item
```
# Tags
CBOR data items can be wrapped in semantic [tags](tag) (RFC 8949 §3.4). The
wrapper types in the [`tag`] module capture and emit tags through serde:
```rust
use cbor2::tag::RequireExact;
// Tag 32: a URI.
type Uri = RequireExact<String, 32>;
let uri: Uri = RequireExact("https://example.com".into());
let bytes = cbor2::to_vec(&uri).unwrap();
assert_eq!(bytes[0], 0xd8); // tag(32)
```
# Integer map keys and tags (COSE)
Protocols like COSE (RFC 9052) key their maps with integers and wrap
their messages in tags, which serde's data model cannot express. With the
`derive` feature, [`#[derive(Cbor)]`](derive@Cbor) declares both — a textual
`#[serde(rename = "1")]` stays a *text* key, so there is no ambiguity
between the two. The derive generates the `Serialize` and `Deserialize`
impls itself, so serde's derives must not be repeated alongside it:
```rust
# #[cfg(feature = "derive")] {
use cbor2::Cbor;
#[derive(Debug, PartialEq, Cbor)]
#[cbor(tag = 98)]
struct CoseSign {
#[cbor(key = 1)]
kty: u8,
#[cbor(key = 3)]
alg: i8,
}
let key = CoseSign { kty: 2, alg: -7 };
let bytes = cbor2::to_vec(&key).unwrap();
assert_eq!(hex::encode(&bytes), "d862a201020326"); // 98({1: 2, 3: -7})
assert_eq!(cbor2::from_slice::<CoseSign>(&bytes).unwrap(), key);
# }
```
The tag is optional, and the serde attributes (`alias`, `default`,
`skip`, `with`, ...) work as usual; map types like `HashMap<String, _>`
are unaffected. The declared keys and tag stay inspectable at runtime
through the [`Cbor`](trait@Cbor) trait, which the derive implements
alongside the serde traits.
The derive touches neither the field names nor the type name — the
protocol details ride along on a hidden shadow type (see
[`ser::STRUCT_MARKER`]) recognized only by this crate's serializers — so
the same type still serializes naturally everywhere else. JSON, for
example, just works, with the original field names and no tag:
```rust
# #[cfg(feature = "derive")] {
# use cbor2::Cbor;
# #[derive(Debug, PartialEq, Cbor)]
# #[cbor(tag = 98)]
# struct CoseSign {
# #[cbor(key = 1)]
# kty: u8,
# #[cbor(key = 3)]
# alg: i8,
# }
# let key = CoseSign { kty: 2, alg: -7 };
let json = serde_json::to_string(&key).unwrap();
assert_eq!(json, r#"{"kty":2,"alg":-7}"#);
assert_eq!(serde_json::from_str::<CoseSign>(&json).unwrap(), key);
# }
```
# Allocation-free helpers
Three helpers work without touching the heap: [`validate`] checks that an
input is exactly one well-formed CBOR item (including text UTF-8 validity),
[`serialized_size`] computes the exact encoded size of any serializable
value, and [`to_slice`] encodes into a caller-provided buffer.
```rust
let value = ("hello", vec![1u8, 2, 3]);
let bytes = cbor2::to_vec(&value).unwrap();
assert_eq!(cbor2::serialized_size(&value).unwrap(), bytes.len() as u64);
assert!(cbor2::validate(&bytes[..]).is_ok());
assert!(cbor2::validate(&bytes[..bytes.len() - 1]).is_err()); // truncated
let mut buffer = [0u8; 16];
assert_eq!(cbor2::to_slice(&value, &mut buffer).unwrap(), &bytes[..]);
```
# Crate features
* **`std`** *(default)* — implements the [`io`] traits for every
`std::io::Read`/`std::io::Write` and adds the `HashMap` conversions.
Implies `alloc`.
* **`alloc`** — everything that needs a heap, without `std`: [`Value`],
[`to_vec`]/[`from_slice`]/[`from_reader`], [`RawValue`], [`diagnostic`],
the deterministic encoders and the [`cbor!`] macro. Readers and writers
are byte slices, `Vec<u8>`, or custom [`io`] trait implementations.
* **neither** — a `#![no_std]` core for constrained targets: streaming
serialization with [`to_writer`]/[`to_slice`]/[`serialized_size`],
[`validate`], the [`tag`] wrappers and the [`core`] header codec.
Deserializing through serde requires `alloc`.
* **`derive`** — the [`Cbor`](derive@Cbor) derive macro; works in all three modes
(deserialization again requiring `alloc`).
# Diagnostic notation
[`diagnostic`] renders raw CBOR as the human-readable text form of
RFC 8949 §8 — handy for logs and debugging. Working on the wire, it can
show what a [`Value`] cannot represent: indefinite-length markers,
`undefined`, and unassigned simple values. `Value` implements
[`Display`](std::fmt::Display) with the same notation, and
[`Debug`](std::fmt::Debug) pretty-prints it with two-space indentation.
```rust
let bytes = hex::decode("bf61610161629f0203ffff").unwrap();
assert_eq!(
cbor2::diagnostic(&bytes[..]).unwrap(),
r#"{_ "a": 1, "b": [_ 2, 3]}"#
);
let value = cbor2::cbor!({ "k": [1, -2.5, null] }).unwrap();
assert_eq!(value.to_string(), r#"{"k": [1, -2.5, null]}"#);
```
# Low-level headers
The [`core`] module exposes the pull/push header codec for applications
that need to preserve wire structure such as indefinite-length strings:
```rust
use cbor2::core::{Decoder, Encoder, Header};
let mut bytes = Vec::new();
let mut enc = Encoder::from(&mut bytes);
enc.push(Header::Array(None)).unwrap();
enc.text("chunked").unwrap();
enc.bytes(&[0xde, 0xad]).unwrap();
enc.push(Header::Break).unwrap();
let mut dec = Decoder::from(&bytes[..]);
assert_eq!(dec.pull().unwrap(), Header::Array(None));
let Header::Text(len) = dec.pull().unwrap() else { unreachable!() };
let mut text = String::new();
dec.text_body(len, &mut text).unwrap();
assert_eq!(text, "chunked");
let Header::Bytes(len) = dec.pull().unwrap() else { unreachable!() };
let mut body = Vec::new();
dec.bytes_body(len, &mut body).unwrap();
assert_eq!(body, vec![0xde, 0xad]);
assert_eq!(dec.pull().unwrap(), Header::Break);
```
# Deterministic encoding
[`to_canonical_vec`]/[`to_canonical_writer`] produce output satisfying the
core deterministic encoding requirements of RFC 8949 §4.2.1: preferred
(smallest) serializations, definite lengths only, and map keys sorted in the
bytewise lexicographic order of their encodings. [`Value::canonicalize`]
applies the same normalization to a `Value` in place.
```rust
use std::collections::HashMap;
// HashMap iteration order is random, but the encoding is stable.
let map: HashMap<&str, i32> = [("z", 1), ("aa", 2), ("b", 3)].into();
let bytes = cbor2::to_canonical_vec(&map).unwrap();
assert_eq!(bytes, cbor2::to_canonical_vec(&map).unwrap());
assert_eq!(hex::encode(&bytes), "a3616203617a01626161 02".replace(' ', ""));
```
Many existing protocols instead use the older "Canonical CBOR" key order of
RFC 7049 §3.9 (kept as RFC 8949 §4.2.3), where shorter encoded keys sort
first. Pass [`KeyOrder::LengthFirst`] to the `*_with` variants for that:
```rust
use cbor2::KeyOrder;
let map: std::collections::HashMap<i64, bool> = [(100, true), (-1, false)].into();
// Bytewise (RFC 8949 §4.2.1): 100 (0x1864) sorts before -1 (0x20).
let core = cbor2::to_canonical_vec(&map).unwrap();
assert_eq!(hex::encode(&core), "a2 1864f5 20f4".replace(' ', ""));
// Length-first (RFC 7049 §3.9): -1 sorts before 100.
let legacy = cbor2::to_canonical_vec_with(&map, KeyOrder::LengthFirst).unwrap();
assert_eq!(hex::encode(&legacy), "a2 20f4 1864f5".replace(' ', ""));
```
# Design decisions
This implementation is wire-compatible with
[`ciborium`](https://docs.rs/ciborium), whose design it follows:
* **Numbers are always encoded in their smallest lossless form**, as
deterministic encoding (RFC 8949 §4.2.1) requires. Integer width in Rust
is treated as an in-memory detail, not a wire property: `1u64` encodes as
one byte, and that byte happily decodes into a `u128` or an `i8`.
* **`u128`/`i128` values outside the 64-bit range** are encoded as bignums
(tags 2 and 3), and bignums small enough to fit are accepted for any
integer type.
* **Maps are represented as `Vec<(Value, Value)>`** in [`Value`], preserving
wire order and arbitrary (even duplicate) keys.
* **Be liberal in what you accept**: decoding handles indefinite-length
items, segmented strings, half-width floats, leading zeros in bignums and
unknown tags in most positions, even though encoding never produces most
of those forms.
* **Deeply nested input fails with
[`RecursionLimitExceeded`](de::Error::RecursionLimitExceeded)** instead of
exhausting the stack; see [`de::Deserializer::with_recursion_limit`].
# History
This crate descends from `cbor` by Andrew Gallant, whose 0.4 and earlier
releases were built on the long-deprecated `rustc-serialize` framework and
predate both serde 1.0 and RFC 8949. Version 0.5 was a from-scratch rewrite
published under the `cbor2` name — the original crates.io name stays with
the legacy release — and 1.0 stabilizes it; none of the old API survives.
*/
extern crate alloc;
pub use cratevalidate;
pub use crate;
pub use cratediagnostic;
pub use crateRawValue;
pub use crate;
pub use crate;
pub use crate;
// Internal items that the `cbor!` macro expansion needs to reach through
// `$crate`. Not public API.
/// Derives [`serde::Serialize`] and [`serde::Deserialize`] with CBOR
/// protocol details: integer map keys and a CBOR tag (COSE, RFC 9052).
///
/// Annotate fields with `#[cbor(key = <integer>)]` and the container with
/// `#[cbor(tag = <integer>)]`. Do **not** also derive serde's
/// `Serialize`/`Deserialize` — this macro generates both impls. Field
/// names and the type name stay untouched, so the same type still
/// serializes naturally to JSON and other formats. See the [crate-level
/// documentation](crate#integer-map-keys-and-tags-cose) for examples.
///
/// The declared protocol details are also exposed for runtime inspection
/// through the [`Cbor`](trait@Cbor) trait, which this macro implements.
pub use Cbor;
/// The CBOR protocol details a [`#[derive(Cbor)]`](derive@Cbor) type
/// declares: its integer map keys and its tag.
///
/// The derive implements this trait alongside `Serialize` and
/// `Deserialize`, so the `#[cbor(...)]` attributes stay inspectable at
/// runtime — for building protocol documentation, validating foreign
/// input against the declared keys, or driving generic code off the tag.
///
/// ```rust
/// # #[cfg(feature = "derive")] {
/// use cbor2::Cbor; // one import: the derive macro and this trait
///
/// #[derive(Cbor)]
/// #[cbor(tag = 98)]
/// struct CoseSign {
/// #[cbor(key = 1)]
/// kty: u8,
/// #[cbor(key = 3)]
/// alg: i8,
/// comment: String, // no key: stays a text key on the wire
/// }
///
/// assert_eq!(CoseSign::KEYS, &[("kty", 1), ("alg", 3)]);
/// assert_eq!(CoseSign::TAG, Some(98));
///
/// let key = CoseSign { kty: 2, alg: -7, comment: "".into() };
/// assert_eq!(key.keys()["kty"], 1);
/// # }
/// ```
/// Builds a [`Value`] from JSON-like syntax.
///
/// Maps use `:` between keys and values, exactly like `serde_json::json!`;
/// any expression implementing [`serde::Serialize`] can be inlined,
/// including nested `cbor!` maps and arrays. Going beyond JSON, map keys
/// may be any CBOR value — integers included — and `null` is the CBOR
/// null. The macro returns `Result<Value, value::Error>`.
///
/// ```rust
/// use cbor2::cbor;
///
/// let value = cbor!({
/// "code": 415,
/// "message": null,
/// "continue": false,
/// "extra": { "numbers": [8.2341e+4, 0.251425] },
/// 1: "an integer key",
/// }).unwrap();
/// ```
///
/// The ciborium-style `=>` separator is accepted as well, and is handy
/// when a key expression itself contains a colon (alternatively,
/// parenthesize the key):
///
/// ```rust
/// use cbor2::cbor;
///
/// const ALG: i8 = 1;
///
/// let value = cbor!({ ALG => -7, (i8::MAX) : 0 }).unwrap();
/// ```
;
// Insert the current entry followed by a trailing comma.
=> ;
// Current entry followed by an unexpected token.
=> ;
// Insert the last entry without a trailing comma.
=> ;
// Next value is an array.
=> ;
=> ;
// Next value is a map.
=> ;
=> ;
// Next value is an expression followed by a comma.
=> ;
=> ;
// Last value is an expression with no trailing comma.
=> ;
=> ;
// Missing value for the last entry: "unexpected end of macro
// invocation".
=> ;
=> ;
// Missing separator and value for the last entry.
=> ;
// Misplaced separator: no key came before it. "No rules expected
// the token `:`/`=>`".
=> ;
=> ;
// A comma inside a key. "No rules expected the token `,`".
=> ;
// A fully parenthesized key — for key expressions containing `:`.
=> ;
=> ;
// Refuse to absorb a separator into the key expression.
=> ;
=> ;
// Munch a token into the current key.
=> ;
//////////// keys and leaves ////////////
// A nested map or array as the key.
=> ;
=> ;
=> ;
// Any serializable expression; `null` is the CBOR null.
=> ;
//////////// entry points ////////////
=> ;
=> ;
=> ;
}
// Produces a "no rules expected the token ..." error at the offending
// token. Not public API.
// Produces an "expected expression followed by `,`"-shaped error at the
// offending tokens. Not public API.