module 0.3.1

Modular NixOS-style configuration crate.
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
//! [`Error`] & friends.
//!
//! This module contains all the machinery used to present nice and useful error
//! messages from merge operations.

use core::fmt::{self, Debug, Display, Write};
use core::iter::FusedIterator;
use core::mem::discriminant;

use alloc::boxed::Box;
use alloc::collections::linked_list::{self, LinkedList};
use alloc::string::ToString;

/// Kind of [`Error`].
#[non_exhaustive]
pub enum ErrorKind {
    /// Values cannot be merged.
    ///
    /// This error should be returned by [`Merge`] implementations when it is
    /// not clear how to merge the values. For example, the 2 values may have
    /// the same priority.
    ///
    /// For many types, the term "merge" does not make sense. How should one
    /// merge 2 [`i32`]s for instance? Types which do not have an obvious merge
    /// strategy or types on which the notion of "merging" cannot be defined
    /// clearly are called "unmergeable". Such types should have a [`Merge`]
    /// implementation but it should unconditionally return this error.
    ///
    /// [`Merge`]: crate::merge::Merge
    Collision,

    /// Cyclic module imports.
    ///
    /// This error should not need to be raised by [`Merge`] implementations. It
    /// is supposed to be raised by evaluators when they encounter cyclic module
    /// imports.
    ///
    /// [`Merge`]: crate::merge::Merge
    Cycle,

    /// A custom error that occurred during merging or evaluating.
    ///
    /// Contains a [`Box`]ed error object.
    Custom(Box<dyn Display + Send + Sync + 'static>),
}

impl ErrorKind {
    /// Check whether `self` is [`ErrorKind::Collision`].
    pub fn is_collision(&self) -> bool {
        matches!(self, Self::Collision)
    }

    /// Check whether `self` is [`ErrorKind::Cycle`].
    pub fn is_cycle(&self) -> bool {
        matches!(self, Self::Cycle)
    }

    /// Check whether `self` is [`ErrorKind::Custom`].
    pub fn is_custom(&self) -> bool {
        matches!(self, Self::Custom(_))
    }
}

impl Debug for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Collision => f.write_str("Collision"),
            Self::Cycle => f.write_str("Cycle"),
            Self::Custom(x) => write!(f, "Custom(\"{x}\")"),
        }
    }
}

impl Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Collision => f.write_str("value collision"),
            Self::Cycle => f.write_str("cyclic imports"),
            Self::Custom(x) => x.fmt(f),
        }
    }
}

impl PartialEq for ErrorKind {
    fn eq(&self, other: &Self) -> bool {
        discriminant(self) == discriminant(other)
    }
}

impl Eq for ErrorKind {}

/// The module backtrace.
#[derive(Clone)]
pub struct Trace {
    modules: LinkedList<Box<str>>,
}

impl Debug for Trace {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.modules()).finish()
    }
}

impl<D> FromIterator<D> for Trace
where
    D: Display,
{
    fn from_iter<T: IntoIterator<Item = D>>(iter: T) -> Self {
        Self {
            modules: iter
                .into_iter()
                .map(|x| x.to_string().into_boxed_str())
                .collect(),
        }
    }
}

impl Trace {
    /// Create a new [`Modules`].
    pub const fn new() -> Self {
        Self {
            modules: LinkedList::new(),
        }
    }

    /// Get the number of modules in the backtrace.
    pub fn len(&self) -> usize {
        self.modules.len()
    }

    /// Check if the backtrace has any modules.
    pub fn is_empty(&self) -> bool {
        self.modules.is_empty()
    }

    /// Push `module` to the front.
    pub fn push_front<D>(&mut self, module: D)
    where
        D: Display,
    {
        self.modules.push_front(module.to_string().into_boxed_str());
    }

    /// Push `module` to the back.
    pub fn push_back<D>(&mut self, module: D)
    where
        D: Display,
    {
        self.modules.push_back(module.to_string().into_boxed_str());
    }

    /// Get an iterator over all modules in the backtrace.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use module::merge::error::Trace;
    /// let mut trace = Trace::new();
    ///
    /// trace.push_back("module 1");
    /// trace.push_back("module 2");
    ///
    /// let mut iter = trace.modules();
    /// assert_eq!(iter.next(), Some("module 1"));
    /// assert_eq!(iter.next(), Some("module 2"));
    /// assert_eq!(iter.next(), None);
    /// ```
    pub fn modules(&self) -> Modules<'_> {
        Modules(self.modules.iter())
    }
}

/// Borrowing iterator for [`Modules`].
pub struct Modules<'a>(linked_list::Iter<'a, Box<str>>);

impl Debug for Modules<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Modules").finish_non_exhaustive()
    }
}

impl<'a> Iterator for Modules<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| &**x)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl DoubleEndedIterator for Modules<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| &**x)
    }
}

impl ExactSizeIterator for Modules<'_> {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl FusedIterator for Modules<'_> {}

/// The value name.
#[derive(Clone)]
pub struct Value {
    components: LinkedList<Box<str>>,
}

impl Value {
    /// Create a new [`Value`].
    pub const fn new() -> Self {
        Self {
            components: LinkedList::new(),
        }
    }

