Skip to main content

DeserializerConfig

Struct DeserializerConfig 

Source
pub struct DeserializerConfig { /* private fields */ }
Expand description

Configures how delimited text is deserialized.

The configuration is independent of the input so it can be created once (even as a constant) and used for many inputs. The methods from_str and from_slice work like the functions of the same name. The default is CSV as described by RFC 4180, with a header, any line ending and blank lines skipped.

use deser_csv::DeserializerConfig;

const SEMICOLONS: DeserializerConfig =
    DeserializerConfig::new().delimiter(b';');
let rows: Vec<(String, u32)> = SEMICOLONS
    .headers(deser_csv::Headers::None)
    .from_str("a;1\nb;2\n")
    .unwrap();
assert_eq!(rows, [("a".into(), 1), ("b".into(), 2)]);

Implementations§

Source§

impl DeserializerConfig

Source

pub const fn new() -> DeserializerConfig

Creates the default configuration (CSV).

Source

pub const fn tsv() -> DeserializerConfig

Creates the configuration for tab separated values.

Fields are separated by tabs and are not quoted. Tabs, line breaks and backslashes in fields are escaped with backslashes (\t, \n, \r and \\) and \N is null (see Escape::Backslash and Nulls::Text). This is how databases (like PostgreSQL’s COPY and MySQL’s LOAD DATA) and many tools write TSV, and it reads the TSV of IANA (which cannot contain tabs and line breaks in fields) as well. For TSV with quotes (as written by spreadsheets) use DeserializerConfig::new().delimiter(b'\t').

use deser_csv::DeserializerConfig;

#[derive(deser::Deserialize)]
struct Row {
    name: String,
    note: Option<String>,
}

let rows: Vec<Row> = DeserializerConfig::tsv()
    .from_str("name\tnote\nJane\ta\\tb\nJohn\t\\N\n")
    .unwrap();
assert_eq!(rows[0].note.as_deref(), Some("a\tb"));
assert_eq!(rows[1].note, None);
Source

pub const fn delimiter(self, delimiter: u8) -> DeserializerConfig

Sets the character that separates fields (, by default).

Special characters (the delimiter, quote, escape and terminator) have to be distinct ASCII characters, otherwise deserializing fails.

Source

pub const fn quote(self, quote: Option<u8>) -> DeserializerConfig

Sets the character that quotes fields (" by default).

Quoted fields can contain the delimiter and line breaks. With None quotes are regular characters.

Source

pub const fn double_quote(self, yes: bool) -> DeserializerConfig

Sets if two quotes in a quoted field are a quote (true by default).

Without doubled quotes, quotes in quoted fields have to be escaped (see escape).

Source

pub const fn escape(self, escape: Escape) -> DeserializerConfig

Sets how characters are escaped (not at all by default).

Source

pub const fn terminator(self, terminator: Terminator) -> DeserializerConfig

Sets what ends records (Terminator::Newline by default).

Source

pub const fn comment(self, comment: Option<u8>) -> DeserializerConfig

Sets the character that starts comment lines (none by default).

Lines that start with it are skipped. The character only starts a comment at the start of a line (a,#b is a regular record).

Source

pub const fn headers(self, headers: Headers) -> DeserializerConfig

Sets where the names of the columns come from (Headers::First by default).

With names, records are maps of the names to the fields. Without names (Headers::None) records are sequences.

Source

pub const fn trim(self, trim: Trim) -> DeserializerConfig

Sets which whitespace is removed (Trim::None by default).

Spaces and tabs are removed from the start and end of unquoted fields and around the quotes of quoted fields (a, "b" ,c).

Source

pub const fn nulls(self, nulls: Nulls) -> DeserializerConfig

Sets which fields are null (Nulls::None by default).

Without nulls, empty fields are None for optionals of types that do not accept the empty string (like Option<u32>) and Some("") for strings. Quoted fields are never null.

Source

pub const fn skip_blank_lines(self, yes: bool) -> DeserializerConfig

Sets if blank lines are skipped (true by default).

Otherwise a blank line is a record with a single empty field. Lines with only whitespace are not blank.

Source

pub const fn flexible(self, yes: bool) -> DeserializerConfig

Sets if records can have a different number of fields (false by default).

By default, records must have as many fields as there are columns (or as the first record has, without names). With flexible records, missing fields are missing in the map and fields without names are keyed with their index ("3"), so they end up in a flattened map or are ignored like unknown fields.

Source

pub const fn lenient_quotes(self, yes: bool) -> DeserializerConfig

Sets if quotes that do not follow the rules are accepted (false by default).

By default, quotes in unquoted fields (5'10") and characters after the closing quote of a field ("a"b) are errors. With lenient quotes the quotes of unquoted fields are regular characters and characters after the closing quote are part of the field (ab).

Source

pub const fn sep_line(self, yes: bool) -> DeserializerConfig

Sets if a sep= line at the start selects the delimiter (false by default).

Excel writes and understands a first line like sep=; which sets the delimiter of the file.

use std::collections::BTreeMap;

let config = deser_csv::DeserializerConfig::new().sep_line(true);
let rows: Vec<BTreeMap<String, u32>> =
    config.from_str("sep=;\na;b\n1;2\n").unwrap();
assert_eq!(rows[0]["b"], 2);
Source

pub const fn max_record_len(self, len: usize) -> DeserializerConfig

Sets the maximum length of a record in a stream in bytes (64 MiB by default).

A record in a stream is buffered until it’s complete. A longer record is an error which ends the stream, which protects from streams that never end a record (like a quoted field that is never closed). Inputs in memory are not limited.

Source

pub const fn bytes(self, format: BytesFormat) -> DeserializerConfig

Sets how fields are decoded into bytes.

Types that expect bytes (like Vec<u8>) decode fields as base64 by default. Fields which are not UTF-8 are passed on as bytes (see bytes).

Source

pub const fn track_locations(self, yes: bool) -> DeserializerConfig

Enables or disables location tracking.

The byte range of every field is always published into the state (see State::input_range). When enabled additionally the input is set as source (see Source). This copies the input.

Source

pub fn from_str<'de, T: Deserialize<'de>>( &self, s: &'de str, ) -> Result<T, Error>

Deserializes the records of a string.

See from_str.

Source

pub fn from_slice<'de, T: Deserialize<'de>>( &self, bytes: &'de [u8], ) -> Result<T, Error>

Deserializes the records of a byte slice.

See from_slice.

Source§

impl DeserializerConfig

Source

pub fn reader<R: Read>(&self, reader: R) -> Reader<R, StreamDeserializer>

Creates a reader of a stream of records (see deser::io::Reader).

Every value is a record, see StreamDeserializer.

Source

pub fn from_reader<T: DeserializeOwned, R: Read>( &self, reader: R, ) -> Result<T, Error>

Deserializes the records of a reader.

See from_reader.

Trait Implementations§

Source§

impl Clone for DeserializerConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DeserializerConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DeserializerConfig

Source§

fn default() -> DeserializerConfig

Returns the “default value” for a type. Read more
Source§

impl Eq for DeserializerConfig

Source§

impl PartialEq for DeserializerConfig

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DeserializerConfig

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.