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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Configuration loading.
//!
//! To load the configuration, multiple sources may need to be combined ‒ multiple files,
//! directories with files, command line, environment variables... and may need to be reloaded
//! during the lifetime of an application.
//!
//! The [`Spirit`][crate::Spirit] object (and it's [`Builder`][crate::Builder]) provide high-level
//! semi-automagical management of that. If you do not want to have the full machinery of that, you
//! can use this module to do the loading manually.
//!
//! The lifetime of loading is:
//!
//! 1. Create a [`Builder`][crate::cfg_loader::Builder] with
//!    [`Builder::new`][crate::cfg_loader::Builder::new].
//! 2. Configure the it, using the methods on it.
//! 3. Parse the command line and prepare the loader with
//!    [`build`][crate::cfg_loader::Builder::build] (or, alternatively
//!    [`build_no_opts`][crate::cfg_loader::Builder::build_no_opts] if command line should not be
//!    considered).
//! 4. Load (even as many times as needed) the configuration using
//!    [`load`][crate::cfg_loader::Loader::load].
//!
//! # Examples
//!
//! ```rust
//! use serde::Deserialize;
//! use spirit::{AnyError, Empty};
//! use spirit::cfg_loader::Builder;
//!
//! #[derive(Default, Deserialize)]
//! struct Cfg {
//!     #[serde(default)]
//!     message: String,
//! }
//!
//! fn main() -> Result<(), AnyError> {
//!     let (Empty {}, mut loader) = Builder::new()
//!         .build();
//!     let cfg: Cfg = loader.load()?;
//!     println!("{}", cfg.message);
//!     Ok(())
//! }
//! ```

use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::ffi::OsString;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::path::{Path, PathBuf};

use config::{Config, Environment, File, FileFormat, Source};
use err_context::prelude::*;
use fallible_iterator::FallibleIterator;
use log::{debug, trace, warn};
use serde::de::DeserializeOwned;
use serde::Serialize;
use structopt::clap::{App, Arg};
use structopt::StructOpt;
use toml::Value;

use crate::utils;
use crate::AnyError;

#[derive(Default)]
struct CommonOpts {
    config_overrides: Vec<(String, String)>,

    configs: Vec<PathBuf>,
}

struct OptWrapper<O> {
    common: CommonOpts,
    other: O,
}

// Unfortunately, StructOpt doesn't like flatten with type parameter
// (https://github.com/TeXitoi/structopt/issues/128). It is not even trivial to do, since some of
// the very important functions are *not* part of the trait.
//
// Furthermore, flattening does ugly things to our version and name and description. Therefore, we
// simply manually use clap and enrich all the things here by the right parameters (inspired by
// what structopt for the CommonOpts actually derives).
impl<O: StructOpt> StructOpt for OptWrapper<O> {
    fn clap<'a, 'b>() -> App<'a, 'b> {
        O::clap()
            .arg(
                Arg::with_name("config-overrides")
                    .takes_value(true)
                    .multiple(true)
                    .validator(|s| {
                        utils::key_val(s.as_str())
                            .map(|_: (String, String)| ())
                            .map_err(|e| e.to_string())
                    })
                    .help("Override specific config values")
                    .short("C")
                    .long("config-override")
                    .number_of_values(1),
            )
            .arg(
                Arg::with_name("configs")
                    .takes_value(true)
                    .multiple(true)
                    .help("Configuration files or directories to load"),
            )
    }

    fn from_clap(matches: &structopt::clap::ArgMatches) -> Self {
        let common = CommonOpts {
            config_overrides: matches
                .values_of("config-overrides")
                .map_or_else(Vec::new, |v| {
                    v.map(|s| utils::key_val(s).unwrap()).collect()
                }),
            configs: matches
                .values_of_os("configs")
                .map_or_else(Vec::new, |v| v.map(utils::absolute_from_os_str).collect()),
        };
        OptWrapper {
            common,
            other: StructOpt::from_clap(matches),
        }
    }
}

/// An error returned whenever the user passes something not a file nor a directory as
/// configuration.
#[derive(Clone, Debug)]
pub struct InvalidFileType(PathBuf);

