|
4 | 4 | // Use of this source code is governed by a BSD-style |
5 | 5 | // license that can be found in the LICENSE file. |
6 | 6 |
|
7 | | -//! Tiny CBOR encoder and decoder. |
| 7 | +//! Tiny, security-focused CBOR encoder/decoder. |
8 | 8 | //! |
9 | | -//! https://datatracker.ietf.org/doc/html/rfc8949 |
| 9 | +//! <https://datatracker.ietf.org/doc/html/rfc8949> |
10 | 10 | //! |
11 | | -//! This is an implementation of the CBOR spec with an extremely reduced type |
12 | | -//! system, focusing on security rather than flexibility or completeness. The |
13 | | -//! following types are supported: |
14 | | -//! - Booleans: bool |
15 | | -//! - Null: Option<T>::None, cbor::Null |
16 | | -//! - 64bit positive integers: u64 |
17 | | -//! - 64bit signed integers: i64 |
18 | | -//! - UTF-8 text strings: String, &str |
19 | | -//! - Byte strings: Vec<u8>, &[u8], [u8; N] |
20 | | -//! - Arrays: (), (X,), (X,Y), ... tuples, or structs with #[cbor(array)] |
21 | | -//! - Maps: structs with #[cbor(key = N)] fields, Option<T>::None omitted |
| 11 | +//! # Type system |
| 12 | +//! |
| 13 | +//! Only a minimal subset of CBOR is supported: |
| 14 | +//! |
| 15 | +//! - **Booleans:** `bool` |
| 16 | +//! - **Integers:** `u64`, `i64` |
| 17 | +//! - **Text:** `String`, `&str` |
| 18 | +//! - **Bytes:** `Vec<u8>`, `&[u8]`, `[u8; N]` |
| 19 | +//! - **Null:** `Option<T>::None`, `cbor::Null` |
| 20 | +//! - **Arrays:** `()`, `(X,)`, `(X, Y)`, ... tuples, or structs with `#[cbor(array)]` |
| 21 | +//! - **Maps:** structs with `#[cbor(key = N)]` fields (integer keys, deterministic order) |
| 22 | +//! - **Raw:** `cbor::Raw` (opaque CBOR bytes, passed through without parsing) |
| 23 | +//! |
| 24 | +//! # Derive macros |
| 25 | +//! |
| 26 | +//! Array mode — fields encode positionally: |
| 27 | +//! |
| 28 | +//! ```ignore |
| 29 | +//! #[derive(Cbor)] |
| 30 | +//! #[cbor(array)] |
| 31 | +//! struct Foo { |
| 32 | +//! a: u64, |
| 33 | +//! b: String, |
| 34 | +//! } |
| 35 | +//! ``` |
| 36 | +//! |
| 37 | +//! Map mode — fields encode as key-value pairs sorted by CBOR key bytes: |
| 38 | +//! |
| 39 | +//! ```ignore |
| 40 | +//! #[derive(Cbor)] |
| 41 | +//! struct Bar { |
| 42 | +//! #[cbor(key = 1)] |
| 43 | +//! x: u64, // required |
| 44 | +//! #[cbor(key = 2)] |
| 45 | +//! y: Option<Vec<u8>>, // optional: omitted when None |
| 46 | +//! #[cbor(key = 3)] |
| 47 | +//! z: Option<Option<u64>>, // nullable: always present, value or null |
| 48 | +//! } |
| 49 | +//! ``` |
| 50 | +//! |
| 51 | +//! # Embedding |
| 52 | +//! |
| 53 | +//! Fields marked `#[cbor(embed)]` are flattened into the parent map. The embedded |
| 54 | +//! type must itself be a map-mode struct. Keys across all embeds and direct fields |
| 55 | +//! must not overlap. |
| 56 | +//! |
| 57 | +//! ```ignore |
| 58 | +//! #[derive(Cbor)] |
| 59 | +//! struct Token { |
| 60 | +//! #[cbor(embed)] |
| 61 | +//! identity: Identity, // keys from Identity merge into Token |
| 62 | +//! #[cbor(key = 3)] |
| 63 | +//! aud: String, |
| 64 | +//! } |
| 65 | +//! ``` |
| 66 | +//! |
| 67 | +//! # Encoding rules |
| 68 | +//! |
| 69 | +//! All output is deterministic (RFC 8949 Section 4.2.1): canonical shortest-form |
| 70 | +//! integers, map keys sorted by encoded bytes, no indefinite lengths, no floats, |
| 71 | +//! no tags. Decoders reject non-canonical input, duplicate keys, and out-of-order |
| 72 | +//! keys. |
22 | 73 |
|
23 | 74 | pub use darkbio_crypto_cbor_derive::Cbor; |
24 | 75 |
|
|
0 commit comments