config-plus

Crates.ioconfig-plus
lib.rsconfig-plus
version0.2.2
created_at2025-06-23 09:02:02.137451+00
updated_at2025-12-31 07:55:48.208566+00
description对config库的增强
homepage
repositoryhttps://gitee.com/itao007/config-plus.git
max_upload_size
id1722646
size58,303
itao (bj-sjt)

documentation

https://docs.rs/config-plus

README

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

#[derive(Debug, Deserialize, Configuration)]
#[config(prefix = "push.file", file(path = "application.yml", require = true), env(name = "CONFIG_PATH", require = true))]

#[config]可以配置的属性

  • prefix: 读取配置文件的前缀
  • file: 配置文件的路径(可以配置多个file属性)
    • path: 文件路径
    • require: 文件是否必须(默认false)
    • format: 非必填(默认为文件名后缀),返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
  • env: 通过指定环境变量来指定文件路径(可以配置多个env属性)
    • name: 环境变量名(如果通过name获取不到值则什么都不做)
    • require: 文件是否必须(默认false)
    • format: 非必填(默认为文件名后缀),返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
  • http: 配置http属性(可以配置多个http属性)
    • url: http的url
    • method: 请求方法(只支持get和post两种, 默认是get)
    • format: 必填,返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
  • environment: 是否从环境变量中读取,默认为true

优先级: http > environment > env > file

示例

#[derive(Debug, Deserialize, Configuration)]
#[config(
  prefix = "push.file",
  file(path = "application.yml", require, format = "yml"),
  env(name = "CONFIG_PATH"),
  http(url = "http://127.0.0.1:8080/toml", format = "toml"),
  environment = true
)]
 #[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();
Commit count: 0

cargo fmt