impl Display for InvalidFileType {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        write!(
            fmt,
            "Configuration path {} is not a file nor a directory",
            self.0.display()
        )
    }
}

impl Error for InvalidFileType {}

/// Returned if configuration path is missing.
#[derive(Clone, Debug)]
pub struct MissingFile(PathBuf);

impl Display for MissingFile {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        write!(
            fmt,
            "Configuration path {} does not exist",
            self.0.display()
        )
    }
}

impl Error for MissingFile {}

/// Interface for configuring configuration loading options.
///
/// This is the common interface of [`cfg_loader::Builder`][Builder] and [spirit
/// `Builder`][crate::Builder] for configuring how and where from should configuration be loaded.
/// The methods are available on both and do the same thing.
///
/// The interface is also implemented on `Result<ConfigBuilder, AnyError>`. This is both for
/// convenience and for gathering errors to be handled within
/// [`SpiritBuilder::run`][crate::SpiritBuilder::run] in uniform way.
pub trait ConfigBuilder: Sized {
    /// Sets the configuration paths in case the user doesn't provide any.
    ///
    /// This replaces any previously set default paths. If none are specified and the user doesn't
    /// specify any either, no config is loaded (but it is not an error in itself, simply the
    /// defaults will be used, if available).
    ///
    /// This has no effect if the user does provide configuration paths on the command line.
    fn config_default_paths<P, I>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>;

    /// Specifies the default configuration.
    ///
    /// This „loads“ the lowest layer of the configuration from the passed string. The expected
    /// format is TOML.
    ///
    /// Any user-provided configuration will be layered on top of it.
    ///
    /// An alternative is to supply the lowest layer through the
    /// [`config_defaults_typed`][ConfigBuilder::config_defaults_typed] ‒ only the last one of the
    /// two wins.
    fn config_defaults<D: Into<String>>(self, config: D) -> Self;

    /// Specifies the default configuration as typed value.
    ///
    /// This is an alternative to [`config_defaults`]. Unlike that
    /// one, this accepts a typed instance of the configuration ‒ the configuration structure.
    ///
    /// The advantage is there's less risk of typos or malformed input and sometimes convenience.
    /// This is, however, less flexible as this needs a *complete* configuration, while
    /// [`config_defaults`] accepts even configurations that are missing some fields. In such case,
    /// defaults would be used (if they are available on the [`Deserialize`][serde::Deserialize]
    /// level) or the configuration would fail if the user provided configuration files don't
    /// contain it.
    ///
    /// Note that pairing between the type passed here and the type of configuration structure
    /// extracted later is not tied together at compile time (as this allows a workaround for the
    /// above described inflexibility, but also because tying them together would be too much work
    /// for very little benefit ‒ it's not likely there would be two different configuration
    /// structures in the same program and got mixed up).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Deserialize, Serialize};
    /// use spirit::AnyError;
    /// use spirit::cfg_loader::{Builder, ConfigBuilder};
    ///
    /// #[derive(Default, Deserialize, Serialize)]
    /// struct Cfg {
    ///     #[serde(default)]
    ///     message: String,
    /// }
    ///
    /// fn main() -> Result<(), AnyError> {
    ///     let mut loader = Builder::new()
    ///         .config_defaults_typed(&Cfg {
    ///             message: "hello".to_owned()
    ///         })
    ///         // Expect, as error here would mean a bug, not bad external conditions.
    ///         .expect("Invalid default configuration")
    ///         .build_no_opts();
    ///     let cfg: Cfg = loader.load()?;
    ///     assert_eq!(cfg.message, "hello");
    ///     Ok(())
    /// }
    /// ```
    ///
    /// [`config_defaults`]: ConfigBuilder::config_defaults
    fn config_defaults_typed<C: Serialize>(self, config: &C) -> Result<Self, AnyError> {
        // We internally delegate into serializing to toml and just calling the other method. This
        // is mostly to avoid implementation complexity:
        // * We don't want two different parts of code doing about the same.
        // * This trait isn't parametrized by any type, but we would have to store the structure
        //   inside. `Serialize` is not very friendly to type erasure.
        // * We would have to explicitly handle the situation where both this and config_defaults
        //   gets called, while this way the last-to-be-called wins naturally follows from the
        //   implementation.
        //
        // The overhead should not be interesting, since configuration loading happens only
        // infrequently.

        // A little trick here. Converting a structure to TOML may result in errors if the
        // structure has the „wrong“ order of fields. Putting it into a type-less Value first
        // doesn't suffer from this error and Value outputs its contents in the correct order.
        let untyped = Value::try_from(config)?;
        Ok(self.config_defaults(toml::to_string(&untyped)?))
    }

