Cbor-data: a library for handling dynamically shaped data

While creating a small query language with expression evaluation I felt the need for a compact yet flexible in-memory representation of dynamically shaped values ā€” without littering the heap with all tiny details. This resulted in the cbor-data crate, which is a faithful implementation of RFC8949, in particular the extended generic data model as described in the document (but not including specific support for the other tags registered with IANA).

Specific focus has been placed on interpreting and referencing the underlying CBOR bytes wherever possible ā€” if the CBOR item is encoded without using indefinite-length byte strings, then no allocations will be made.

The library is geared towards working with small values as well, using a SmallVec to hold owned bytes on the stack for many primitive value types.

Two traits are provided for writing data: Writer gives you access to the low-level encoding (i.e. CBOR major types and tags) while Encoder lets you write high-level Rust types without worrying how to correctly represent them ā€” here some things are still missing, like writing timestamp or bignums.

Example:

use cbor_data::{CborBuilder, index_str, Encoder, value::Number};

let cbor = CborBuilder::new().encode_array(|builder| {
    builder.encode_u64(42);
});

let item = cbor.index(index_str("[0]")).unwrap();
assert_eq!(item.decode().to_number().unwrap(), Number::Int(42));

Iā€™m looking forward to any kind of feedback on the approach or applicability!

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.