sk-cbor 0.2.0

CBOR parsing library
Documentation
  • Coverage
  • 68.42%
    52 out of 76 items documented5 out of 51 items with examples
  • Size
  • Source code size: 115.0 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 775.7 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • google/OpenSK
    3467 351 12
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • gendx daviddrysdale kaczmarczyck

CBOR Parsing Library

crates.io crates.io docs.rs License Maintenance

This crate implements Concise Binary Object Representation (CBOR) from RFC 8949.

Usage

fn main() {
    // Build a CBOR object with the crate's convenience macros. Note that this
    // object is not built in canonical order.
    let map_object = cbor_map! {
        1 => cbor_array![2, 3],
        "tstr" => cbor_bytes!(vec![1, 2, 3]),
        -2 => cbor_null!(),
        3 => cbor_true!(),
    };

    println!("Object {:?}", map_object);

    // Serialize to bytes.
    let mut map_data = vec![];
    sk_cbor::writer::write(map_object, &mut map_data).unwrap();
    let hex_map_data = hex::encode(&map_data);

    // Serialized version is in canonical order.
    println!("Serializes to {}", hex_map_data);
    assert_eq!(
        hex_map_data,
        concat!(
            "a4",         // 4-map
            "01",         // int(1) =>
            "820203",     // 2-array [2, 3],
            "03",         // int(3) =>
            "f5",         // true,
            "21",         // nint(-2) =>
            "f6",         // null,
            "6474737472", // 4-tstr "tstr" =>
            "43010203"    // 3-bstr
        )
    );

    // Convert back to an object.  This is different than the original object,
    // because the map is now in canonical order.
    let recovered_object = sk_cbor::reader::read(&map_data).unwrap();
    println!("Deserializes to {:?}", recovered_object);
}