    /// Enables loading configuration from environment variables.
    ///
    /// If this is used, after loading the normal configuration files, the environment of the
    /// process is examined. Variables with the provided prefix are merged into the configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::Deserialize;
    /// use spirit::{AnyError, Empty};
    /// use spirit::cfg_loader::{Builder, ConfigBuilder};
    ///
    /// #[derive(Default, Deserialize)]
    /// struct Cfg {
    ///     message: String,
    /// }
    ///
    /// const DEFAULT_CFG: &str = r#"
    /// message = "Hello"
    /// "#;
    ///
    /// fn main() -> Result<(), AnyError> {
    ///     let (_, mut loader) = Builder::new()
    ///         .config_defaults(DEFAULT_CFG)
    ///         .config_env("HELLO")
    ///         .build::<Empty>();
    ///     let cfg: Cfg = loader.load()?;
    ///     println!("{}", cfg.message);
    ///     Ok(())
    /// }
    /// ```
    ///
    /// If run like this, it'll print `Hi`. The environment takes precedence ‒ even if there was
    /// configuration file and it set the `message`, the `Hi` here would win.
    ///
    /// ```sh
    /// HELLO_MESSAGE="Hi" ./hello
    /// ```
    fn config_env<E: Into<String>>(self, env: E) -> Self;

    /// Configures a config dir filter for a single extension.
    ///
    /// Sets the config directory filter (see [`config_filter`](#method.config_filter)) to one
    /// matching this single extension.
    fn config_ext<E: Into<OsString>>(self, ext: E) -> Self {
        let ext = ext.into();
        self.config_filter(move |path| path.extension() == Some(&ext))
    }

    /// Configures a config dir filter for multiple extensions.
    ///
    /// Sets the config directory filter (see [`config_filter`](#method.config_filter)) to one
    /// matching files with any of the provided extensions.
    fn config_exts<I, E>(self, exts: I) -> Self
    where
        I: IntoIterator<Item = E>,
        E: Into<OsString>,
    {
        let exts = exts.into_iter().map(Into::into).collect::<HashSet<_>>();
        self.config_filter(move |path| {
            path.extension()
                .map(|ext| exts.contains(ext))
                .unwrap_or(false)
        })
    }

    /// Sets the config dir filter for all the supported extensions.
    ///
    /// Note that the list of extensions depends on the enabled features.
    fn config_supported_exts(self) -> Self {
        let mut exts = Vec::new();

        exts.push("toml");
        #[cfg(feature = "json")]
        exts.push("json");
        #[cfg(feature = "yaml")]
        exts.push("yaml");
        #[cfg(feature = "ini")]
        exts.push("ini");
        #[cfg(feature = "hjson")]
        exts.push("hjson");

        self.config_exts(exts)
    }

    /// Sets a configuration dir filter.
    ///
    /// If the user passes a directory path instead of a file path, the directory is traversed
    /// (every time the configuration is reloaded, so if files are added or removed, it is
    /// reflected) and files passing this filter are merged into the configuration, in the
    /// lexicographical order of their file names.
    ///
    /// There's ever only one filter and the default one passes no files (therefore, directories
    /// are ignored by default).
    ///
    /// The filter has no effect on files passed directly, only on loading directories. Only files
    /// directly in the directory are loaded ‒ subdirectories are not traversed.
    ///
    /// For more convenient ways to set the filter, see [`config_ext`](#method.config_ext) and
    /// [`config_exts`](#method.config_exts).
    fn config_filter<F: FnMut(&Path) -> bool + Send + 'static>(self, filter: F) -> Self;

