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
Error
s:
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 primaryType
is different from the expectedType
;Error::UnknownLenType(byte)
: the CBOR is serialized in an unknown or unsupported format;Error::IndefiniteLenUnsupported(t)
: the Indefinite length is not supported for the givenType
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§
source§impl<R> Deserializer<R>
impl<R> Deserializer<R>
source§impl<R: BufRead> Deserializer<R>
impl<R: BufRead> Deserializer<R>
sourcepub fn cbor_type(&mut self) -> Result<Type>
pub fn cbor_type(&mut self) -> Result<Type>
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);
sourcepub fn cbor_len(&mut self) -> Result<(Len, usize)>
pub fn cbor_len(&mut self) -> Result<(Len, usize)>
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 theType
.
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);
sourcepub fn advance(&mut self, len: usize) -> Result<()>
pub fn advance(&mut self, len: usize) -> Result<()>
consume the given len
from the underlying buffer. Skipped bytes are
then lost, they cannot be retrieved for future references.
sourcepub fn unsigned_integer(&mut self) -> Result<u64>
pub fn unsigned_integer(&mut self) -> Result<u64>
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();
sourcepub fn negative_integer(&mut self) -> Result<i64>
pub fn negative_integer(&mut self) -> Result<i64>
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);
sourcepub fn bytes<'a>(&'a mut self) -> Result<Vec<u8>>
pub fn bytes<'a>(&'a mut self) -> Result<Vec<u8>>
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();
sourcepub fn text(&mut self) -> Result<String>
pub fn text(&mut self) -> Result<String>
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");
sourcepub fn array(&mut self) -> Result<Len>
pub fn array(&mut self) -> Result<Len>
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));
sourcepub fn tuple(
&mut self,
expected_len: u64,
error_location: &'static str
) -> Result<()>
pub fn tuple(
&mut self,
expected_len: u64,
error_location: &'static str
) -> Result<()>
Expect an array of a specified length. Must be a definite-length array.
sourcepub fn map(&mut self) -> Result<Len>
pub fn map(&mut self) -> Result<Len>
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));
sourcepub fn tag(&mut self) -> Result<u64>
pub fn tag(&mut self) -> Result<u64>
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());
pub fn set_tag(&mut self) -> Result<()>
pub fn special(&mut self) -> Result<Special>
pub fn bool(&mut self) -> Result<bool>
pub fn deserialize<T>(&mut self) -> Result<T>where
T: Deserialize,
sourcepub fn deserialize_complete<T>(&mut self) -> Result<T>where
T: Deserialize,
pub fn deserialize_complete<T>(&mut self) -> Result<T>where
T: Deserialize,
Deserialize a value of type T
and check that there is no
trailing data.