2 个版本
| 0.1.1 | 2023年2月8日 | 
|---|---|
| 0.1.0 | 2023年1月26日 | 
#179 in #settings
每月 29 次下载
7KB
67 行
config-source
推导 config-rs 的 config::Source 特性的宏。
使用方法
[dependencies]
config = "0.13"
config-source = "0.1"
更多
有关更多使用信息,请参阅文档。
lib.rs:
config-source
推导 config-rs 的 config::Source 特性的宏。
使用方法
要为结构体推导 config::Source,只需将 #[derive(ConfigSource)] 属性添加到结构体中
#[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,
        }
    }
}
然后,您可以使用 config::Config 结构体使用默认值加载您的配置
#
#
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`");
依赖项
~2MB
~42K SLoC