    /// Sets if warning should be produced for each unused configuration key.
    ///
    /// If set, a warning message is produced upon loading a configuration for each unused key.
    /// Note that this might not always work reliably.
    ///
    /// The default is true.
    fn warn_on_unused(self, warn: bool) -> Self;
}

impl<C: ConfigBuilder, Error> ConfigBuilder for Result<C, Error> {
    fn config_default_paths<P, I>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        self.map(|c| c.config_default_paths(paths))
    }

    fn config_defaults<D: Into<String>>(self, config: D) -> Self {
        self.map(|c| c.config_defaults(config))
    }

    fn config_env<E: Into<String>>(self, env: E) -> Self {
        self.map(|c| c.config_env(env))
    }

    fn config_filter<F: FnMut(&Path) -> bool + Send + 'static>(self, filter: F) -> Self {
        self.map(|c| c.config_filter(filter))
    }

    fn warn_on_unused(self, warn: bool) -> Self {
        self.map(|c| c.warn_on_unused(warn))
    }
}

/// A builder for the [`Loader`].
///
/// See the [module documentation][crate::cfg_loader] for details about the use.
pub struct Builder {
    default_paths: Vec<PathBuf>,
    defaults: Option<String>,
    env: Option<String>,
    filter: Box<dyn FnMut(&Path) -> bool + Send>,
    warn_on_unused: bool,
}

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

impl Builder {
    /// Creates a new config loader builder.
    pub fn new() -> Self {
        Self {
            default_paths: Vec::new(),
            defaults: None,
            env: None,
            filter: Box::new(|_| false),
            warn_on_unused: true,
        }
    }

    /// The inner part of building, independent of where the options come from.
    fn build_inner(self, opts: CommonOpts) -> Loader {
        let files = if opts.configs.is_empty() {
            self.default_paths
        } else {
            opts.configs
        };
        trace!("Parsed command line arguments");

        Loader {
            files,
            defaults: self.defaults,
            env: self.env,
            filter: self.filter,
            overrides: opts.config_overrides.into_iter().collect(),
            warn_on_unused: self.warn_on_unused,
        }
    }

    /// Turns the builder into the [`Loader`].
    ///
    /// This parses the command line options ‒ the ones specified by the type parameter, enriched
    /// by options related to configuration (paths to config files and config overrides).
    ///
    /// This returns the parsed options and the loader.
    ///
    /// If the command line parsing fails, the application terminates (and prints relevant help).
    pub fn build<O: StructOpt>(self) -> (O, Loader) {
        let opts = OptWrapper::<O>::from_args();
        let loader = self.build_inner(opts.common);
        (opts.other, loader)
    }

    /// Turns this into the [`Loader`], without command line parsing.
    ///
    /// It is similar to [`build`][Builder::build], but doesn't parse the command line, therefore
    /// only the [`config_default_paths`][ConfigBuilder::config_default_paths] are used to
    /// find the config files.
    ///
    /// This is likely useful for tests.
    pub fn build_no_opts(self) -> Loader {
        self.build_inner(Default::default())
    }

    /// Turns this into the [`Loader`], and command line is parsed from the provided iterator.
    ///
    /// Similar to the [`build`][Builder::build], this returns the options and the loader. However,
    /// the options is loaded from the provided iterator and error is explicitly returned. This
    /// makes it better match for tests, but can be useful in other circumstances too.
    ///
    /// Note that the 0th argument is considered to be the name of the application and is not
    /// parsed as an option.
    pub fn build_explicit_opts<O, I>(self, args: I) -> Result<(O, Loader), AnyError>
    where
        O: StructOpt,
        I: IntoIterator,
        I::Item: Into<OsString> + Clone,
    {
        let opts = OptWrapper::<O>::from_iter_safe(args)?;
        let loader = self.build_inner(opts.common);
        Ok((opts.other, loader))
    }
}

impl ConfigBuilder for Builder {
    fn config_defaults<D: Into<String>>(self, config: D) -> Self {
        Self {
            defaults: Some(config.into()),
            ..self
        }
    }

