deser-ini 0.10.1

INI files (and git's config files) for deser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::borrow::Cow;

use deser_core::Text;
use deser_core::de::{
    self, Deserialize, DeserializeDriver, DuplicateKeys, LexicalRules, deserialize_value,
};
use deser_core::{Atom, ContainerShape, Error, ErrorKind, Event, Source, TrackLocations};

use crate::parser::{self, Document, NodeKind, Range};
use crate::{Continuation, InlineComments, Quotes, Syntax};

/// Configures how INI files are deserialized.
///
/// The default ([`new`](Self::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`](Self::python) and
/// [`git`](Self::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\"");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeserializerConfig {
    pub(crate) syntax: Syntax,
    pub(crate) inline_comments: InlineComments,
    pub(crate) colon_delimiter: bool,
    pub(crate) continuation: Continuation,
    pub(crate) quotes: Quotes,
    pub(crate) allow_no_value: bool,
    pub(crate) lowercase_names: bool,

    context: deser_core::Context,
}

impl Default for DeserializerConfig {
    fn default() -> DeserializerConfig {
        DeserializerConfig::new()
    }
}

impl DeserializerConfig {
    /// Creates the default configuration (common INI files).
    ///
    /// * [`Syntax::Ini`]
    /// * [`InlineComments::AfterWhitespace`]
    /// * `=` and `:` are delimiters
    /// * [`Continuation::Indented`]
    /// * [`Quotes::Value`]
    /// * keys without values are allowed
    /// * names keep their case
    pub const fn new() -> DeserializerConfig {
        DeserializerConfig {
            syntax: Syntax::Ini,
            inline_comments: InlineComments::AfterWhitespace,
            colon_delimiter: true,
            continuation: Continuation::Indented,
            quotes: Quotes::Value,
            allow_no_value: true,
            lowercase_names: false,

            context: deser_core::Context::new(),
        }
    }

    /// 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");
    /// ```
    pub const fn python() -> DeserializerConfig {
        DeserializerConfig::builder()
            .inline_comments(InlineComments::None)
            .quotes(Quotes::None)
            .build()
    }

    /// 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");
    /// ```
    pub const fn git() -> DeserializerConfig {
        DeserializerConfig::builder().syntax(Syntax::Git).build()
    }

