#config-file #json #map #memory #json-format #file-read #format-file

confmap

一个将配置文件读取到内存映射中的库。其理念与 golang 中的 viper 包相同

4 个稳定版本

1.0.3 2023 年 8 月 15 日

#2611解析器实现

MIT/Apache

17KB
308

confmap
A library for reading config file into a map in memory.
This library is based on serde_json and once_cell.
after the config file is read, you can easily get the config by 
 using get_string, get_int64, get_bool...
This library is created because I cannot find a library like this 
 in rust. (the idea is the same to viper package in golang)

example: 
put a json format file in your project folder like this:
        config.json
        {
            "testGetString": "YesMan",
            "testGetInt64": 43,
            "testGetStringArray": [
                "+44 1234567",
                "+44 2345678"
            ]
        }
add dependency in Cargo.toml:
[dependencies]
confmap = "1.0.0"

in your project main.rs:
use confmap;

fn main() {
    // if you don't call add_config_path method or the path_str is not exist, 
    // it will scan the folder where the executable file is located.
    confmap::add_config_path(path_str); 
    confmap::set_config_name("config.json");
    confmap::read_config();
    assert_eq!(Some("YesMan".to_string()), confmap::get_string("testGetString"));
    assert_eq!(Some(43), confmap::get_int64("testGetInt64"));
    assert_eq!(Some(vec!["+44 1234567".to_string(), "+44 2345678".to_string()]), 
               confmap::get_string_array("testGetStringArray"));
}

依赖

~0.4–0.8MB
~17K SLoC