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.15.1"

§Features

§Multi-threaded

The multithreaded feature is available but not enabled by default. It uses Arc for reference counting instead of Rc. To enable it, add the following to your Cargo.toml:

[dependencies.dcbor]
version = "0.15.1"
features = ["multithreaded"]

§no_std

The dcbor library is no_std compatible. To use it in a no_std environment, disable the default features in your Cargo.toml and enable the no_std feature:

[dependencies.dcbor]
version = "0.15.1"
default-features = false
features = ["no_std"]

§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: CBOR = array.into();
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::try_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§

  • Utilities for comparing and ordering values.
  • Utilities for formatting and printing Strings.
  • Generic hashing support.
  • Overloadable operators.
  • Single-threaded reference-counting pointers. ‘Rc’ stands for ‘Reference Counted’.
  • Utilities for the str primitive type.
  • Useful synchronization primitives.

Macros§

  • Creates a String using interpolation of runtime expressions.

Structs§

  • An ordered map based on a B-Tree.
  • An iterator over the values of a BTreeMap.
  • A pointer type that uniquely owns a heap allocation of type T.
  • A symbolic representation of CBOR data.
  • A CBOR-friendly representation of a date and time.
  • A Duration type to represent a span of time, typically used for system timeouts.
  • A hash map implemented with quadratic probing and SIMD lookup.
  • A hash set implemented as a HashMap where the value is ().
  • A CBOR map.
  • An iterator over the entries of a CBOR map.
  • A UTF-8–encoded, growable string.
  • A CBOR tag.
  • A dictionary of mappings between tags and their names.
  • The error type returned when a conversion from a slice to an array fails.
  • A contiguous growable array type, written as Vec<T>, short for ‘vector’.
  • A double-ended queue implemented with a growable ring buffer.

Enums§

Traits§

Derive Macros§