[][src]Struct jomini::BinaryDeserializer

pub struct BinaryDeserializer;

A structure to deserialize binary data into Rust values.

By default, if a token is unable to be resolved then it will be ignored by the default. Construct a custom instance through the builder method to tweak this behavior.

The example below demonstrates multiple ways to deserialize data

use jomini::{BinaryDeserializer, BinaryTape};
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct StructA {
  #[serde(flatten)]
  b: StructB,

  #[serde(flatten)]
  c: StructC,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct StructB {
  field1: String,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct StructC {
  field2: String,
}

let data = [
   0x82, 0x2d, 0x01, 0x00, 0x0f, 0x00, 0x03, 0x00, 0x45, 0x4e, 0x47,
   0x83, 0x2d, 0x01, 0x00, 0x0f, 0x00, 0x03, 0x00, 0x45, 0x4e, 0x48,
];

let mut map = HashMap::new();
map.insert(0x2d82, String::from("field1"));
map.insert(0x2d83, String::from("field2"));

// the data can be parsed and deserialized in one step
let a: StructA = BinaryDeserializer::from_slice(&data[..], &map)?;
assert_eq!(a, StructA {
  b: StructB { field1: "ENG".to_string() },
  c: StructC { field2: "ENH".to_string() },
});

// or split into two steps, whatever is appropriate.
let tape = BinaryTape::from_slice(&data[..])?;
let b: StructB = BinaryDeserializer::from_tape(&tape, &map)?;
let c: StructC = BinaryDeserializer::from_tape(&tape, &map)?;
assert_eq!(b, StructB { field1: "ENG".to_string() });
assert_eq!(c, StructC { field2: "ENH".to_string() });

Implementations

impl BinaryDeserializer[src]

pub fn builder() -> BinaryDeserializerBuilder[src]

Create a builder to custom binary deserialization

pub fn from_slice<'de, 'res: 'de, RES, T>(
    data: &'de [u8],
    resolver: &'res RES
) -> Result<T, Error> where
    T: Deserialize<'de>,
    RES: TokenResolver
[src]

Deserialize a structure from the given binary data. Convenience method that combines parsing and deserialization in one step

pub fn from_tape<'de, 'res: 'de, RES, T>(
    tape: &BinaryTape<'de>,
    resolver: &'res RES
) -> Result<T, Error> where
    T: Deserialize<'de>,
    RES: TokenResolver
[src]

Deserialize a structure from the already parsed binary data. Useful for when one needs to deserialize a single set of data into more than one structure.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.