#cbor #serialization #data-encoding

no-std bin+lib minicbor

A small CBOR codec suitable for no_std environments

53 releases (24 breaking)

0.25.1 Sep 12, 2024
0.24.4 Sep 4, 2024
0.24.2 Jun 28, 2024
0.21.1 Mar 28, 2024
0.1.1 Mar 29, 2020

#60 in Encoding

Download history 17170/week @ 2024-07-18 15637/week @ 2024-07-25 18750/week @ 2024-08-01 20660/week @ 2024-08-08 22398/week @ 2024-08-15 23335/week @ 2024-08-22 24173/week @ 2024-08-29 24421/week @ 2024-09-05 25144/week @ 2024-09-12 20020/week @ 2024-09-19 24047/week @ 2024-09-26 23389/week @ 2024-10-03 23915/week @ 2024-10-10 26555/week @ 2024-10-17 29952/week @ 2024-10-24 29368/week @ 2024-10-31

114,121 downloads per month
Used in 255 crates (79 directly)

BlueOak-1.0.0

195KB
4.5K SLoC

minicbor

A small CBOR codec suitable for no_std environments.

Documentation

Documentation is available at https://docs.rs/minicbor/

License

This software is licensed under the Blue Oak Model License Version 1.0.0. If you are interested in contributing to this project, please read the file CONTRIBUTING.md first.


lib.rs:

A small CBOR codec suitable for no_std environments.

The crate is organised around the following entities:

  • Encoder and Decoder for type-directed encoding and decoding of values.

  • Encode and Decode traits which can be implemented for any type that should be encoded to or decoded from CBOR. They are similar to serde's Serialize and Deserialize traits but do not abstract over the encoder/decoder.

Encoding and decoding proceeds in a type-directed way, i.e. by calling methods for expected data item types, e.g. Decoder::u32 or Encoder::str. In addition there is support for data type inspection. The Decoder can be queried for the current data type which returns a data::Type that can represent every possible CBOR type and decoding can thus proceed based on this information. It is also possible to just tokenize the input bytes using a Tokenizer, i.e. an Iterator over CBOR Tokens. Finally, the length in bytes of a value's CBOR representation can be calculated if the value's type implements the CborLen trait.

Optionally, Encode and Decode can be derived for structs and enums using the respective derive macros (requires feature "derive"). See minicbor_derive for details.

For I/O support see minicbor-io.

Support for serde is available in minicbor-serde.

Feature flags

The following feature flags are supported:

  • "alloc": Enables most collection types in a no_std environment.

  • "std": Implies "alloc" and enables more functionality that depends on the std crate.

  • "derive": Allows deriving Encode and Decode traits.

Example: generic encoding and decoding

use minicbor::{Encode, Decode};

let input = ["hello", "world"];
let mut buffer = [0u8; 128];

minicbor::encode(&input, buffer.as_mut())?;
let output: [&str; 2] = minicbor::decode(buffer.as_ref())?;
assert_eq!(input, output);

Example: ad-hoc encoding

use minicbor::Encoder;

let mut buffer = [0u8; 128];
let mut encoder = Encoder::new(&mut buffer[..]);

encoder.begin_map()? // using an indefinite map here
    .str("hello")?.str("world")?
    .str("submap")?.map(2)?
        .u8(1)?.bool(true)?
        .u8(2)?.bool(false)?
    .u16(34234)?.array(3)?.u8(1)?.u8(2)?.u8(3)?
    .bool(true)?.null()?
.end()?;

Example: ad-hoc decoding

use minicbor::Decoder;
use minicbor::data::IanaTag;

let input = [
    0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30,
    0x33, 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a,
    0x30, 0x34, 0x3a, 0x30, 0x30, 0x5a
];

let mut decoder = Decoder::new(&input);
assert_eq!(IanaTag::DateTime.tag(), decoder.tag()?);
assert_eq!("2013-03-21T20:04:00Z", decoder.str()?);

Example: tokenization

use minicbor::display;
use minicbor::{Encoder, Decoder};
use minicbor::data::Token;

let input  = [0x83, 0x01, 0x9f, 0x02, 0x03, 0xff, 0x82, 0x04, 0x05];

assert_eq!("[1, [_ 2, 3], [4, 5]]", format!("{}", display(&input)));

let tokens = Decoder::new(&input).tokens().collect::<Result<Vec<Token>, _>>()?;

assert_eq! { &tokens[..],
    &[Token::Array(3),
      Token::U8(1),
      Token::BeginArray,
      Token::U8(2),
      Token::U8(3),
      Token::Break,
      Token::Array(2),
      Token::U8(4),
      Token::U8(5)]
};

let mut buffer = [0u8; 9];
Encoder::new(buffer.as_mut()).tokens(&tokens)?;

assert_eq!(input, buffer);

Dependencies

~170KB