Skip to main content

DeserializerConfig

Struct DeserializerConfig 

Source
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

Source

pub const fn new() -> DeserializerConfig

Creates the default configuration (common INI files).

Source

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");
Source

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");
Source

pub const fn builder() -> DeserializerConfigBuilder

Returns a builder for the configuration (see DeserializerConfigBuilder).

Source

pub const fn into_builder(self) -> DeserializerConfigBuilder

Returns a builder that starts with this configuration.

Source

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.

Source

pub fn context(&self) -> &Context

Returns the context the values are deserialized in.

Source

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.

Source

pub const fn syntax(&self) -> Syntax

Returns the syntax.

Source

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.

Source

pub const fn inline_comments(&self) -> InlineComments

Returns where comments start after values.

Source

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).

Source

pub const fn colon_delimiter(&self) -> bool

Returns if : separates keys and values.

Source

pub const fn set_continuation(&mut self, continuation: Continuation)

Sets how values continue on the next lines.

The default is Continuation::Indented.

Source

pub const fn continuation(&self) -> Continuation

Returns how values continue on the next lines.

Source

pub const fn set_quotes(&mut self, quotes: Quotes)

Sets how quoted values are read.

The default is Quotes::Value.

Source

pub const fn quotes(&self) -> Quotes

Returns how quoted values are read.

Source

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.

Source

pub const fn allow_no_value(&self) -> bool

Returns if keys can be given without value.

Source

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).

Source

pub const fn lowercase_names(&self) -> bool

Returns if the names of sections and keys are lowercased.

Source

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.

Source

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

Source

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

Creates a reader of an INI file (see deser::io::Reader).

See StreamDeserializer.

Source

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

Deserializes an INI file from 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.