confik 0.11.8

A library for reading application configuration split across multiple sources
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
//! Implementations of [`Configuration`](crate::Configuration) for standard library types.

use std::{
    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
    ffi::OsString,
    fmt::Display,
    hash::{BuildHasher, Hash},
    marker::PhantomData,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
    path::PathBuf,
    time::{Duration, SystemTime},
};

use serde::{de::DeserializeOwned, Deserialize};

use crate::{Configuration, ConfigurationBuilder, Error, MissingValue, UnexpectedSecret};

/// Convenience macro for the large number of foreign library types to implement the
/// [`Configuration`] using an [`Option`] as their [`ConfigurationBuilder`].
macro_rules! impl_multi_source_via_option {
    ($type:ty) => {
        impl Configuration for $type {
            type Builder = Option<Self>;
        }
    };

    ($($type:ty),* $(,)?) => {
        $(
            impl_multi_source_via_option! { $type }
        )*
    };
}

impl_multi_source_via_option! {
    // Signed integers
    i8, i16, i32, i64, i128, isize,

    // Unsigned integers
    u8, u16, u32, u64, u128, usize,

    // Floats
    f32, f64,

    // Networking types
    SocketAddr, SocketAddrV4, SocketAddrV6, IpAddr, Ipv4Addr, Ipv6Addr,

    // Time
    Duration, SystemTime,

    // Other standard types
    String, OsString, PathBuf, char, bool,
}

// Containers

// Type aliases for easier reading
type KeyOf<C> = <C as KeyedContainer>::Key;
type ItemOf<C> = <C as IntoIterator>::Item;
type ValueOf<C> = <C as KeyedContainer>::Value;
type TargetOf<B> = <B as ConfigurationBuilder>::Target;
type BuilderOf<T> = <T as Configuration>::Builder;

/// Builder type for unkeyed containers such as [`Vec`] (as opposed to keyed containers like
/// [`HashMap`]).
#[derive(Debug, Default, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
#[serde(from = "Container")]
pub enum UnkeyedContainerBuilder<Container, Target> {
    /// No data has been provided yet.
    ///
    /// Default to `None` but allow overwriting by later [`merge`][ConfigurationBuilder::merge]s.
    #[default]
    Unspecified,

    /// Data has been provided.
    ///
    /// Will not be overwritten by later [`merge`][ConfigurationBuilder::merge]s.
    Some(Container),

    /// Never instantiated, used to hold the [`Target`][ConfigurationBuilder::Target] type.
    _PhantomData(PhantomData<fn() -> Target>),
}

impl<Container, Target> From<Container> for UnkeyedContainerBuilder<Container, Target> {
    fn from(value: Container) -> Self {
        Self::Some(value)
    }
}

impl<Container, Target> ConfigurationBuilder for UnkeyedContainerBuilder<Container, Target>
where
    Self: DeserializeOwned,
    Container: IntoIterator + 'static,
    ItemOf<Container>: ConfigurationBuilder,
    Target: Default + FromIterator<TargetOf<ItemOf<Container>>>,
    for<'a> &'a Container: IntoIterator<Item = &'a ItemOf<Container>>,
{
    type Target = Target;

    fn merge(self, other: Self) -> Self {
        if matches!(self, Self::Unspecified) {
            other
        } else {
            self
        }
    }

    fn try_build(self) -> Result<Self::Target, Error> {
        match self {
            Self::Unspecified => Err(Error::MissingValue(MissingValue::default())),
            Self::Some(val) => val
                .into_iter()
                .map(ConfigurationBuilder::try_build)
                .collect(),
            Self::_PhantomData(_) => unreachable!("PhantomData is never instantiated"),
        }
    }

    fn contains_non_secret_data(&self) -> Result<bool, UnexpectedSecret> {
        match self {
            Self::Unspecified => Ok(false),

            // An explicit empty container is counted as as data, overriding any default.
            // If this branch is ever reached, then there is some data, even if it is empty.
            // So always return either an error or `true`.
            Self::Some(val) => val
                .into_iter()
                .map(ConfigurationBuilder::contains_non_secret_data)
                .enumerate()
                .find(|(_index, result)| result.is_err())
                .map(|(index, result)| result.map_err(|err| err.prepend(index.to_string())))
                .unwrap_or(Ok(true)),

            Self::_PhantomData(_) => unreachable!("PhantomData is never instantiated"),
        }
    }
}

impl<T> Configuration for Vec<T>
where
    T: Configuration,
    BuilderOf<T>: 'static,
{
    type Builder = UnkeyedContainerBuilder<Vec<BuilderOf<T>>, Self>;
}

impl<T> Configuration for BTreeSet<T>
where
    T: Configuration + Ord,
    BuilderOf<T>: Ord + 'static,
{
    type Builder = UnkeyedContainerBuilder<BTreeSet<BuilderOf<T>>, Self>;
}