    /// Returns a builder for the configuration (see [`DeserializerConfigBuilder`]).
    pub const fn builder() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder::new()
    }

    /// Returns a builder that starts with this configuration.
    pub const fn into_builder(self) -> DeserializerConfigBuilder {
        DeserializerConfigBuilder { value: self }
    }

    /// 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`](deser_core::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.
    pub fn set_context(&mut self, context: deser_core::Context) {
        self.context = context;
    }

    /// Returns the configuration without its context (for the frames of
    /// streams, which get the context of the stream).
    pub(crate) fn without_context(&self) -> DeserializerConfig {
        let mut config = self.clone();
        config.context = deser_core::Context::default();
        config
    }

    /// Returns the context the values are deserialized in.
    pub fn context(&self) -> &deser_core::Context {
        &self.context
    }

    /// Sets the syntax.
    ///
    /// The default is [`Syntax::Ini`].  With [`Syntax::Git`] the other
    /// options (except for the context) are ignored.
    pub const fn set_syntax(&mut self, syntax: Syntax) {
        self.syntax = syntax;
    }

    /// Returns the syntax.
    pub const fn syntax(&self) -> Syntax {
        self.syntax
    }

    /// Sets where comments start after values.
    ///
    /// The default is [`InlineComments::AfterWhitespace`].  Lines that
    /// start with `;` or `#` are always comments.
    pub const fn set_inline_comments(&mut self, comments: InlineComments) {
        self.inline_comments = comments;
    }

    /// Returns where comments start after values.
    pub const fn inline_comments(&self) -> InlineComments {
        self.inline_comments
    }

    /// 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`).
    pub const fn set_colon_delimiter(&mut self, yes: bool) {
        self.colon_delimiter = yes;
    }

    /// Returns if `:` separates keys and values.
    pub const fn colon_delimiter(&self) -> bool {
        self.colon_delimiter
    }

    /// Sets how values continue on the next lines.
    ///
    /// The default is [`Continuation::Indented`].
    pub const fn set_continuation(&mut self, continuation: Continuation) {
        self.continuation = continuation;
    }

    /// Returns how values continue on the next lines.
    pub const fn continuation(&self) -> Continuation {
        self.continuation
    }

    /// Sets how quoted values are read.
    ///
    /// The default is [`Quotes::Value`].
    pub const fn set_quotes(&mut self, quotes: Quotes) {
        self.quotes = quotes;
    }

    /// Returns how quoted values are read.
    pub const fn quotes(&self) -> Quotes {
        self.quotes
    }

    /// 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`](deser_core::adapters::Flag) adapter is `true` for it.  If
    /// `false`, such lines are an error.
    pub const fn set_allow_no_value(&mut self, yes: bool) {
        self.allow_no_value = yes;
    }

    /// Returns if keys can be given without value.
    pub const fn allow_no_value(&self) -> bool {
        self.allow_no_value
    }

    /// 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).
    pub const fn set_lowercase_names(&mut self, yes: bool) {
        self.lowercase_names = yes;
    }

    /// Returns if the names of sections and keys are lowercased.
    pub const fn lowercase_names(&self) -> bool {
        self.lowercase_names
    }

    /// Deserializes a value from an INI file.
    ///
    /// See [`from_str`](crate::from_str).
    pub fn from_str<'de, T: Deserialize<'de>>(&self, s: &'de str) -> Result<T, Error> {
        deserialize_value(|driver| self.drive_str(s, driver))
    }

    /// The part of [`from_str`](Self::from_str) that does not depend on the type
    /// of the value, it exists once.
    fn drive_str<'de>(
        &self,
        s: &'de str,
        driver: &mut DeserializeDriver<'_, 'de>,
    ) -> Result<(), Error> {
        de::Deserializer::drive(
            &mut Deserializer::from_str_with_config(s, self.clone()),
            driver,
        )
    }

    /// Deserializes a value from an INI file in a byte slice.
    ///
    /// See [`from_slice`](crate::from_slice).
    pub fn from_slice<'de, T: Deserialize<'de>>(&self, bytes: &'de [u8]) -> Result<T, Error> {
        deserialize_value(|driver| self.drive_slice(bytes, driver))
    }

    /// The part of [`from_slice`](Self::from_slice) that does not depend on the type
    /// of the value, it exists once.
    fn drive_slice<'de>(
        &self,
        bytes: &'de [u8],
        driver: &mut DeserializeDriver<'_, 'de>,
    ) -> Result<(), Error> {
        de::Deserializer::drive(
            &mut Deserializer::from_slice_with_config(bytes, self.clone()),
            driver,
        )
    }
}

/// Builds a [`DeserializerConfig`].
///
/// The methods have the names of the setters of [`DeserializerConfig`] (without `set_`).
#[derive(Debug, Clone)]
#[must_use]
pub struct DeserializerConfigBuilder {
    value: DeserializerConfig,
}

impl DeserializerConfigBuilder {
    /// Creates a builder that starts with the default.
    pub const fn new() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder {
            value: DeserializerConfig::new(),
        }
    }

    /// Sets the syntax.
    ///
    /// See [`DeserializerConfig::set_syntax`].
    pub const fn syntax(mut self, syntax: Syntax) -> DeserializerConfigBuilder {
        self.value.set_syntax(syntax);
        self
    }

    /// Sets where comments start after values.
    ///
    /// See [`DeserializerConfig::set_inline_comments`].
    pub const fn inline_comments(mut self, comments: InlineComments) -> DeserializerConfigBuilder {
        self.value.set_inline_comments(comments);
        self
    }

    /// Sets if `:` separates keys and values (like `=`).
    ///
    /// See [`DeserializerConfig::set_colon_delimiter`].
    pub const fn colon_delimiter(mut self, yes: bool) -> DeserializerConfigBuilder {
        self.value.set_colon_delimiter(yes);
        self
    }

    /// Sets how values continue on the next lines.
    ///
    /// See [`DeserializerConfig::set_continuation`].
    pub const fn continuation(mut self, continuation: Continuation) -> DeserializerConfigBuilder {
        self.value.set_continuation(continuation);
        self
    }

    /// Sets how quoted values are read.
    ///
    /// See [`DeserializerConfig::set_quotes`].
    pub const fn quotes(mut self, quotes: Quotes) -> DeserializerConfigBuilder {
        self.value.set_quotes(quotes);
        self
    }

    /// Sets if keys can be given without value.
    ///
    /// See [`DeserializerConfig::set_allow_no_value`].
    pub const fn allow_no_value(mut self, yes: bool) -> DeserializerConfigBuilder {
        self.value.set_allow_no_value(yes);
        self
    }

    /// Sets if the names of sections and keys are lowercased.
    ///
    /// See [`DeserializerConfig::set_lowercase_names`].
    pub const fn lowercase_names(mut self, yes: bool) -> DeserializerConfigBuilder {
        self.value.set_lowercase_names(yes);
        self
    }

    /// Sets the context the values are deserialized in.
    ///
    /// See [`DeserializerConfig::set_context`].
    pub fn context(mut self, context: deser_core::Context) -> DeserializerConfigBuilder {
        self.value.set_context(context);
        self
    }

    /// Returns the built [`DeserializerConfig`].
    pub const fn build(self) -> DeserializerConfig {
        // the value cannot be moved out of the builder in a const fn as the
        // builder needs dropping (the context has a destructor)
        // SAFETY: the value is read once and the builder is forgotten
        let value = unsafe { core::ptr::read(&self.value) };
        core::mem::forget(self);
        value
    }
}

