/cborg

Primary LanguageRustMIT LicenseMIT

CBORG

A CBOR parser for Rust.

Incomplete and built for my own use-case. You may want serde_cbor

Usage

decode_to() will decode CBOR and unmarshal it into a given object:

// Unmarshal Map
use std::collections::HashMap;
let bytes = &[0b1010_0010, 0b0011_1000, 0b0001_1000, 0b0110_0011, 0x61, 0x62, 0x63,
              0b0000_0111, 0b0110_0011, 0x44, 0x45, 0x46];
let map: HashMap<i8, String> = cborg::decode_to(bytes).unwrap().unwrap();
assert_eq!("abc", map[&-25]);
assert_eq!("DEF", map[&7]);
// Unmarshal Array
let bytes = &[0b1000_0011, 11, 22, 0b0001_1000, 33];
let array: Vec<u32> = cborg::decode_to(bytes).unwrap().unwrap();
assert_eq!(11, array[0]);
assert_eq!(22, array[1]);
assert_eq!(33, array[2]);

encode() will encode any object that has From implemented for cborg::Value:

use std::collections::HashMap;
let map: HashMap<u32, &str> = [
   (33, "thirty-three"),
   (44, "fourty-four"),
	(55, "fifty-five")
].iter().cloned().collect();
let cbor_bytes: Vec<u8> = cborg::encode(map);

pipeline status