impl<T, S> Configuration for HashSet<T, S>
where
    T: Configuration + Eq + Hash,
    BuilderOf<T>: Hash + Eq + 'static,
    S: BuildHasher + Default + 'static,
{
    type Builder = UnkeyedContainerBuilder<HashSet<BuilderOf<T>, S>, Self>;
}

/// Trait governing access to keyed containers
trait KeyedContainer {
    type Key;
    type Value;

    fn insert(&mut self, k: Self::Key, v: Self::Value);
    fn remove(&mut self, k: &Self::Key) -> Option<Self::Value>;
}

/// Builder type for keyed containers, such as [`HashMap`] (as opposed to unkeyed containers like [`Vec`]).
#[derive(Debug, Default, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
#[serde(from = "Container")]
pub enum KeyedContainerBuilder<Container, Target> {
    /// No data has been provided yet.
    ///
    /// Default to `None` but allow overwriting by later [`merge`][ConfigurationBuilder::merge]s.
    #[default]
    Unspecified,

    /// Data has been provided.
    ///
    /// Will not be overwritten by later [`merge`][ConfigurationBuilder::merge]s.
    Some(Container),

    /// Never instantiated, used to hold the [`Target`][ConfigurationBuilder::Target] type.
    _PhantomData(PhantomData<fn() -> Target>),
}

impl<Container, Target> From<Container> for KeyedContainerBuilder<Container, Target> {
    fn from(value: Container) -> Self {
        Self::Some(value)
    }
}

impl<Container, Target> ConfigurationBuilder for KeyedContainerBuilder<Container, Target>
where
    Self: DeserializeOwned,
    Container:
        KeyedContainer + IntoIterator<Item = (KeyOf<Container>, ValueOf<Container>)> + 'static,
    KeyOf<Container>: Display,
    ValueOf<Container>: ConfigurationBuilder + 'static,
    Target: Default + FromIterator<(KeyOf<Container>, TargetOf<ValueOf<Container>>)>,
    for<'a> &'a Container: IntoIterator<Item = (&'a KeyOf<Container>, &'a ValueOf<Container>)>,
{
    type Target = Target;

    fn merge(self, other: Self) -> Self {
        match (self, other) {
            (Self::_PhantomData(_), _) | (_, Self::_PhantomData(_)) => {
                unreachable!("PhantomData is never instantiated")
            }
            (Self::Unspecified, other) => other,
            (us, Self::Unspecified) => us,
            (Self::Some(mut us), Self::Some(other)) => {
                for (key, their_val) in other {
                    let val = if let Some(our_val) = us.remove(&key) {
                        our_val.merge(their_val)
                    } else {
                        their_val
                    };

                    us.insert(key, val);
                }

                Self::Some(us)
            }
        }
    }

    fn try_build(self) -> Result<Self::Target, Error> {
        match self {
            Self::Unspecified => Err(Error::MissingValue(MissingValue::default())),
            Self::Some(val) => val
                .into_iter()
                .map(|(key, value)| Ok((key, value.try_build()?)))
                .collect(),
            Self::_PhantomData(_) => unreachable!("PhantomData is never instantiated"),
        }
    }

    fn contains_non_secret_data(&self) -> Result<bool, UnexpectedSecret> {
        match self {
            Self::Unspecified => Ok(false),

            // An explicit empty container is counted as as data, overriding any default.
            // If this branch is ever reached, then there is some data, even if it is empty.
            // So always return either an error or `true`.
            Self::Some(val) => val
                .into_iter()
                .map(|(key, value)| (key, value.contains_non_secret_data()))
                .find(|(_key, result)| result.is_err())
                .map(|(key, result)| result.map_err(|err| err.prepend(key.to_string())))
                .unwrap_or(Ok(true)),

            Self::_PhantomData(_) => unreachable!("PhantomData is never instantiated"),
        }
    }
}

impl<K, V> KeyedContainer for BTreeMap<K, V>
where
    K: Ord,
{
    type Key = K;
    type Value = V;

    fn insert(&mut self, k: Self::Key, v: Self::Value) {
        self.insert(k, v);
    }

    fn remove(&mut self, k: &Self::Key) -> Option<Self::Value> {
        self.remove(k)
    }
}

impl<K, V> Configuration for BTreeMap<K, V>
where
    K: Ord + Display + DeserializeOwned + 'static,
    V: Configuration,
    BuilderOf<V>: 'static,
{
    type Builder = KeyedContainerBuilder<BTreeMap<K, BuilderOf<V>>, Self>;
}

impl<K, V, S> KeyedContainer for HashMap<K, V, S>
where
    K: Hash + Eq,
    S: BuildHasher + Default,
{
    type Key = K;
    type Value = V;

    fn insert(&mut self, k: Self::Key, v: Self::Value) {
        self.insert(k, v);
    }

    fn remove(&mut self, k: &Self::Key) -> Option<Self::Value> {
        self.remove(k)
    }
}