impl Default for DeserializerConfigBuilder {
    fn default() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder::new()
    }
}

/// Deserializes INI files.
///
/// Most of the time the [`from_str`](crate::from_str) and
/// [`from_slice`](crate::from_slice) functions (or the methods of the same
/// name on [`DeserializerConfig`]) are all that is needed.  The deserializer
/// is useful to configure the driver, for instance to add layers, or to
/// update a value (see [`update`](deser_core::de::Deserializer::update)):
///
/// ```
/// use deser_path::{Path, PathLayer};
/// use deser_ini::Deserializer;
///
/// #[derive(Debug, deser::Deserialize)]
/// struct Config {
///     server: Server,
/// }
///
/// #[derive(Debug, deser::Deserialize)]
/// struct Server {
///     port: u16,
/// }
///
/// let err = Deserializer::from_str("[server]\nport = http\n")
///     .deserialize_with::<Config, _>(|driver| {
///         driver.push_layer(PathLayer::new())
///     })
///     .unwrap_err();
/// assert_eq!(err.message(), "invalid value \"http\", expected u16");
/// assert_eq!(err.attachment::<Path>().unwrap().to_string(), "server.port");
/// assert_eq!(err.line(), Some(2));
/// ```
pub struct Deserializer<'a> {
    input: &'a str,
    /// An error that is reported instead of parsing (invalid UTF-8).
    error: Option<Error>,
    config: DeserializerConfig,
}

impl<'a> Deserializer<'a> {
    /// Creates a new deserializer for a string.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(input: &'a str) -> Deserializer<'a> {
        Deserializer::from_str_with_config(input, DeserializerConfig::new())
    }

    /// Creates a new deserializer for a string with the given configuration.
    pub fn from_str_with_config(input: &'a str, config: DeserializerConfig) -> Deserializer<'a> {
        Deserializer {
            input,
            error: None,
            config,
        }
    }

    /// Creates a new deserializer for a byte slice.
    ///
    /// The input must be UTF-8 (a byte order mark is skipped), otherwise
    /// deserializing fails.
    pub fn from_slice(input: &'a [u8]) -> Deserializer<'a> {
        Deserializer::from_slice_with_config(input, DeserializerConfig::new())
    }

    /// Creates a new deserializer for a byte slice with the given
    /// configuration.
    pub fn from_slice_with_config(input: &'a [u8], config: DeserializerConfig) -> Deserializer<'a> {
        match std::str::from_utf8(input) {
            Ok(input) => Deserializer::from_str_with_config(input, config),
            Err(err) => Deserializer {
                input: "",
                error: Some(Error::with_offset(
                    ErrorKind::Syntax,
                    "input is not valid UTF-8",
                    err.valid_up_to(),
                )),
                config,
            },
        }
    }

    /// Returns the configuration.
    pub fn config(&self) -> &DeserializerConfig {
        &self.config
    }

    /// Deserializes the input.
    ///
    /// To configure the deserialization (for instance to add layers) use
    /// [`deserialize_with`](Self::deserialize_with).
    pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error> {
        de::Deserializer::deserialize(self)
    }

    /// Deserializes the input with a configured driver.
    ///
    /// The callback is invoked with the driver before the value is
    /// deserialized, for instance to add [`Layer`](deser_core::de::Layer)s.
    pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
    where
        T: Deserialize<'a>,
        F: FnOnce(&mut DeserializeDriver<'_, 'a>),
    {
        de::Deserializer::deserialize_with(self, setup)
    }

    /// Parses the input and feeds the events into the given driver.
    ///
    /// The whole input is parsed before the first event is emitted, so
    /// malformed input is reported before any value is deserialized.  Keys
    /// and values that are not changed (by quotes, escapes, continuation
    /// lines or lowercasing) are passed on borrowed from the input (see
    /// [`emit_borrowed`](DeserializeDriver::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`]).
    pub fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        if !self.config.context.is_empty() {
            driver.set_default_context(self.config.context.clone());
        }
        if let Some(err) = self.error.take() {
            return Err(err);
        }
        let doc = parser::parse(self.input, &self.config).map_err(|mut err| {
            err.resolve_position(self.input.as_bytes());
            err
        })?;
        let state = driver.state_mut();
        if TrackLocations::of(state) {
            Source(self.input.into()).set(state);
        }
        // the last value of repeated keys is used and everything is text
        // unless the context says otherwise
        DuplicateKeys::Last.set_default(state);
        LexicalRules::LENIENT.set_default(state);
        emit(&doc, self.input.len(), driver).map_err(|mut err| {
            err.resolve_position(self.input.as_bytes());
            err
        })
    }
}

impl<'a> de::Deserializer<'a> for Deserializer<'a> {
    fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        Deserializer::drive(self, driver)
    }
}

