Skip to main content

Deserializer

Struct Deserializer 

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

Deserializes INI files.

Most of the time the from_str and from_slice functions (or the methods of the same name on DeserializerConfig) are all that is needed. The deserializer is useful to configure the driver, for instance to add layers, or to update a value (see update):

use deser_path::{Path, PathLayer};
use deser_ini::Deserializer;

#[derive(Debug, deser::Deserialize)]
struct Config {
    server: Server,
}

#[derive(Debug, deser::Deserialize)]
struct Server {
    port: u16,
}

let err = Deserializer::from_str("[server]\nport = http\n")
    .deserialize_with::<Config, _>(|driver| {
        driver.push_layer(PathLayer::new())
    })
    .unwrap_err();
assert_eq!(err.message(), "invalid value \"http\", expected u16");
assert_eq!(err.attachment::<Path>().unwrap().to_string(), "server.port");
assert_eq!(err.line(), Some(2));

Implementations§

Source§

impl<'a> Deserializer<'a>

Source

pub fn from_str(input: &'a str) -> Deserializer<'a>

Creates a new deserializer for a string.

Source

pub fn from_str_with_config( input: &'a str, config: DeserializerConfig, ) -> Deserializer<'a>

Creates a new deserializer for a string with the given configuration.

Source

pub fn from_slice(input: &'a [u8]) -> Deserializer<'a>

Creates a new deserializer for a byte slice.

The input must be UTF-8 (a byte order mark is skipped), otherwise deserializing fails.

Source

pub fn from_slice_with_config( input: &'a [u8], config: DeserializerConfig, ) -> Deserializer<'a>

Creates a new deserializer for a byte slice with the given configuration.

Source

pub fn config(&self) -> &DeserializerConfig

Returns the configuration.

Source

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

Deserializes the input.

To configure the deserialization (for instance to add layers) use deserialize_with.

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 input with a configured driver.

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

Source

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

Parses the input and feeds the events into the given driver.

The whole input is parsed before the first event is emitted, so malformed input is reported before any value is deserialized. Keys and values that are not changed (by quotes, escapes, continuation lines or lowercasing) are passed on borrowed from the input (see emit_borrowed).

The context of the configuration is given to the driver (values that the context of the driver has take precedence, see DeserializeDriver::set_default_context).

Trait Implementations§

Source§

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

Source§

fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> 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.