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'sSerializeandDeserializetraits but do not abstract over the encoder/decoder.
As mentioned, 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.
Optionally, Encode and Decode can be derived for structs and enums
using the respective derive macros. See [minicbor_derive] for details.
Example: generic encoding and decoding
use ;
let input = ;
let mut buffer = ;
encode?;
let output: = decode?;
assert_eq!;
# Ok::
Example: ad-hoc encoding
use Encoder;
let mut buffer = ;
let mut encoder = new;
encoder.begin_map? // using an indefinite map here
.str?.str?
.str?.map?
.u8?.bool?
.u8?.bool?
.u16?.array?.u8?.u8?.u8?
.bool?.null?
.end?;
# Ok::
Example: ad-hoc decoding
use ;
let input = ;
let mut decoder = new;
assert_eq!;
assert_eq!;
# Ok::