/// Returns the shape of a table.
///
/// Tables are multimaps, the keys are given once per value.
fn table_shape(doc: &Document<'_>, children: &[usize]) -> ContainerShape {
    let len = children
        .iter()
        .map(|&child| doc.nodes[child].values.len().max(1))
        .sum();
    let mut shape = ContainerShape::with_len(len);
    shape.set_multimap(true);
    shape
}

/// Emits the events of the document.
fn emit<'a>(
    doc: &Document<'a>,
    input_len: usize,
    driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<(), Error> {
    struct Frame {
        node: usize,
        pos: usize,
    }

    let root = &doc.nodes[0];
    emit_at(
        driver,
        Event::MapStart(table_shape(doc, &root.children)),
        root.range,
    )?;
    let mut stack = vec![Frame { node: 0, pos: 0 }];
    while let Some(frame) = stack.last_mut() {
        let table = &doc.nodes[frame.node];
        let Some(&child) = table.children.get(frame.pos) else {
            let range = if frame.node == 0 {
                (input_len, input_len)
            } else {
                table.range
            };
            stack.pop();
            emit_at(driver, Event::MapEnd, range)?;
            continue;
        };
        frame.pos += 1;
        let node = &doc.nodes[child];
        match node.kind {
            NodeKind::Key => {
                // the key is emitted for every value (tables are multimaps)
                for (value, range) in &node.values {
                    emit_text(driver, &node.name, node.range)?;
                    match *value {
                        Some(ref value) => emit_text(driver, value, *range)?,
                        None => emit_at(driver, Atom::Null, *range)?,
                    }
                }
            }
            NodeKind::Table => {
                emit_text(driver, &node.name, node.range)?;
                emit_at(
                    driver,
                    Event::MapStart(table_shape(doc, &node.children)),
                    node.range,
                )?;
                stack.push(Frame {
                    node: child,
                    pos: 0,
                });
            }
        }
    }
    Ok(())
}

/// Emits an event with a byte range.
#[inline]
fn emit_at<'e, E: Into<Event<'e>>>(
    driver: &mut DeserializeDriver<'_, '_>,
    event: E,
    range: Range,
) -> Result<(), Error> {
    driver.state_mut().set_input_range(range.0, range.1);
    driver.emit(event)
}

/// Emits text as lexical atom, borrowed if it's a slice of the input.
// the text is a `Cow` as borrowed text is passed on for `'a`
#[allow(clippy::ptr_arg)]
#[inline]
fn emit_text<'a>(
    driver: &mut DeserializeDriver<'_, 'a>,
    text: &Cow<'a, str>,
    range: Range,
) -> Result<(), Error> {
    driver.state_mut().set_input_range(range.0, range.1);
    match *text {
        Cow::Borrowed(text) => driver.emit_borrowed(Atom::Lexical(Text::borrowed(text))),
        Cow::Owned(ref text) => driver.emit(Atom::Lexical(Text::borrowed(text.as_str()))),
    }
}