dcbor_parse/
lib.rs

1//! # dCBOR Diagnostic Parser and Composer
2//!
3//! This crate provides tools for parsing and composing the [CBOR diagnostic
4//! notation](https://datatracker.ietf.org/doc/html/rfc8949#name-diagnostic-notation)
5//! into [dCBOR (deterministic
6//! CBOR)](https://datatracker.ietf.org/doc/draft-mcnally-deterministic-cbor/)
7//! data items.
8//!
9//! It is intended for use in testing, debugging, the `dcbor` command line tool,
10//! and other scenarios where a human-readable representation of dCBOR is
11//! useful. It is not optimized for performance and should not be used in
12//! production environments where binary dCBOR is expected.
13//!
14//! The primary functions provided are:
15//!
16//! - `parse_dcbor_item`: Parses a string in CBOR diagnostic notation into a
17//!   `CBOR` object.
18//! - `compose_dcbor_array`: Composes a `CBOR` array from a slice of strings
19//!   representing dCBOR items in diagnostic notation.
20//! - `compose_dcbor_map`: Composes a `CBOR` map from a slice of strings
21//!   representing the key-value pairs in dCBOR diagnostic notation.
22//!
23//! | Type                | Example(s)                                                  |
24//! | ------------------- | ----------------------------------------------------------- |
25//! | Boolean             | `true`<br>`false`                                           |
26//! | Null                | `null`                                                      |
27//! | Integers            | `0`<br>`1`<br>`-1`<br>`42`                                  |
28//! | Floats              | `3.14`<br>`-2.5`<br>`Infinity`<br>`-Infinity`<br>`NaN`      |
29//! | Strings             | `"hello"`<br>`"🌎"`                                      |
30//! | Date Literals       | `2023-02-08`<br>`2023-02-08T15:30:45Z`<br>`1965-05-15`   |
31//! | Hex Byte Strings    | `h'68656c6c6f'`                                             |
32//! | Base64 Byte Strings | `b64'AQIDBAUGBwgJCg=='`                                     |
33//! | Tagged Values       | `1234("hello")`<br>`5678(3.14)`                             |
34//! | Name-Tagged Values  | `tag-name("hello")`<br>`tag-name(3.14)`                     |
35//! | Known Values        | `'1'`<br>`'isA'`                                            |
36//! | Unit Known Value    | `Unit`<br>`''`<br>`'0'`                                     |
37//! | URs                 | `ur:date/cyisdadmlasgtapttl`                                |
38//! | Arrays              | `[1, 2, 3]`<br>`["hello", "world"]`<br>`[1, [2, 3]]`        |
39//! | Maps                | `{1: 2, 3: 4}`<br>`{"key": "value"}`<br>`{1: [2, 3], 4: 5}` |
40//!
41//! ## Parsing Named Tags and Uniform Resources (URs)
42//!
43//! A [Uniform Resource
44//! (UR)](https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-005-ur.md)
45//! is a URI representation of tagged dCBOR, where the tag is represented as a
46//! text type component. The last component of the UR is the untagged CBOR
47//! encoded as ByteWords, including a CRC-32 checksum in the last eight letters.
48//!
49//! To parse named tags and URs, the correspondence between the tag name (UR
50//! type) and the integer CBOR tag value must be known. This is done by using
51//! the `with_tags!` macro to access the global tags registry. Clients wishing
52//! to parse named tags and URs must register the CBOR tag value and its
53//! corresponding name in the global tags registry. The
54//! [`dcbor`](https://crates.io/crates/dcbor) crate only registers one tag and
55//! name for `date` (tag 1). The [`bc-tags`](https://crates.io/crates/bc-tags)
56//! crate registers many more. See the `register_tags` functions in these crates
57//! for examples of how to register your own tags.
58
59mod parse;
60pub use parse::{parse_dcbor_item, parse_dcbor_item_partial};
61
62mod token;
63pub use token::Token;
64
65mod error;
66pub use error::{Error as ParseError, Result as ParseResult};
67
68mod compose;
69pub use compose::{
70    Error as ComposeError, Result as ComposeResult, compose_dcbor_array,
71    compose_dcbor_map,
72};