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
DataItemvariants. - data_
item - Module containing a data item
- decode
- Module for decoding
CBORbytes TurningCBORbytes 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
CBORbytes. - error
- Module containing different type of error
- index
- Module for index Looking items up inside arrays and maps.
Structs§
- Array
- Content of an array,
CBORmajor type 4. - Byte
String - Content of a byte string,
CBORmajor type 2. - Decoder
- A configurable
CBORdecoder. - Encoder
- A configurable
CBORencoder. - Map
- Content of a map,
CBORmajor type 5. - Simple
- A simple value of
CBORmajor type 7 which has no dedicated variant. - Tag
- Content of a tagged item,
CBORmajor type 6. - Text
String - Content of a UTF-8 text string,
CBORmajor type 3.
Enums§
- Data
Item - Enum representing different types of data item that can be encoded or
decoded in
CBOR(Concise Binary Object Representation). - Deterministic
Mode - Ordering a deterministic encoding applies to the keys of a map.
- Error
- Error returned when a byte sequence cannot be decoded as
CBOR. - Float
Mode - Width used for floating point numbers.
- Length
Mode - 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.