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>
impl<'a> Deserializer<'a>
Sourcepub fn from_str(input: &'a str) -> Deserializer<'a>
pub fn from_str(input: &'a str) -> Deserializer<'a>
Creates a new deserializer for a string.
Sourcepub fn from_str_with_config(
input: &'a str,
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_str_with_config( input: &'a str, config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a string with the given configuration.
Sourcepub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
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.
Sourcepub fn from_slice_with_config(
input: &'a [u8],
config: DeserializerConfig,
) -> Deserializer<'a>
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.
Sourcepub fn config(&self) -> &DeserializerConfig
pub fn config(&self) -> &DeserializerConfig
Returns the configuration.
Sourcepub fn headers(&self) -> Option<&[String]>
pub fn headers(&self) -> Option<&[String]>
Returns the names of the columns.
They are read with the first record.
Sourcepub fn is_end(&self) -> bool
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.
Sourcepub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
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.
Sourcepub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
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.
Sourcepub fn deserialize_record<T: Deserialize<'a>>(
&mut self,
) -> Result<Option<T>, Error>
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());Sourcepub fn deserialize_record_with<T, F>(
&mut self,
setup: F,
) -> Result<Option<T>, Error>
pub fn deserialize_record_with<T, F>( &mut self, setup: F, ) -> Result<Option<T>, Error>
Deserializes the next record with a configured driver.
See deserialize_record.
Sourcepub fn records<T: Deserialize<'a>>(&mut self) -> Records<'_, 'a, T> ⓘ
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).
Sourcepub fn drive_record(
&mut self,
driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<bool, Error>
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.
Sourcepub fn drive(
&mut self,
driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<(), Error>
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).