pub struct DeserializerConfig { /* private fields */ }Expand description
Configures how INI files are deserialized.
The default (new) reads the INI files that are common
today: ; and # comments, = and : delimiters, comments after
values ( ; comment), values continued on indented lines, quoted values
and keys without values. The presets python and
git read the dialects of Python’s configparser and of
git. The configuration is independent of the input so it can be created
once (even as a constant) and used for many inputs.
use std::collections::BTreeMap;
use deser_ini::{DeserializerConfig, Quotes};
const CONFIG: DeserializerConfig =
DeserializerConfig::builder().quotes(Quotes::None).build();
let value: BTreeMap<String, BTreeMap<String, String>> =
CONFIG.from_str("[a]\nb = \"c\"").unwrap();
assert_eq!(value["a"]["b"], "\"c\"");Implementations§
Source§impl DeserializerConfig
impl DeserializerConfig
Sourcepub const fn new() -> DeserializerConfig
pub const fn new() -> DeserializerConfig
Creates the default configuration (common INI files).
Syntax::IniInlineComments::AfterWhitespace=and:are delimitersContinuation::IndentedQuotes::Value- keys without values are allowed
- names keep their case
Sourcepub const fn python() -> DeserializerConfig
pub const fn python() -> DeserializerConfig
Creates the configuration for the files of Python’s configparser.
These are setup.cfg, tox.ini, pytest.ini and the like. This is
the default but without inline comments and quotes, like
RawConfigParser(strict=False, allow_no_value=True, allow_unnamed_section=True, interpolation=None) with keys that keep
their case.
use std::collections::BTreeMap;
use deser_ini::DeserializerConfig;
let value: BTreeMap<String, BTreeMap<String, String>> =
DeserializerConfig::python()
.from_str("[tox]\nenvlist = py312 ; py313\n")
.unwrap();
assert_eq!(value["tox"]["envlist"], "py312 ; py313");Sourcepub const fn git() -> DeserializerConfig
pub const fn git() -> DeserializerConfig
Creates the configuration for git’s config files.
This is Syntax::Git which reads .gitconfig, .git/config and
.gitmodules like git does.
use std::collections::BTreeMap;
use deser_ini::DeserializerConfig;
type Config = BTreeMap<String, BTreeMap<String, BTreeMap<String, String>>>;
let config: Config = DeserializerConfig::git()
.from_str("[remote \"origin\"]\n\turl = https://example.com/x.git\n")
.unwrap();
assert_eq!(config["remote"]["origin"]["url"], "https://example.com/x.git");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 set_syntax(&mut self, syntax: Syntax)
pub const fn set_syntax(&mut self, syntax: Syntax)
Sets the syntax.
The default is Syntax::Ini. With Syntax::Git the other
options (except for the context) are ignored.
Sourcepub const fn set_inline_comments(&mut self, comments: InlineComments)
pub const fn set_inline_comments(&mut self, comments: InlineComments)
Sets where comments start after values.
The default is InlineComments::AfterWhitespace. Lines that
start with ; or # are always comments.
Sourcepub const fn inline_comments(&self) -> InlineComments
pub const fn inline_comments(&self) -> InlineComments
Returns where comments start after values.
Sourcepub const fn set_colon_delimiter(&mut self, yes: bool)
pub const fn set_colon_delimiter(&mut self, yes: bool)
Sets if : separates keys and values (like =).
The default is true, the first = or : of a line separates the
key and the value (url = http://x is the key url).
Sourcepub const fn colon_delimiter(&self) -> bool
pub const fn colon_delimiter(&self) -> bool
Returns if : separates keys and values.
Sourcepub const fn set_continuation(&mut self, continuation: Continuation)
pub const fn set_continuation(&mut self, continuation: Continuation)
Sets how values continue on the next lines.
The default is Continuation::Indented.
Sourcepub const fn continuation(&self) -> Continuation
pub const fn continuation(&self) -> Continuation
Returns how values continue on the next lines.
Sourcepub const fn set_quotes(&mut self, quotes: Quotes)
pub const fn set_quotes(&mut self, quotes: Quotes)
Sets how quoted values are read.
The default is Quotes::Value.
Sourcepub const fn set_allow_no_value(&mut self, yes: bool)
pub const fn set_allow_no_value(&mut self, yes: bool)
Sets if keys can be given without value.
The default is true: a line with a key but no delimiter (like
skip-name-resolve in MySQL’s configuration) is the key with a null
value. Optionals are None and the
Flag adapter is true for it. If
false, such lines are an error.
Sourcepub const fn allow_no_value(&self) -> bool
pub const fn allow_no_value(&self) -> bool
Returns if keys can be given without value.
Sourcepub const fn set_lowercase_names(&mut self, yes: bool)
pub const fn set_lowercase_names(&mut self, yes: bool)
Sets if the names of sections and keys are lowercased.
The default is false. Only ASCII letters are lowercased. This
makes names case insensitive, like they are for Windows and Python’s
configparser (which lowercases keys).
Sourcepub const fn lowercase_names(&self) -> bool
pub const fn lowercase_names(&self) -> bool
Returns if the names of sections and keys are lowercased.
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 a value from an INI file.
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 a value from an INI file in 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 an INI file (see
deser::io::Reader).
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 an INI file from a reader.
See from_reader.