Struct jomini::TextDeserializer [−][src]
pub struct TextDeserializer;
Expand description
A structure to deserialize text 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::{TextDeserializer, TextTape};
use serde::Deserialize;
#[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 = b"field1=ENG field2=ENH";
// the data can be parsed and deserialized in one step
let a: StructA = TextDeserializer::from_windows1252_slice(&data[..])?;
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 = TextTape::from_slice(&data[..])?;
let b: StructB = TextDeserializer::from_windows1252_tape(&tape)?;
let c: StructC = TextDeserializer::from_windows1252_tape(&tape)?;
assert_eq!(b, StructB { field1: "ENG".to_string() });
assert_eq!(c, StructC { field2: "ENH".to_string() });
Implementations
pub fn from_windows1252_slice<'a, T>(data: &'a [u8]) -> Result<T, Error> where
T: Deserialize<'a>,
pub fn from_windows1252_slice<'a, T>(data: &'a [u8]) -> Result<T, Error> where
T: Deserialize<'a>,
Convenience method for parsing the given text data and deserializing as windows1252 encoded.
pub fn from_windows1252_tape<'a, T>(tape: &TextTape<'a>) -> Result<T, Error> where
T: Deserialize<'a>,
pub fn from_windows1252_tape<'a, T>(tape: &TextTape<'a>) -> Result<T, Error> where
T: Deserialize<'a>,
Deserialize the given text tape assuming quoted strings are windows1252 encoded.
Convenience method for parsing the given text data and deserializing as utf8 encoded.
pub fn from_utf8_tape<'a, 'b, T>(tape: &'b TextTape<'a>) -> Result<T, Error> where
T: Deserialize<'a>,
pub fn from_utf8_tape<'a, 'b, T>(tape: &'b TextTape<'a>) -> Result<T, Error> where
T: Deserialize<'a>,
Deserialize the given text tape assuming quoted strings are utf8 encoded.
pub fn from_encoded_tape<'b, 'a: 'b, T, E>(
tape: &'b TextTape<'a>,
encoding: E
) -> Result<T, Error> where
T: Deserialize<'a>,
E: Encoding + Clone,
pub fn from_encoded_tape<'b, 'a: 'b, T, E>(
tape: &'b TextTape<'a>,
encoding: E
) -> Result<T, Error> where
T: Deserialize<'a>,
E: Encoding + Clone,
Deserialize the given text tape assuming quoted strings can be decoded according to the given encoder