Derive Macro Configuration

Source
#[derive(Configuration)]
{
    // Attributes available to this derive:
    #[properties]
}
Expand description

为结构体生成一个config方法,调用该方法可以读取配置文件中的属性

§Example

application.yml文件的内容为

"
push:
  file:
    enable: 1
    appId:  4ULDFh55cx9uM
    appKey: MPRaknd2Q49hgE1
    masterSecret: esEDaOfvvi5aD
    packageName:  www.baidu.com
"

rust代码

r#"
use config_macro::Configuration;
use serde::Deserialize;

#[derive(Debug, Deserialize, Configuration)]
#[properties(prefix = "push.file", file = "application.yml")]
#[serde(rename_all(deserialize = "camelCase"))]
struct PushFile {
    enable: u8,
    app_id: String,
    app_key: String,
    master_secret: String,
    package_name: String,
}

let config = PushFile::config();
"#

#[properties]可以配置的属性

  • prefix: 读取配置文件的前缀
  • file: 配置文件的路径(可以配置多个file属性)
  • environment: 是否从环境变量中读取,默认为true