Struct jomini::BinaryDeserializer[][src]

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::eu4_builder().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_eu4(&data[..])?;
let deserializer = BinaryDeserializer::eu4_builder();
let b: StructB = deserializer.from_tape(&tape, &map)?;
let c: StructC = deserializer.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 eu4_builder() -> BinaryDeserializerBuilder<Eu4Flavor>[src]

Create a builder to custom binary deserialization

pub fn ck3_builder() -> BinaryDeserializerBuilder<Ck3Flavor>[src]

Create a builder to custom binary deserialization

pub fn builder_flavor<F>(flavor: F) -> BinaryDeserializerBuilder<F> where
    F: BinaryFlavor
[src]

A customized builder for a certain flavor of binary data

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

Convenience method for parsing and deserializing binary data in a single step

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

Convenience method for parsing and deserializing binary data in a single step

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.