    /// Get the number of components of the [`Value`].
    pub fn len(&self) -> usize {
        self.components.len()
    }

    /// Check if the [`Value`] has any components.
    pub fn is_empty(&self) -> bool {
        self.components.is_empty()
    }

    /// Push `component` to the front.
    pub fn push_front<D>(&mut self, component: D)
    where
        D: Display,
    {
        self.components
            .push_front(component.to_string().into_boxed_str());
    }

    /// Push `component` to the back.
    pub fn push_back<D>(&mut self, component: D)
    where
        D: Display,
    {
        self.components
            .push_back(component.to_string().into_boxed_str());
    }

    /// Get an iterator over all components of the value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use module::merge::error::Value;
    /// let mut value = Value::new();
    ///
    /// value.push_back("value 1");
    /// value.push_back("value 2");
    ///
    /// let mut iter = value.components();
    /// assert_eq!(iter.next(), Some("value 1"));
    /// assert_eq!(iter.next(), Some("value 2"));
    /// assert_eq!(iter.next(), None);
    /// ```
    pub fn components(&self) -> Components<'_> {
        Components(self.components.iter())
    }
}

impl Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sep = if f.align().is_some() { f.fill() } else { '.' };

        if !f.alternate() {
            f.write_char('\"')?;
        }

        for (i, component) in self.components().enumerate() {
            if i > 0 {
                f.write_char(sep)?;
            }

            f.write_str(component)?;
        }

        if !f.alternate() {
            f.write_char('\"')?;
        }

        Ok(())
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <Self as Debug>::fmt(self, f)
    }
}

/// Borrowing iterator for [`Value`].
pub struct Components<'a>(linked_list::Iter<'a, Box<str>>);

impl Debug for Components<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Components").finish_non_exhaustive()
    }
}

impl<'a> Iterator for Components<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| &**x)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl DoubleEndedIterator for Components<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| &**x)
    }
}

impl ExactSizeIterator for Components<'_> {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl FusedIterator for Components<'_> {}

/// Error returned by [`Merge`].
///
/// # Display
///
/// The default [`Display`] implementation may not fit into the style of
/// your app.
///
/// ```rust
/// # use module::merge::{Merge, Error, Context};
/// # let a = 42i32;
/// # let b = 43i32;
/// let r = a.merge(b)
///     .value("count")
///     .value("settings")
///     .module("user.json")
///     .module("config.json");
///
/// let err = r.unwrap_err();
///
/// assert_eq!(format!("{err}"),
/// r#"value collision while evaluating "settings.count"
///
///     in user.json
///   from config.json
/// "#);
///
/// // without quotes...
/// assert_eq!(format!("{err:#}"),
/// r#"value collision while evaluating settings.count
///
///     in user.json
///   from config.json
/// "#);
///
/// // or with a custom separator...
/// assert_eq!(format!("{err:/<}"),
/// r#"value collision while evaluating "settings/count"
///
///     in user.json
///   from config.json
/// "#);
/// ```
///
/// For this reason, the [`Error`] type tries to make all relevant
/// information publically accessible. This way you can write another
/// [`Display`] implementation that fits more inline with your vision.
///
/// [`Merge`]: crate::Merge
#[derive(Debug)]
#[allow(clippy::manual_non_exhaustive)]
pub struct Error {
    _priv: (),

    /// Error kind.
    pub kind: ErrorKind,

    /// Module trace.
    ///
    /// This field holds information regarding the module in which the error
    /// occurred.
    pub trace: Trace,

    /// Value name.
    ///
    /// This field holds the full path of the value which caused the merge
    /// error. The path is stored as a list of components and can be accessed as
    /// an [`Iterator`].
    pub value: Value,
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Self::with_kind(kind)
    }
}

impl Error {
    /// Raised when [`Merge`] encounters 2 values which cannot be merged using
    /// the current strategy.
    ///
    /// [`Merge`]: crate::Merge
    pub fn collision() -> Self {
        Self::with_kind(ErrorKind::Collision)
    }

    /// Raised when evaluation encounters cyclic imports.
    pub fn cycle() -> Self {
        Self::with_kind(ErrorKind::Cycle)
    }

    /// Raised when there is a general error when merging 2 values.
    pub fn custom<T>(msg: T) -> Self
    where
        T: Display + Send + Sync + 'static,
    {
        Self::with_kind(ErrorKind::Custom(Box::new(msg)))
    }

    fn with_kind(kind: ErrorKind) -> Self {
        Self {
            _priv: (),
            kind,
            trace: Trace::new(),
            value: Value::new(),
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.kind, f)?;

        if !self.value.is_empty() {
            f.write_str(" while evaluating ")?;
            Display::fmt(&self.value, f)?;
        }

        f.write_char('\n')?;
        f.write_char('\n')?;

        for (i, module) in self.trace.modules().rev().enumerate() {
            if i == 0 {
                f.write_str("    in ")?;
            } else {
                f.write_str("  from ")?;
            }

            f.write_str(module)?;
            f.write_char('\n')?;
        }

        Ok(())
    }
}

impl core::error::Error for Error {}