settings_loader 1.0.0

Opinionated configuration settings load mechanism for Rust applications
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
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
use std::fmt::Debug;
use std::path::{Path, PathBuf};

use config::builder::DefaultState;
use config::ConfigBuilder;
use path_absolutize::*;
use serde::de::DeserializeOwned;

use crate::environment::Environment;
use crate::error::SettingsError;
#[cfg(feature = "metadata")]
use crate::introspection::SettingsIntrospection;
use crate::loading_options::LoadingOptions;

type ConfigFile = config::File<config::FileSourceFile, config::FileFormat>;

/// The main driver for loading application settings from multiple sources.
///
/// The `SettingsLoader` trait defines the core logic for composing configuration
/// sources into a unified representation for an application. It integrates settings
/// from configuration files, environment variables, secrets files, and command-line
/// arguments.
///
/// This trait is typically implemented for a settings structure that represents
/// the application's full configuration. The `load()` function orchestrates the
/// loading process, using `LoadingOptions` to determine available sources and
/// their precedence.
///
/// # Configuration Sources
///
/// The `SettingsLoader` trait loads settings from multiple sources, in the following
/// order of precedence (highest to lowest):
///
/// 1. **CLI Option Overrides** – Explicit settings provided via command-line arguments.
/// 2. **Environment Variables** – Overrides defined in the system environment.
/// 3. **Secrets File** – A dedicated configuration file for storing sensitive values.
/// 4. **Explicit Configuration File** – A user-defined settings file.
/// 5. **Implicit Configuration Files** – Default configurations inferred based on environment.
///
/// # Example Usage
///
/// ```rust,ignore
/// use serde::Deserialize;
/// use settings_loader::SettingsLoader;
///
/// #[derive(Debug, Deserialize)]
/// struct Settings {
///     pub http_api: HttpApiSettings,
///     pub database: DatabaseSettings,
/// }
///
/// impl SettingsLoader for Settings {
///     type Options = CliOptions;
/// }
///
/// let options = CliOptions::parse();
/// let settings = Settings::load(&options).expect("Failed to load settings");
/// println!("{:?}", settings);
/// ```
///
pub trait SettingsLoader: Debug + Sized {
    /// The options type that specifies how settings are loaded.
    type Options: LoadingOptions + Debug;

    /// Returns the default directory where configuration resources are stored.
    ///
    /// By default, this function returns `"resources"`. Implementations may override this method
    /// to provide an application-specific configuration directory.
    fn resources_home() -> PathBuf {
        PathBuf::from("resources")
    }

