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::builder()
.delimiter(b';')
.headers(deser_csv::Headers::None)
.build();
let rows: Vec<(String, u32)> = SEMICOLONS.from_str("a;1\nb;2\n").unwrap();
assert_eq!(rows, [("a".into(), 1), ("b".into(), 2)]);Implementations§
Source§impl DeserializerConfig
impl DeserializerConfig
Sourcepub const fn new() -> DeserializerConfig
pub const fn new() -> DeserializerConfig
Creates the default configuration (CSV).
Sourcepub const fn builder() -> DeserializerConfigBuilder
pub const fn builder() -> DeserializerConfigBuilder
Returns a builder for the configuration (see DeserializerConfigBuilder).
Sourcepub const fn into_builder(self) -> DeserializerConfigBuilder
pub const fn into_builder(self) -> DeserializerConfigBuilder
Returns a builder that starts with this configuration.
Sourcepub fn set_context(&mut self, context: Context)
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), for instance
the variants of open enums. The deserializers and readers created
with the configuration use this context. A context set
on the driver takes precedence.
Sourcepub const fn tsv() -> DeserializerConfig
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::builder().delimiter(b'\t').build().
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);Sourcepub const fn set_delimiter(&mut self, delimiter: u8)
pub const fn set_delimiter(&mut self, delimiter: u8)
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.
Sourcepub const fn set_quote(&mut self, quote: Option<u8>)
pub const fn set_quote(&mut self, quote: Option<u8>)
Sets the character that quotes fields (" by default).
Quoted fields can contain the delimiter and line breaks. With
None quotes are regular characters.
Sourcepub const fn set_double_quote(&mut self, yes: bool)
pub const fn set_double_quote(&mut self, yes: bool)
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 set_escape).
Sourcepub const fn set_escape(&mut self, escape: Escape)
pub const fn set_escape(&mut self, escape: Escape)
Sets how characters are escaped (not at all by default).
Sourcepub const fn set_terminator(&mut self, terminator: Terminator)
pub const fn set_terminator(&mut self, terminator: Terminator)
Sets what ends records (Terminator::Newline by default).
Sourcepub const fn set_comment(&mut self, comment: Option<u8>)
pub const fn set_comment(&mut self, comment: Option<u8>)
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).
Sourcepub const fn set_headers(&mut self, headers: Headers)
pub const fn set_headers(&mut self, headers: Headers)
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.
Sourcepub const fn set_trim(&mut self, trim: Trim)
pub const fn set_trim(&mut self, trim: Trim)
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).
Sourcepub const fn set_nulls(&mut self, nulls: Nulls)
pub const fn set_nulls(&mut self, nulls: Nulls)
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.
Sourcepub const fn set_skip_blank_lines(&mut self, yes: bool)
pub const fn set_skip_blank_lines(&mut self, yes: bool)
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.
Sourcepub const fn set_flexible(&mut self, yes: bool)
pub const fn set_flexible(&mut self, yes: bool)
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.
Sourcepub const fn set_lenient_quotes(&mut self, yes: bool)
pub const fn set_lenient_quotes(&mut self, yes: bool)
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).
Sourcepub const fn set_sep_line(&mut self, yes: bool)
pub const fn set_sep_line(&mut self, yes: bool)
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::builder().sep_line(true).build();
let rows: Vec<BTreeMap<String, u32>> =
config.from_str("sep=;\na;b\n1;2\n").unwrap();
assert_eq!(rows[0]["b"], 2);Sourcepub const fn set_max_record_len(&mut self, len: usize)
pub const fn set_max_record_len(&mut self, len: usize)
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.
Sourcepub fn from_str<'de, T: Deserialize<'de>>(
&self,
s: &'de str,
) -> Result<T, Error>
pub fn from_str<'de, T: Deserialize<'de>>( &self, s: &'de str, ) -> Result<T, Error>
Deserializes the records of a string.
See from_str.
Sourcepub fn from_slice<'de, T: Deserialize<'de>>(
&self,
bytes: &'de [u8],
) -> Result<T, Error>
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
impl DeserializerConfig
Sourcepub fn reader<R: Read>(&self, reader: R) -> Reader<R, StreamDeserializer>
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.
Sourcepub fn from_reader<T: DeserializeOwned, R: Read>(
&self,
reader: R,
) -> Result<T, Error>
pub fn from_reader<T: DeserializeOwned, R: Read>( &self, reader: R, ) -> Result<T, Error>
Deserializes the records of a reader.
See from_reader.