config_plus/
lib.rs

1//! 为结构体生成一个`config`方法,调用该方法可以读取配置文件中的属性
2//! 使用方式
3//! `#[derive(Debug, Deserialize, Configuration)]`
4//! `#[config(prefix = "push.file", file(path = "application.yml", require = true)]`
5//! 
6//! `#[config]`可以配置的属性
7//! - prefix: 读取配置文件的前缀
8//! - file: 配置文件的路径(可以配置多个file属性)
9//!   + path: 文件路径
10//!   + require: 文件是否必须(默认false)
11//!   + format: 非必填(默认为文件名后缀),返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
12//! - env: 通过指定环境变量来指定文件路径(可以配置多个env属性)
13//!   + name: 环境变量名(如果通过name获取不到值则什么都不做)
14//!   + require: 文件是否必须(默认false)
15//!   + format: 非必填(默认为文件名后缀),返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
16//! - http: 配置http属性(可以配置多个http属性)
17//!   + url: http的url
18//!   + method: 请求方法(只支持get和post两种)
19//!   + format: 必填,返回的字符串的格式(支持 toml、json、yaml、yml、json5、ini、ron)
20//! - environment: 是否从环境变量中读取,默认为true
21//! > 优先级: http > environment > env > file
22
23mod http;
24
25use config::{FileFormat, FileSourceFile};
26pub use http::*;
27pub use config_plus_macro::*;
28
29
30pub fn convert_format(format: &str) -> FileFormat {
31    match format {
32        "toml" => FileFormat::Toml,
33        "json" => FileFormat::Json,
34        "yaml" => FileFormat::Yaml,
35        "ini" => FileFormat::Ini,
36        "ron" => FileFormat::Ron,
37        "json5" => FileFormat::Json5,
38        _ => unreachable!("不支持格式"),
39    }
40}
41
42
43pub fn get_file(path: &str, format: &str) -> File<FileSourceFile, FileFormat> {
44    let format = convert_format(format);
45    File::new(path, format)
46}