    /// Returns the base name for the application's primary configuration file.
    ///
    /// Defaults to `"application"`, meaning configuration files are expected to follow a naming
    /// pattern like `application.yaml`, `application.json`, etc.
    fn app_config_basename() -> &'static str {
        "application"
    }

    /// Returns the prefix used for environment variable-based configuration.
    ///
    /// Defaults to `"app"`, meaning environment variables should be prefixed with `APP_`
    /// (e.g., `APP_DATABASE_HOST`).
    fn environment_prefix() -> &'static str {
        "app"
    }

    /// Returns the separator used in environment variable keys.
    ///
    /// Defaults to `"__"`, meaning nested settings should be represented as
    /// `APP_DATABASE__HOST` instead of `APP_DATABASE_HOST`.
    fn environment_path_separator() -> &'static str {
        "__"
    }

    /// Loads application settings by composing multiple configuration sources.
    ///
    /// This function orchestrates the loading process, combining various sources
    /// in a defined order of precedence:
    ///     1. CLI option overrides,
    ///     2. environment variables,
    ///     3. secrets file
    ///     4. explicit application configuration file or implicitly loaded application
    ///        configuration with environment file overrides.
    ///
    /// If an explicit configuration file is provided via CLI options, it is loaded directly.
    /// Otherwise, implicit search paths and environment-based configurations are used.
    ///
    /// # Errors
    /// Returns a `SettingsError` if any part of the configuration loading process fails.
    ///
    /// # Instrumentation
    /// This function includes `tracing::instrument` logging to track the settings loading process.
    #[tracing::instrument(level = "info")]
    fn load(options: &Self::Options) -> Result<Self, SettingsError>
    where
        Self: DeserializeOwned,
    {
        let initial_builder = crate::layer::LayerBuilder::new();
        let builder = options.build_layers(initial_builder);

        if builder.has_layers() {
            let config_builder = builder.build()?;
            let config = config_builder.build()?;
            return config.try_deserialize().map_err(Into::into);
        }

        Self::load_implicit(options)
    }

    /// Loads application settings and returns a `SourceMap` containing provenance information.
    #[tracing::instrument(level = "info")]
    fn load_with_provenance(options: &Self::Options) -> Result<(Self, crate::provenance::SourceMap), SettingsError>
    where
        Self: DeserializeOwned,
    {
        let initial_builder = crate::layer::LayerBuilder::new();
        let builder = options.build_layers(initial_builder);

        let (config_builder, source_map) = builder.build_with_provenance()?;
        let config = config_builder.build()?;
        let settings = config.try_deserialize()?;

        Ok((settings, source_map))
    }

    /// Loads application settings and returns a `ConfigEditor` for modifying them.
    ///
    /// This method uses the same provenance tracking as the regular load process
    /// to initialize an editor that knows where each setting came from.
    #[cfg(feature = "editor")]
    #[tracing::instrument(level = "info")]
    fn load_with_editor(options: &Self::Options) -> Result<(Self, crate::editor::ConfigEditor), SettingsError>
    where
        Self: DeserializeOwned,
    {
        let initial_builder = crate::layer::LayerBuilder::new();
        let builder = options.build_layers(initial_builder);

        let (config_builder, source_map) = builder.build_with_provenance()?;
        let config = config_builder.build()?;
        let settings = config.try_deserialize()?;

        let mut editor = crate::editor::ConfigEditor::new(source_map);

        // If a config path is explicitly provided, it's a good candidate for default target
        if let Some(path) = options.config_path() {
            editor.set_default_target(path);
        }

        Ok((settings, editor))
    }

    /// Loads application settings by composing multiple configuration sources.
    ///
    /// This function orchestrates the loading process, combining various sources
    /// in a defined order of precedence:
    ///     1. CLI option overrides,
    ///     2. environment variables,
    ///     3. secrets file
    ///     4. explicit application configuration file or implicitly loaded application
    ///        configuration with environment file overrides.
    ///
    /// If an explicit configuration file is provided via CLI options, it is loaded directly.
    /// Otherwise, implicit search paths and environment-based configurations are used.
    ///
    /// # Errors
    /// Returns a `SettingsError` if any part of the configuration loading process fails.
    ///
    /// # Instrumentation
    /// This function includes `tracing::instrument` logging to track the settings loading process.
    #[tracing::instrument(level = "info")]
    fn load_implicit(options: &Self::Options) -> Result<Self, SettingsError>
    where
        Self: DeserializeOwned,
    {
        let mut builder = config::Config::builder();
        match options.config_path() {
            Some(ref path) => {
                builder = builder.add_source(Self::make_explicit_config_source(path));
            },
            None => {
                tracing::info!(?options, "loading settings based on CLI options and environment.");
                let mut resource_dirs = Vec::default();
                for dir in options.implicit_search_paths() {
                    resource_dirs.push(dir.absolutize()?.into_owned());
                }
                if resource_dirs.is_empty() {
                    tracing::info!("no resource directories specified, using default.");
                    resource_dirs.push(Self::default_resource_path());
                }

                builder = builder.add_source(Self::make_implicit_config_source(
                    Self::app_config_basename(),
                    &resource_dirs,
                ));

                if let Some(env) = options.environment() {
                    for source in Self::make_environment_sources(env, &resource_dirs) {
                        builder = builder.add_source(source);
                    }
                }
            },
        }

        if let Some(ref secrets) = options.secrets_path() {
            let abs_secrets = secrets.absolutize()?;
            builder = builder.add_source(Self::make_secrets_source(&abs_secrets));
        }

        builder = builder.add_source(Self::make_environment_variables_source());

        builder = options
            .load_overrides(builder)
            .map_err(|err| SettingsError::CliOption(err.into()))?;

        let config = builder.build()?;
        tracing::info!(?config, "configuration loaded");
        let settings = config.try_deserialize()?;
        tracing::info!(?settings, "settings built for application.");
        Ok(settings)
    }

    /// Returns the default path to the resources directory.
    fn default_resource_path() -> PathBuf {
        let current_dir = std::env::current_dir().expect("failed to get current directory");
        current_dir.join(Self::resources_home())
    }

    /// Constructs a glob walker for searching configuration files in a directory.
    ///
    /// If the walker fails to initialize, an error is logged, and `None` is returned.
    fn make_glob_walker(base_dir: impl AsRef<Path>, pattern: impl AsRef<str>) -> Option<globwalk::GlobWalker> {
        globwalk::GlobWalkerBuilder::new(base_dir.as_ref(), pattern.as_ref())
            .build()
            .map_err(|err| {
                tracing::warn!(
                    error=?err,
                    "failed to build glob walker for base-directory:{:?} pattern:{}",
                    base_dir.as_ref(), pattern.as_ref()
                );
                err
            })
            .ok()
    }

    /// Searches for a configuration resource file in the given directories.
    ///
    /// Returns the first directory where the resource file is found.
    fn find_resource_dir(resource: &str, dirs: &[PathBuf]) -> Option<PathBuf> {
        for d in dirs.iter() {
            let walker = Self::make_glob_walker(d, format!("{}.*", resource));
            let walker = match walker {
                Some(w) => w,
                None => continue,
            };

            let found = walker
                .into_iter()
                .filter_map(|entry| {
                    tracing::info!("found settings entry: {entry:?}");
                    Result::ok(entry)
                })
                .next()
                .is_some();
            if found {
                tracing::info!("found settings {resource} file in base-directory:{d:?}");
                return Some(d.clone());
            }
        }

        tracing::warn!("no settings resource found for {resource} in {dirs:?}");
        None
    }

    /// Creates a configuration source for an explicitly specified settings file.
    ///
    /// This method is used when the user provides a configuration file path via CLI.
    fn make_explicit_config_source(path: &Path) -> ConfigFile {
        ConfigFile::from(path).required(true)
    }

    /// Creates a configuration source for implicitly loaded application settings.
    ///
    /// The source file is determined by searching the provided directories for a file
    /// matching the `app_config_basename()` (e.g., `application.yaml`).
    fn make_implicit_config_source(basename: &str, dir_paths: &[PathBuf]) -> ConfigFile {
        let source_dir = Self::find_resource_dir(basename, dir_paths)
            // .cloned()
            .unwrap_or_else(Self::default_resource_path);

        let path = source_dir.join(basename);
        ConfigFile::from(path).required(true)
    }

    /// Generates a list of environment-specific configuration sources.
    ///
    /// This function looks for files named after the environment (e.g., `production.yaml`)
    /// in the provided search directories.
    fn make_environment_sources(environment: Environment, dir_paths: &[PathBuf]) -> Vec<ConfigFile> {
        dir_paths
            .iter()
            .rev()
            .map(|dir| Self::make_app_environment_source(&environment, dir))
            .collect()
    }

    /// Creates a configuration source for an environment-specific settings file.
    fn make_app_environment_source(environment: &Environment, resources: &Path) -> ConfigFile {
        tracing::info!("creating application {environment} settings source at {:?}", resources);
        let env_path = resources.join(environment.as_ref());
        ConfigFile::from(env_path).required(false)
    }

    // Creates a configuration source for a secrets file.
    ///
    /// The secrets file contains sensitive credentials such as database passwords.
    fn make_secrets_source(secrets_path: &Path) -> ConfigFile {
        if secrets_path.exists() {
            tracing::info!("adding secrets override configuration source at {:?}", secrets_path);
        } else {
            tracing::error!("cannot find secrets override configuration at {:?}", secrets_path);
        }
        ConfigFile::from(secrets_path).required(true)
    }

    /// Creates a configuration source that pulls settings from environment variables.
    ///
    /// The environment variables must follow the prefix and separator rules defined
    /// by `environment_prefix()` and `environment_path_separator()`.
    fn make_environment_variables_source() -> config::Environment {
        let prefix = Self::environment_prefix();
        let delim = Self::environment_path_separator();
        let config_env = config::Environment::with_prefix(prefix).separator(delim);
        tracing::info!("loading environment variables with: {:?}", config_env);
        config_env
    }

    /// Adds an environment-specific configuration source to the settings builder.
    #[tracing::instrument(level = "info", skip(config))]
    fn add_app_environment_source(
        config: ConfigBuilder<DefaultState>, env: Environment, resources_path: &Path,
    ) -> ConfigBuilder<DefaultState> {
        let env_config_path = resources_path.join(env.as_ref());
        tracing::debug!("looking for {} configu at {:?}", env, resources_path);
        config.add_source(config::File::from(env_config_path).required(false))
    }

    /// Adds a secrets file source to the settings builder.
    #[tracing::instrument(level = "info", skip(config))]
    fn add_secrets_source(
        config: ConfigBuilder<DefaultState>, secrets_path: Option<PathBuf>,
    ) -> ConfigBuilder<DefaultState> {
        if let Some(path) = secrets_path {
            tracing::debug!(
                "looking for secrets configuration at: {:?} -- exists:{}",
                path,
                path.as_path().exists()
            );
            if path.as_path().exists() {
                tracing::info!("adding secrets override configuration source from {:?}", path);
            } else {
                tracing::error!("cannot find secrets override configuration at {:?}", path);
            }
            config.add_source(config::File::from(path).required(true))
        } else {
            config
        }
    }

    /// Adds environment variables as a source to the settings builder.
    #[tracing::instrument(level = "info", skip(config))]
    fn add_environment_variables_source(config: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
        let config_env =
            config::Environment::with_prefix(Self::environment_prefix()).separator(Self::environment_path_separator());
        tracing::info!("loading environment variables with prefix: {:?}", config_env);
        config.add_source(config_env)
    }

    // ========================================================================
    // SCHEMA & DOCUMENTATION EXPORT
    // ========================================================================

    /// Export the application's configuration schema as JSON Schema.
    ///
    /// This uses the metadata registered in the global registry.
    #[cfg(feature = "metadata")]
    fn export_json_schema(path: &Path) -> Result<(), SettingsError> {
        crate::registry::global_schema().export_json_schema(path)
    }

    /// Export the application's configuration documentation as HTML.
    ///
    /// This uses the metadata registered in the global registry.
    #[cfg(feature = "metadata")]
    fn export_docs(path: &Path) -> Result<(), SettingsError> {
        crate::registry::global_schema().export_docs(path)
    }

    /// Export an example configuration file with defaults and descriptions.
    ///
    /// This uses the metadata registered in the global registry.
    #[cfg(feature = "metadata")]
    fn export_example_config(path: &Path) -> Result<(), SettingsError> {
        crate::registry::global_schema().export_example_config(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::loading_options::APP_ENVIRONMENT;
    use crate::{environment, NoOptions};
    use assert_matches2::{assert_let, assert_matches};
    use config::{Config, FileFormat};
    use pretty_assertions::assert_eq;
    use serde::{Deserialize, Serialize};
    use serde_with::{serde_as, DisplayFromStr};

    #[derive(Debug, PartialEq, Eq)]
    struct TestOptions(String, Option<Environment>);

    impl LoadingOptions for TestOptions {
        type Error = SettingsError;

        fn config_path(&self) -> Option<PathBuf> {
            None
        }

        fn secrets_path(&self) -> Option<PathBuf> {
            Some(PathBuf::from("./resources/secrets.yaml"))
        }

        fn environment_override(&self) -> Option<Environment> {
            self.1.clone()
        }

        #[tracing::instrument(level = "info", skip(config))]
        fn load_overrides(
            &self, config: ConfigBuilder<DefaultState>,
        ) -> Result<ConfigBuilder<DefaultState>, Self::Error> {
            Ok(config.set_override("foo", self.0.as_str())?)
        }

        fn implicit_search_paths(&self) -> Vec<PathBuf> {
            vec!["./tests/override".into(), "resources".into()]
        }
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TestSettings {
        pub application: TestHttpSettings,
        pub database: TestDbSettings,
        pub foo: String,
    }

    impl SettingsLoader for TestSettings {
        type Options = TestOptions;
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TestSettingsNoOpts {
        pub application: TestHttpSettings,
        pub database: TestDbSettings,
        pub foo: String,
    }

    impl SettingsLoader for TestSettingsNoOpts {
        type Options = NoOptions;
    }

    #[serde_as]
    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TestDbSettings {
        pub username: String,
        pub password: String,
        #[serde_as(as = "DisplayFromStr")]
        pub port: u16,
        pub host: String,
        #[serde(rename = "name")]
        pub database_name: String,
        pub require_ssl: bool,
    }

    #[serde_as]
    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TestHttpSettings {
        pub host: String,
        #[serde_as(as = "DisplayFromStr")]
        pub port: u16,
    }

    #[test]
    fn test_load_string_settings() -> anyhow::Result<()> {
        with_env_vars(
            "test_load_string_settings",
            vec![(APP_ENVIRONMENT, None)], // Some("local"))],
            || {
                eprintln!("+ test_load_string_settings");
                Lazy::force(&TEST_TRACING);
                let main_span = tracing::info_span!("test_load_string_settings");
                let _ = main_span.enter();

                assert_matches!(env::var(APP_ENVIRONMENT), Err(_));
                tracing::info!("envar: {} = {:?}", APP_ENVIRONMENT, env::var(APP_ENVIRONMENT));

                let config = Config::builder().add_source(config::File::from_str(
                    r###"
                    | application:
                    |   port: 8000
                    |   host: 10.1.2.57
                    |   base_url: "http://10.1.2.57"
                    | database:
                    |   username: postgres
                    |   password: password
                    |   port: 5432
                    |   host: "localhost"
                    |   name: "propensity"
                    |   require_ssl: true
                "###
                    .trim_margin_with("| ")
                    .unwrap()
                    .as_str(),
                    FileFormat::Yaml,
                ));

                let options = TestOptions("bar".to_string(), Some(environment::LOCAL.clone()));

                assert_let!(Ok(config) = options.load_overrides(config));
                assert_let!(Ok(config) = config.build());
                tracing::info!(?config, "eligibility config loaded.");

                assert_let!(Ok(actual) = config.try_deserialize::<TestSettings>());
                assert_eq!(
                    actual,
                    TestSettings {
                        application: TestHttpSettings {
                            port: 8000,
                            host: "10.1.2.57".to_string(),
                            // base_url: "http://10.1.2.57".to_string(),
                        },
                        database: TestDbSettings {
                            username: "postgres".to_string(),
                            password: "password".to_string(),
                            port: 5432,
                            host: "localhost".to_string(),
                            database_name: "propensity".to_string(),
                            require_ssl: true,
                        },
                        foo: "bar".to_string(),
                    }
                );
            },
        );
        eprintln!("- test_load_string_settings");
        Ok(())
    }

    #[test]
    fn test_settings_load_w_options() -> anyhow::Result<()> {
        with_env_vars(
            "test_settings_load_w_options",
            vec![
                (APP_ENVIRONMENT, Some("local")),
                ("APP__APPLICATION__PORT", Some("80")),
                ("APP__DATABASE__PASSWORD", Some("my voice is my password")),
                ("APP__DATABASE__PORT", Some("1111")),
            ],
            || {
                eprintln!("+ test_settings_load_w_options");
                Lazy::force(&TEST_TRACING);
                let main_span = tracing::info_span!("test_settings_load_w_options");
                let _ = main_span.enter();

                assert_matches!(env::var(APP_ENVIRONMENT), Ok(actual));
                assert_eq!(actual, "local");
                tracing::info!("envar: {} = {:?}", APP_ENVIRONMENT, std::env::var(APP_ENVIRONMENT));

                assert_let!(Ok(actual) = TestSettings::load(&TestOptions("zed".to_string(), None)));

                let expected: TestSettings = TestSettings {
                    application: TestHttpSettings {
                        port: 80,
                        host: "127.0.0.1".to_string(),
                        // base_url: "http://127.0.0.1".to_string(),
                    },
                    database: TestDbSettings {
                        username: "postgres".to_string(),
                        password: "my voice is my password".to_string(),
                        port: 1111,
                        host: "localhost".to_string(),
                        database_name: "local_db".to_string(),
                        require_ssl: false,
                    },
                    foo: "zed".to_string(),
                };

                assert_eq!(actual, expected);
            },
        );
        eprintln!("- test_settings_load_w_options");
        Ok(())
    }

    #[test]
    fn test_settings_load_w_no_options() -> anyhow::Result<()> {
        eprintln!("+ test_settings_load_w_no_options");
        Lazy::force(&TEST_TRACING);
        let main_span = tracing::info_span!("test_settings_load_w_no_options");
        let _ = main_span.enter();

        with_env_vars(
            "test_settings_load_w_no_options",
            vec![(APP_ENVIRONMENT, Some("production"))],
            || {
                assert_matches!(env::var(APP_ENVIRONMENT), Ok(actual));
                assert_eq!(actual, "production");
                tracing::info!("envar: {} = {:?}", APP_ENVIRONMENT, std::env::var(APP_ENVIRONMENT));

                assert_let!(Ok(actual) = TestSettingsNoOpts::load(&()));

                let expected = TestSettingsNoOpts {
                    application: TestHttpSettings {
                        port: 8000,
                        host: "0.0.0.0".to_string(),
                        // base_url: "http://127.0.0.1".to_string(),
                    },
                    database: TestDbSettings {
                        username: "postgres".to_string(),
                        password: "resources".to_string(),
                        port: 5432,
                        host: "localhost".to_string(),
                        database_name: "default_db".to_string(),
                        require_ssl: false,
                    },
                    foo: "without_options".to_string(),
                };

                assert_eq!(actual, expected);
            },
        );
        eprintln!("- test_settings_load_w_no_options");
        Ok(())
    }

    #[test]
    fn test_settings_load_w_override() -> anyhow::Result<()> {
        eprintln!("+ test_settings_load_w_override");
        Lazy::force(&TEST_TRACING);
        let main_span = tracing::info_span!("test_settings_load_w_override");
        let _ = main_span.enter();

        with_env_vars(
            "test_settings_load_w_override",
            vec![(APP_ENVIRONMENT, Some("production"))],
            || {
                assert_matches!(env::var(APP_ENVIRONMENT), Ok(actual));
                assert_eq!(actual, "production");
                tracing::info!("envar: {} = {:?}", APP_ENVIRONMENT, std::env::var(APP_ENVIRONMENT));

                assert_let!(Ok(actual) = TestSettings::load(&TestOptions("zed".to_string(), None)));

                let expected = TestSettings {
                    application: TestHttpSettings {
                        port: 8000,
                        host: "127.0.0.1".to_string(),
                        // base_url: "http://127.0.0.1".to_string(),
                    },
                    database: TestDbSettings {
                        username: "postgres".to_string(),
                        password: "password".to_string(),
                        port: 5432,
                        host: "localhost".to_string(),
                        database_name: "override".to_string(),
                        require_ssl: false,
                    },
                    foo: "zed".to_string(),
                };

                assert_eq!(actual, expected);
            },
        );
        eprintln!("- test_settings_load_w_no_options");
        Ok(())
    }

    use std::env::VarError;
    use std::panic::{RefUnwindSafe, UnwindSafe};
    use std::sync::Mutex;
    use std::{env, panic};

    use once_cell::sync::Lazy;
    use trim_margin::MarginTrimmable;

    use crate::tracing::TEST_TRACING;

    static SERIAL_TEST: Lazy<Mutex<()>> = Lazy::new(Default::default);

    /// Sets environment variables to the given value for the duration of the closure.
    /// Restores the previous values when the closure completes or panics, before unwinding the
    /// panic.
    pub fn with_env_vars<F>(label: &str, kvs: Vec<(&str, Option<&str>)>, closure: F)
    where
        F: Fn() + UnwindSafe + RefUnwindSafe,
    {
        let guard = SERIAL_TEST.lock().unwrap();
        let mut old_kvs: Vec<(&str, Result<String, VarError>)> = Vec::new();
        for (k, v) in kvs {
            let old_v = env::var(k);
            old_kvs.push((k, old_v));
            match v {
                None => env::remove_var(k),
                Some(v) => env::set_var(k, v),
            }
        }
        eprintln!("W_ENV[{}]: OLD_KVS: {:?}", label, old_kvs);
        let old_kvs_2 = old_kvs.clone();

        match panic::catch_unwind(|| {
            closure();
        }) {
            Ok(_) => {
                eprintln!("W_END[{}]: OK - resetting env to: {:?}", label, old_kvs);
                for (k, v) in old_kvs {
                    reset_env(k, v);
                }
            },
            Err(err) => {
                eprintln!("W_END[{}]: Err - resetting env to: {:?}", label, old_kvs);
                for (k, v) in old_kvs {
                    reset_env(k, v);
                }
                drop(guard);
                panic::resume_unwind(err);
            },
        };
        for (k, v) in old_kvs_2 {
            eprintln!(
                "W_END[{}] RESET ACTUAL: {:?}:{:?} expected:{:?}",
                label,
                k,
                env::var(k),
                v
            );
        }
    }

    fn reset_env(k: &str, old: Result<String, VarError>) {
        if let Ok(v) = old {
            env::set_var(k, v);
        } else {
            env::remove_var(k);
        }
    }
}