    fn config_default_paths<P, I>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        let paths = paths.into_iter().map(Into::into).collect();
        Self {
            default_paths: paths,
            ..self
        }
    }

    fn config_env<E: Into<String>>(self, env: E) -> Self {
        Self {
            env: Some(env.into()),
            ..self
        }
    }

    fn config_filter<F: FnMut(&Path) -> bool + Send + 'static>(self, filter: F) -> Self {
        Self {
            filter: Box::new(filter),
            ..self
        }
    }

    fn warn_on_unused(self, warn: bool) -> Self {
        Self {
            warn_on_unused: warn,
            ..self
        }
    }
}

/// The loader of configuration.
///
/// This is created by the [`Builder`]. See the [module documentation][crate::cfg_loader] for
/// details.
pub struct Loader {
    files: Vec<PathBuf>,
    defaults: Option<String>,
    env: Option<String>,
    overrides: HashMap<String, String>,
    filter: Box<dyn FnMut(&Path) -> bool + Send>,
    warn_on_unused: bool,
}

impl Loader {
    /// Loads configuration according to parameters configured on the originating [`Builder`] and on
    /// the command line.
    ///
    /// Note that it is possible to load the configuration multiple times during the lifetime of
    /// the [`Loader`]. Each time all the sources are loaded from scratch (even new files in
    /// directories are discovered), so this can be used to reflect configuration changes at
    /// runtime.
    pub fn load<C: DeserializeOwned>(&mut self) -> Result<C, AnyError> {
        debug!("Loading configuration");
        let mut config = Config::new();
        let mut external_cfg = false;
        // To avoid problems with trying to parse without any configuration present (it would
        // complain that it found unit and whatever the config was is expected instead).
        config.merge(File::from_str("", FileFormat::Toml))?;
        if let Some(ref defaults) = self.defaults {
            trace!("Loading config defaults");
            config
                .merge(File::from_str(defaults, FileFormat::Toml))
                .context("Failed to read defaults")?;
        }
        for path in &self.files {
            if path.is_file() {
                external_cfg = true;
                trace!("Loading config file {:?}", path);
                config
                    .merge(File::from(path as &Path))
                    .with_context(|_| format!("Failed to load config file {:?}", path))?;
            } else if path.is_dir() {
                trace!("Scanning directory {:?}", path);
                // Take all the file entries passing the config file filter, handling errors on the
                // way.
                let filter = &mut self.filter;
                let mut files = fallible_iterator::convert(path.read_dir()?)
                    .map(|entry| -> Result<Option<PathBuf>, std::io::Error> {
                        let path = entry.path();
                        let meta = path.symlink_metadata()?;
                        if meta.is_file() && (filter)(&path) {
                            Ok(Some(path))
                        } else {
                            trace!("Skipping {:?}", path);
                            Ok(None)
                        }
                    })
                    .filter_map(Ok)
                    .collect::<Vec<_>>()?;
                // Traverse them sorted.
                files.sort();
                for file in files {
                    external_cfg = true;
                    trace!("Loading config file {:?}", file);
                    config
                        .merge(File::from(&file as &Path))
                        .with_context(|_| format!("Failed to load config file {:?}", file))?;
                }
            } else if path.exists() {
                return Err(InvalidFileType(path.to_owned()).into());
            } else {
                return Err(MissingFile(path.to_owned()).into());
            }
        }
        if let Some(env_prefix) = self.env.as_ref() {
            trace!("Loading config from environment {}", env_prefix);
            let env = Environment::with_prefix(env_prefix).separator("_");
            if !external_cfg {
                // We want to know if anything is in the environment, but that means iterating it
                // twice, so do it only if we know we didn't get anything already.
                external_cfg = !env
                    .collect()
                    .context("Failed to include environment in config")?
                    .is_empty();
            }
            config
                .merge(env)
                .context("Failed to include environment in config")?;
        }
        for (ref key, ref value) in &self.overrides {
            trace!("Config override {} => {}", key, value);
            config.set(*key, *value as &str).with_context(|_| {
                external_cfg = true;
                format!("Failed to push override {}={} into config", key, value)
            })?;
        }

        let mut ignored_cback = |ignored: serde_ignored::Path| {
            if self.warn_on_unused {
                warn!("Unused configuration key {}", ignored);
            }
        };
        let config = serde_ignored::Deserializer::new(config, &mut ignored_cback);

        let mut result: Result<_, AnyError> =
            serde_path_to_error::deserialize(config).map_err(|e| {
                let ctx = format!("Failed to decode configuration at {}", e.path());
                e.into_inner().context(ctx).into()
            });

        if !external_cfg {
            result = result
                .context("No config passed to application")
                .map_err(AnyError::from)
        }

        Ok(result?)
    }
}

