Expand description
CBOR (RFC 7049) decoder implementation supporting the following features:
- Generic decoding using an intermediate representation
- Tag validation
- Resource limits (e.g. maximum nesting level)
- Direct decoding into Rust types
- Indefinite sized bytes, strings, arrays and objects
The module is structured as follows:
Kernelcontains the basic decoding functionality, capable of decoding simple unstructured types.Decoderdirectly decodes into native Rust types.GenericDecoderhandles arbitrary CBOR items and decodes them into anValueAST.
§Example 1: Direct decoding
use cbor::{Config, Decoder};
use std::io::Cursor;
let mut d = Decoder::new(Config::default(), Cursor::new(vec![0u8]));
assert_eq!(Some(0), d.u64().ok())§Example 2: Direct decoding (nested array)
extern crate cbor;
extern crate rustc_serialize;
use cbor::{Config, Decoder};
use rustc_serialize::hex::FromHex;
use std::io::Cursor;
fn main() {
let input = Cursor::new("828301020383010203".from_hex().unwrap());
let mut dec = Decoder::new(Config::default(), input);
let mut res = Vec::new();
for _ in 0 .. dec.array().unwrap() {
let mut vec = Vec::new();
for _ in 0 .. dec.array().unwrap() {
vec.push(dec.u8().unwrap())
}
res.push(vec)
}
assert_eq!(vec![vec![1, 2, 3], vec![1, 2, 3]], res)
}§Example 3: Generic decoding
extern crate cbor;
extern crate rustc_serialize;
use cbor::{Config, GenericDecoder};
use cbor::value::{self, Key};
use rustc_serialize::hex::FromHex;
use std::io::Cursor;
fn main() {
let input = Cursor::new("a2616101028103".from_hex().unwrap());
let mut d = GenericDecoder::new(Config::default(), input);
let value = d.value().unwrap();
let c = value::Cursor::new(&value);
assert_eq!(Some(1), c.field("a").u8());
assert_eq!(Some(3), c.get(Key::u64(2)).at(0).u8())
}§Example 4: Direct decoding (optional value)
use cbor::{opt, Config, Decoder};
use std::io::Cursor;
let mut d = Decoder::new(Config::default(), Cursor::new(vec![0xF6]));
assert_eq!(None, opt(d.u8()).unwrap())Structs§
- Bytes
Iter - Iterator over the chunks of an indefinite bytes item.
- Config
Configcontains various settings which limit resource consumption or enable certain validation options. Please note that the various maximum length/size values apply to an individual element only.- Decoder
- The actual decoder type definition
- Generic
Decoder - A generic decoder decodes arbitrary CBOR into a
ValueAST. - Kernel
- This decoding kernel reads from an underlying
std::io::Readtype primitive CBOR values such as unsigned and signed integers as well as raw bytes. It forms the basis on whichDecoderandGenericDecoderadd logic for handlingTags, heterogenous data and generic value decoding. - Text
Iter - Iterator over the chunks of an indefinite text item.
Enums§
Functions§
- maybe
- When decoding an item which may be
Undefinedthis function will mapUndefinedtoNoneand any other value toSome(value). - opt
- When decoding an optional item, i.e. a
Nullvalue has to be considered, this function will mapNulltoNoneand any other value toSome(value). - or_
break - When decoding an indefinite item, every element item can be a
Breakvalue. By wrapping it inor_break, this case can be handled more conveniently.