#configuration #settings

macro config-source

Macro for deriving config::Source (config-rs) trait

2 releases

0.1.1 Feb 8, 2023
0.1.0 Jan 26, 2023

#179 in #settings

29 downloads per month

MIT license

7KB
67 lines

config-source

Macro for deriving config-rs's config::Source trait.

Usage

[dependencies]
config = "0.13"
config-source = "0.1"

More

See the documentation for more usage information.


lib.rs:

config-source

Macro for deriving config-rs's config::Source trait.

Usage

To derive config::Source for a struct, simply add the #[derive(ConfigSource)] attribute to the struct:

#[derive(serde::Deserialize, config_source::ConfigSource, Clone, Debug)]
pub struct MyConfig {
    pub question: String,
    pub answer: u64,
}

impl Default for MyConfig {
    fn default() -> Self {
        Self {
            question: String::from("The Ultimate Question of Life, the Universe, and Everything"),
            answer: 42,
        }
    }
}

Then, you can use the config::Config struct to load your configuration using a default value:

#
#
let config = config::Config::builder()
    .add_source(MyConfig::default()) // Default value as source!
    .add_source(config::File::with_name("my_config.toml").required(false))
    .add_source(config::Environment::with_prefix("MY_CONFIG").separator("__"))
    .build()
    .expect("Failed to build `MyConfig`");

Dependencies

~2MB
~42K SLoC