Skip to main content

Deserializer

Struct Deserializer 

Source
pub struct Deserializer<'a> { /* private fields */ }
Expand description

Deserializes types from a Value.

This is a Deserializer which emits the events of a value. It’s what from_value uses, use it directly to configure the deserialization, for instance to add layers:

use std::collections::BTreeMap;
use deser_path::{Path, PathLayer};
use deser_value::{value, Deserializer};

let value = value!({"items": [1, "two"]});
let err = Deserializer::new(&value)
    .deserialize_with::<BTreeMap<String, Vec<u32>>, _>(|driver| {
        driver.push_layer(PathLayer::new());
    })
    .unwrap_err();
assert_eq!(err.attachment::<Path>().unwrap().to_string(), "items[1]");

Strings and bytes are passed on borrowed from the value, which means that types like &str can be deserialized. The meta data of the values is restored for every event: event data is attached and if the value has spans, the input ranges and the source are published. This means that errors refer to the location in the input the value was deserialized from.

Implementations§

Source§

impl<'a> Deserializer<'a>

Source

pub fn new(value: &'a Value) -> Deserializer<'a>

Creates a deserializer for a value.

Source

pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>

Deserializes the value.

Source

pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
where T: Deserialize<'a>, F: FnOnce(&mut DeserializeDriver<'_, 'a>),

Deserializes the value with a configured driver.

The callback is invoked with the driver before the value is deserialized, for instance to add Layers.

Source

pub fn set_context(&mut self, context: Context)

Sets the context the values are deserialized in.

The values of the context are the defaults of the extension values of the state (see Context). A context set on the driver (for instance in the setup callback of deserialize_with) takes precedence.

Source

pub fn context(&self) -> &Context

Returns the context the values are deserialized in.

Trait Implementations§

Source§

impl<'de> Deserializer<'de> for Deserializer<'de>

Source§

fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'de>, ) -> Result<(), Error>

Parses the input and feeds the events of a value into the driver.
Source§

fn deserialize<T>(&mut self) -> Result<T, Error>
where T: Deserialize<'de>, Self: Sized,

Deserializes a value.
Source§

fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Deserializes a value with a configured driver. Read more
Source§

fn update<T>(&mut self, value: &mut T) -> Result<(), Error>
where T: Deserialize<'de>, Self: Sized,

Updates an existing value with the next value. Read more
Source§

fn update_with<T, F>(&mut self, value: &mut T, setup: F) -> Result<(), Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Updates an existing value with the next value after setting up the driver. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Deserializer<'a>

§

impl<'a> !UnwindSafe for Deserializer<'a>

§

impl<'a> Freeze for Deserializer<'a>

§

impl<'a> Send for Deserializer<'a>

§

impl<'a> Sync for Deserializer<'a>

§

impl<'a> Unpin for Deserializer<'a>

§

impl<'a> UnsafeUnpin for Deserializer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.