Skip to main content

Crate cbor_next

Crate cbor_next 

Source
Expand description

Library to handle a Concise Binary Object Representation (CBOR)

Every CBOR value is a DataItem, which is encoded with DataItem::encode and decoded with DataItem::decode.

use cbor_next::{DataItem, Get as _, TextString};

let item = DataItem::map([("name", "cbor"), ("kind", "format")]);
let encoded = item.encode();

let decoded = DataItem::decode(&encoded).unwrap();
assert_eq!(
    decoded
        .get("name")
        .and_then(DataItem::as_text)
        .map(TextString::to_text)
        .unwrap(),
    "cbor"
);

Those two methods use the default settings. Encoder and Decoder do the same work under settings of your choosing, which is how deterministic output is produced and how untrusted input is held to a profile.

use cbor_next::{DataItem, Decoder, DeterministicMode, Encoder};

let item = DataItem::map([(2, "b"), (1, "a")]);
let encoded = Encoder::new()
    .deterministic(DeterministicMode::Core)
    .encode(&item);

let decoder = Decoder::new()
    .max_nesting_depth(16)
    .require_deterministic(DeterministicMode::Core);
assert_eq!(
    decoder.decode(&encoded).unwrap(),
    DataItem::map([(1, "a"), (2, "b")])
);

Modules§

content
Module for the content held by the different data item variants Contents carried by the different DataItem variants.
data_item
Module containing a data item
decode
Module for decoding CBOR bytes Turning CBOR bytes into data items.
deterministic
Module containing different deterministic mode Deterministic, also called canonical, encoding of data items.
encode
Module for encoding data items Turning data items into CBOR bytes.
error
Module containing different type of error
index
Module for index Looking items up inside arrays and maps.

Structs§

Array
Content of an array, CBOR major type 4.
ByteString
Content of a byte string, CBOR major type 2.
Decoder
A configurable CBOR decoder.
Encoder
A configurable CBOR encoder.
Map
Content of a map, CBOR major type 5.
Simple
A simple value of CBOR major type 7 which has no dedicated variant.
Tag
Content of a tagged item, CBOR major type 6.
TextString
Content of a UTF-8 text string, CBOR major type 3.

Enums§

DataItem
Enum representing different types of data item that can be encoded or decoded in CBOR (Concise Binary Object Representation).
DeterministicMode
Ordering a deterministic encoding applies to the keys of a map.
Error
Error returned when a byte sequence cannot be decoded as CBOR.
FloatMode
Width used for floating point numbers.
LengthMode
Length form used for byte strings, text strings, arrays and maps.

Traits§

Get
Trait which is used to get a data item out of a data item.