Skip to main content

Deserializer

Struct Deserializer 

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

Deserializes delimited text.

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: they deserialize all records as a sequence. The deserializer can also read one record at a time (see records) and configure the driver, for instance to add layers:

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

#[derive(Debug, deser::Deserialize)]
struct Row {
    name: String,
    age: u32,
}

let err = Deserializer::from_str("name,age\njane,42\njohn,x\n")
    .deserialize_with::<Vec<Row>, _>(|driver| {
        driver.push_layer(PathLayer::new())
    })
    .unwrap_err();
assert_eq!(err.message(), "invalid value \"x\", expected u32");
assert_eq!(err.attachment::<Path>().unwrap().to_string(), "[1].age");
assert_eq!((err.line(), err.column()), (Some(3), Some(6)));

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.

Fields which are not UTF-8 are passed on as bytes.

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 headers(&self) -> Option<&[String]>

Returns the names of the columns.

They are read with the first record.

Source

pub fn is_end(&self) -> bool

Returns true if there are no more records.

This is also the case after an error that the input cannot recover from. Blank lines and comments are not records, but they are only skipped when the next record is read.

Source

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

Deserializes all records as a sequence.

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 all records as a sequence with a configured driver.

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

Source

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

Deserializes the next record.

Returns None if there are no more records. Errors of a record (like a field that does not fit the type) only discard the record, the next call continues with the next record.

#[derive(deser::Deserialize)]
struct Row {
    name: String,
    age: u32,
}

let mut de = deser_csv::Deserializer::from_str(
    "name,age\njane,42\njohn,x\nmax,7\n",
);
assert_eq!(de.deserialize_record::<Row>().unwrap().unwrap().age, 42);
assert!(de.deserialize_record::<Row>().is_err());
assert_eq!(de.deserialize_record::<Row>().unwrap().unwrap().age, 7);
assert!(de.deserialize_record::<Row>().unwrap().is_none());
Source

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

Deserializes the next record with a configured driver.

See deserialize_record.

Source

pub fn records<T: Deserialize<'a>>(&mut self) -> Records<'_, 'a, T> ⓘ

Returns an iterator over the remaining records.

Errors of records are returned and the iteration continues with the next record (see deserialize_record).

Source

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

Feeds the events of the next record into the given driver.

Returns false if there are no more records.

Source

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

Parses the input and feeds all records as a sequence into the given driver.

Fields that do not need to be decoded 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> Freeze for Deserializer<'a>

§

impl<'a> RefUnwindSafe 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>

§

impl<'a> UnwindSafe 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.