Crate dcbor

source ·
Expand description

dCBOR: Deterministic CBOR Codec

dcbor is a CBOR codec that focuses on writing and parsing “deterministic” CBOR per §4.2 of RFC-8949. It does not support parts of the spec forbidden by deterministic CBOR (such as indefinite length arrays and maps). It is strict in both what it writes and reads: in particular it will return decoding errors if variable-length integers are not encoded in their minimal form, or CBOR map keys are not in lexicographic order, or there is extra data past the end of the decoded CBOR item.

Getting Started

Add the following to your Cargo.toml:

[dependencies]
dcbor = "0.11.0"

Specification

The current specification of the norms and practices guiding the creation of this implementation are currently found in this IETF Internet Draft: draft-mcnally-deterministic-cbor.

Usage

Encode an array of integers as CBOR.

use dcbor::prelude::*;
let array = [1000, 2000, 3000];
let cbor = array.cbor();
assert_eq!(cbor.hex(), "831903e81907d0190bb8");

Decode CBOR binary back to an array of integers.

use dcbor::prelude::*;
let data = hex_literal::hex!("831903e81907d0190bb8");
let cbor = CBOR::from_data(&data).unwrap();
assert_eq!(cbor.diagnostic(), "[1000, 2000, 3000]");
let array: Vec::<u32> = cbor.try_into().unwrap();
assert_eq!(format!("{:?}", array), "[1000, 2000, 3000]");

See the unit tests For further examples, including encoding and decoding arrays with heterogenous elements, maps, and user-defined types with custom CBOR tags.

Modules

Structs

  • A symbolic representation of CBOR data.
  • A CBOR-friendly representation of a date and time.
  • A CBOR map.
  • An iterator over the entries of a CBOR map.
  • A CBOR tag.
  • A dictionary of mappings between tags and their names.

Enums

Traits