Struct cbor_event::de::Deserializer

source ·
pub struct Deserializer<R>(_);
Expand description

Deserializer represents a chunk of bytes believed to be cbor object. The validity of the cbor bytes is known only when trying to get meaningful cbor objects from it.

Examples

If you already know the CBOR Primary Type you are expecting, you can efficiently use the appropriate commands:

use cbor_event::de::*;
use std::io::Cursor;

let vec = vec![0x18, 0x40];
let mut raw = Deserializer::from(Cursor::new(vec));

assert!(raw.unsigned_integer().is_ok());
use cbor_event::de::*;
use std::io::Cursor;

let vec = vec![0x18, 0x40];
let mut raw = Deserializer::from(Cursor::new(vec));

assert!(raw.array().is_err());

If you don’t know the Type and are only analyzing the structure, you can use cbor_type to get the type of the next object to parse.

Error

When deserialising from Deserializer it is possible to see the following Errors:

  • Error::NotEnough(current_size, needed_size): meaning we are expecting a more bytes to parse the CBOR properly;
  • Error::Expected(expected_type, current_type): the current cbor primary Type is different from the expected Type;
  • Error::UnknownLenType(byte): the CBOR is serialized in an unknown or unsupported format;
  • Error::IndefiniteLenUnsupported(t): the Indefinite length is not supported for the given Type t;
  • Error::IoError(io_error): error due relating to buffer management;

Panic

There is no explicit panic! in this code, except a few unreachable!.

Implementations§

function to extract the type of the given Deserializer.

This function does not consume the underlying buffer.

Examples
use cbor_event::{de::*, Type};
use std::io::Cursor;

let vec = vec![0x18, 0x40];
let mut raw = Deserializer::from(Cursor::new(vec));
let cbor_type = raw.cbor_type().unwrap();

assert!(cbor_type == Type::UnsignedInteger);

function to extract the get the length parameter of the given cbor object. The returned tuple contains

  • the Len;
  • the size of the encoded length (the number of bytes the data was encoded in). 0 means the length is < 24 and was encoded along the Type.

If you are expecting a Type UnsignedInteger or NegativeInteger the meaning of the length is slightly different:

  • Len::Indefinite is an error;
  • Len::Len(len) is the read value of the integer.

This function does not consume the underlying buffer.

Examples
use cbor_event::{de::*, Len};
use std::io::Cursor;

let vec = vec![0x83, 0x01, 0x02, 0x03];
let mut raw = Deserializer::from(Cursor::new(vec));
let (len, len_sz) = raw.cbor_len().unwrap();

assert_eq!(len, Len::Len(3));
assert_eq!(len_sz, 0);

consume the given len from the underlying buffer. Skipped bytes are then lost, they cannot be retrieved for future references.

Read an UnsignedInteger from the Deserializer

The function fails if the type of the given Deserializer is not Type::UnsignedInteger.

Example
use cbor_event::de::{*};
use std::io::Cursor;

let vec = vec![0x18, 0x40];
let mut raw = Deserializer::from(Cursor::new(vec));

let integer = raw.unsigned_integer().unwrap();

assert_eq!(integer, 64);
use cbor_event::de::{*};
use std::io::Cursor;

let vec = vec![0x83, 0x01, 0x02, 0x03];
let mut raw = Deserializer::from(Cursor::new(vec));

// the following line will panic:
let integer = raw.unsigned_integer().unwrap();

Read a NegativeInteger from the Deserializer

The function fails if the type of the given Deserializer is not Type::NegativeInteger.

Example
use cbor_event::de::{*};
use std::io::Cursor;

let vec = vec![0x38, 0x29];
let mut raw = Deserializer::from(Cursor::new(vec));

let integer = raw.negative_integer().unwrap();

assert_eq!(integer, -42);

Read a Bytes from the Deserializer

The function fails if the type of the given Deserializer is not Type::Bytes.

Example
use cbor_event::de::{*};
use std::io::Cursor;

let vec = vec![0x52, 0x73, 0x6F, 0x6D, 0x65, 0x20, 0x72, 0x61, 0x6E, 0x64, 0x6F, 0x6D, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67];
let mut raw = Deserializer::from(Cursor::new(vec));

let bytes = raw.bytes().unwrap();

Read a Text from the Deserializer

The function fails if the type of the given Deserializer is not Type::Text.

Example
use cbor_event::de::{*};
use std::io::Cursor;

let vec = vec![0x64, 0x74, 0x65, 0x78, 0x74];
let mut raw = Deserializer::from(Cursor::new(vec));

let text = raw.text().unwrap();

assert!(&*text == "text");

cbor array of cbor objects

The function fails if the type of the given Deserializer is not Type::Array.

Example
use cbor_event::{de::{*}, Len};
use std::io::Cursor;

let vec = vec![0x86, 0,1,2,3,4,5];
let mut raw = Deserializer::from(Cursor::new(vec));

let len = raw.array().unwrap();

assert_eq!(len, Len::Len(6));

Expect an array of a specified length. Must be a definite-length array.

cbor map

The function fails if the type of the given Deserializer is not Type::Map.

Example
use cbor_event::{de::{*}, Len};
use std::io::Cursor;

let vec = vec![0xA2, 0x00, 0x64, 0x74, 0x65, 0x78, 0x74, 0x01, 0x18, 0x2A];
let mut raw = Deserializer::from(Cursor::new(vec));

let len = raw.map().unwrap();

assert_eq!(len, Len::Len(2));

Cbor Tag

The function fails if the type of the given Deserializer is not Type::Tag.

Example
use std::io::Cursor;
use cbor_event::{de::{*}, Len};

let vec = vec![0xD8, 0x18, 0x64, 0x74, 0x65, 0x78, 0x74];
let mut raw = Deserializer::from(Cursor::new(vec));

let tag = raw.tag().unwrap();

assert_eq!(24, tag);
assert_eq!("text", &*raw.text().unwrap());

Deserialize a value of type T and check that there is no trailing data.

Trait Implementations§

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.