#[cfg(test)]
mod tests {
    use maplit::hashmap;
    use serde::Deserialize;

    use super::*;
    use crate::Empty;

    #[test]
    fn enum_keys() {
        #[derive(Debug, Deserialize, Eq, PartialEq, Hash)]
        #[serde(rename_all = "kebab-case")]
        enum Key {
            A,
            B,
        }

        #[derive(Debug, Deserialize, Eq, PartialEq)]
        #[serde(rename_all = "kebab-case")]
        struct Cfg {
            map: HashMap<Key, String>,
        }

        const CFG: &str = r#"
            [map]
            a = "hello"
            b = "world"
        "#;

        let cfg: Cfg = Builder::new()
            .config_defaults(CFG)
            .build_no_opts()
            .load()
            .unwrap();

        assert_eq!(
            cfg,
            Cfg {
                map: hashmap! {
                    Key::A => "hello".to_owned(),
                    Key::B => "world".to_owned(),
                }
            }
        );
    }

    #[test]
    fn usize_key() {
        #[derive(Debug, Deserialize, Eq, PartialEq)]
        #[serde(rename_all = "kebab-case")]
        struct Cfg {
            map: HashMap<usize, String>,
        }

        const CFG: &str = r#"
            [map]
            1 = "hello"
            "2" = "world"
        "#;

        let cfg: Cfg = Builder::new()
            .config_defaults(CFG)
            .build_no_opts()
            .load()
            .unwrap();

        assert_eq!(
            cfg,
            Cfg {
                map: hashmap! {
                    1 => "hello".to_owned(),
                    2 => "world".to_owned(),
                }
            }
        );
    }

    #[test]
    fn str_to_int() {
        #[derive(Debug, Deserialize, Eq, PartialEq)]
        #[serde(rename_all = "kebab-case")]
        struct Cfg {
            value: usize,
        }

        // Here the value is encoded as string. The config crate should handle that.
        // This happens for example when we do overrides from command line.
        const CFG: &str = r#"value = "42""#;

        let cfg: Cfg = Builder::new()
            .config_defaults(CFG)
            .build_no_opts()
            .load()
            .unwrap();

        assert_eq!(cfg, Cfg { value: 42 });
    }

    #[test]
    fn cmd_overrides() {
        #[derive(Debug, Deserialize, Eq, PartialEq)]
        #[serde(rename_all = "kebab-case")]
        struct Cfg {
            value: usize,
        }

        #[derive(Debug, Eq, PartialEq, StructOpt)]
        struct Opts {
            #[structopt(short = "o")]
            option: bool,
        }

        const CFG: &str = r#"
            value = 42
        "#;

        let (opts, mut loader): (Opts, Loader) = Builder::new()
            .config_defaults(CFG)
            .build_explicit_opts(vec!["my-app", "-o", "-C", "value=12"])
            .unwrap();

        assert_eq!(opts, Opts { option: true });

        let cfg: Cfg = loader.load().unwrap();

        assert_eq!(cfg, Cfg { value: 12 });
    }

    #[test]
    fn combine_dir() {
        #[derive(Debug, Deserialize, Eq, PartialEq)]
        struct Cfg {
            value: usize,
            option: bool,
            another: String,
        }

        const CFG: &str = r#"
            value = 42
            another = "Hello"
        "#;

        let (Empty {}, mut loader) = Builder::new()
            .config_supported_exts()
            .config_defaults(CFG)
            .build_explicit_opts(vec!["my-app", "tests/data"])
            .unwrap();

        let cfg: Cfg = loader.load().unwrap();
        assert_eq!(
            cfg,
            Cfg {
                value: 12,                   // Overridden from tests/data/cfg1.yaml
                option: true,                // From tests/data/cfg2.toml
                another: "Hello".to_owned(), // From the defaults
            }
        );
    }
}