Skip to main content

Crate cbor_core

Crate cbor_core 

Source
Expand description

Deterministic CBOR encoder and decoder following the CBOR::Core profile (draft-rundgren-cbor-core-25).

This crate works with CBOR as an owned data structure rather than as a serialization layer. Values can be constructed, inspected, modified, and round-tripped through their canonical byte encoding.

§Types

Value is the central type and a good starting point. It represents any CBOR data item and provides constructors, accessors, encoding, and decoding.

TypeRole
ValueAny CBOR data item. Start here.
SimpleValueCBOR simple value (null, true, false, 0-255).
DataTypeClassification of a value for type-level dispatch.
ErrorAll errors produced by this crate.

The following types are helpers that appear in From/Into bounds and are rarely used directly:

TypeRole
ArrayWrapper around Vec<Value> for flexible array construction.
MapWrapper around BTreeMap<Value, Value> for flexible map construction.
FloatIEEE 754 float stored in shortest CBOR form (f16, f32, or f64).
DateTimeValidated ISO 8601 UTC string for tag 0 construction.
EpochTimeValidated numeric epoch time for tag 1 construction.

§Quick start

use cbor_core::{Value, array, map};

// Build a value
let value = map! {
    1 => "hello",
    2 => array![10, 20, 30],
};

// Encode to bytes and decode back
let bytes = value.encode();
let decoded = Value::decode(&bytes).unwrap();
assert_eq!(value, decoded);

// Access inner data
let greeting = decoded[1].as_str().unwrap();
assert_eq!(greeting, "hello");

§Encoding rules

All encoding is deterministic: integers and floats use their shortest representation, and map keys are sorted in CBOR canonical order. The decoder rejects non-canonical input.

NaN values, including signaling NaNs and custom payloads, are preserved through encode/decode round-trips. Conversion between float widths uses bit-level manipulation to avoid hardware NaN canonicalization.

Macros§

array
Construct a CBOR array from a list of expressions.
map
Construct a CBOR map from a list of key => value pairs.

Structs§

Array
Helper for flexible array construction.
DateTime
Helper for validated date/time string construction.
EpochTime
Helper for validated epoch time construction.
Float
A floating-point value stored in its shortest CBOR encoding form.
Map
Helper for flexible map construction.
SimpleValue
A CBOR simple value (major type 7, values 0-23 and 32-255).

Enums§

DataType
Classification of a Value for lightweight type checks.
Error
Errors produced during CBOR encoding, decoding, or value access.
Value
A single CBOR data item.

Type Aliases§

Result
Convenience alias used throughout this crate.