impl<K, V, S> Configuration for HashMap<K, V, S>
where
    K: Hash + Eq + Display + DeserializeOwned + 'static,
    V: Configuration,
    BuilderOf<V>: 'static,
    S: Default + BuildHasher + 'static,
{
    type Builder = KeyedContainerBuilder<HashMap<K, BuilderOf<V>, S>, Self>;
}

impl<T, const N: usize> Configuration for [T; N]
where
    [BuilderOf<T>; N]: DeserializeOwned + Default,
    T: Configuration,
{
    type Builder = [BuilderOf<T>; N];
}

impl<T, const N: usize> ConfigurationBuilder for [T; N]
where
    Self: DeserializeOwned + Default,
    T: ConfigurationBuilder,
{
    type Target = [TargetOf<T>; N];

    fn merge(self, other: Self) -> Self {
        let mut iter = other.into_iter();
        self.map(|us| us.merge(iter.next().unwrap()))
    }

    fn try_build(self) -> Result<Self::Target, Error> {
        self.into_iter()
            .enumerate()
            .map(|(index, val)| {
                val.try_build().map_err(|err| match err {
                    Error::MissingValue(err) => Error::MissingValue(err.prepend(index.to_string())),
                    err => err,
                })
            })
            .collect::<Result<Vec<_>, _>>()?
            .try_into()
            .map_err(|vec: Vec<_>| {
                Error::MissingValue(MissingValue::default().prepend(vec.len().to_string()))
            })
    }

    fn contains_non_secret_data(&self) -> Result<bool, UnexpectedSecret> {
        self.iter()
            .map(ConfigurationBuilder::contains_non_secret_data)
            .enumerate()
            .try_fold(false, |has_secret, (index, val)| {
                Ok(val.map_err(|err| err.prepend(index.to_string()))? || has_secret)
            })
    }
}

/// `PhantomData` does not need a builder, however we cannot use `()` as that would make `T`
/// unconstrained. Instead just making it use itself as a builder and rely on serde handling it
/// alright.
impl<T> Configuration for PhantomData<T> {
    type Builder = Self;
}

/// `PhantomData` does not need a builder, however we cannot use `()` as that would make `T`
/// unconstrained. Instead just making it use itself as a builder and rely on serde handling it
/// alright.
impl<T> ConfigurationBuilder for PhantomData<T> {
    type Target = Self;

    fn merge(self, _other: Self) -> Self {
        self
    }

    fn try_build(self) -> Result<Self::Target, Error> {
        Ok(self)
    }

    fn contains_non_secret_data(&self) -> Result<bool, UnexpectedSecret> {
        Ok(false)
    }
}

/// Build an `Option<T>` with a custom structure as we want `None` to be an explicit value that will
/// not be overwritten.
impl<T: Configuration> Configuration for Option<T>
where
    OptionBuilder<BuilderOf<T>>: DeserializeOwned,
{
    type Builder = OptionBuilder<BuilderOf<T>>;
}

/// Build an `Option<T>` with a custom structure as we want `None` to be an explicit value that will
/// not be overwritten.
#[derive(Debug, Default, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
#[serde(from = "Option<T>")]
pub enum OptionBuilder<T> {
    /// No item has been provided yet.
    ///
    /// Default to `None` but allow overwriting by later [`merge`][ConfigurationBuilder::merge]s.
    #[default]
    Unspecified,

    /// Explicit `None`.
    ///
    /// Will not be overwritten by later [`merge`][ConfigurationBuilder::merge]s.
    None,

    /// Explicit `Some`.
    ///
    /// Will not be overwritten by later [`merge`][ConfigurationBuilder::merge]s.
    Some(T),
}

impl<T> From<Option<T>> for OptionBuilder<T> {
    fn from(opt: Option<T>) -> Self {
        opt.map_or(Self::None, |val| Self::Some(val))
    }
}

impl<T: ConfigurationBuilder> ConfigurationBuilder for OptionBuilder<T>
where
    Self: DeserializeOwned,
{
    type Target = Option<TargetOf<T>>;

    fn merge(self, other: Self) -> Self {
        match (self, other) {
            // If both `Some` then merge the contained builders
            (Self::Some(us), Self::Some(other)) => Self::Some(us.merge(other)),
            // If we don't have a value then always take the other
            (Self::Unspecified, other) => other,
            // Either:
            // - We're explicitly `None`
            // - We're explicitly `Some` and the other is `Unspecified` or `None`
            //
            // In either case, just take our value, which should be preferred to other.
            (us, _) => us,
        }
    }

    fn try_build(self) -> Result<Self::Target, Error> {
        match self {
            Self::Unspecified | Self::None => Ok(None),
            Self::Some(val) => Ok(Some(val.try_build()?)),
        }
    }

    fn contains_non_secret_data(&self) -> Result<bool, UnexpectedSecret> {
        match self {
            Self::Some(data) => data.contains_non_secret_data(),

            // An explicit `None` is counted as data, overriding any default.
            Self::None => Ok(true),

            Self::Unspecified => Ok(false),
        }
    }
}