4个版本
0.1.3 | 2023年2月2日 |
---|---|
0.1.2 | 2023年1月30日 |
0.1.1 | 2023年1月30日 |
0.1.0 | 2023年1月30日 |
#676 in 配置
22KB
118 代码行
RSCONFIG
一个简单的配置库,帮助程序管理配置。
导入
如果只使用RSCONFIG,可以这样做
[dependencies]
rsconfig = "0.1.3" # replace with latest version
示例
命令行配置
use rsconfig::CommandlineConfig;
use std::env;
// our config class that we can expand upon to add different values
// to expand upon it, simply add more fields and update the import function(s)
#[derive(Debug)]
struct TestConfig {
test: bool
}
impl CommandlineConfig for TestConfig {
fn from_env_args(args: Vec<String>) -> Self {
// check if commandline args contains --test
Self { test: args.contains(&"--test".to_string()) }
}
}
fn main() {
// fetch commandline args
let args: Vec<String> = env::args().collect();
// load config from commandline args
let mut config = TestConfig::from_env_args(args);
// should output TestConfig { test: true } if --test is in the command
// otherwise, it will print TestConfig { test: false }
println!("{:?}", config);
// you can change the value of the config
config.test = !config.test;
}
Yaml配置
// import YamlConfig and files
// files has some useful load functions
use rsconfig::YamlConfig;
use rsconfig::files;
use yaml_rust;
use std::fs;
// our config class that we can expand upon to add different values
// to expand upon it, simply add more fields and update the import function(s)
#[derive(Debug)]
struct TestConfig {
test: bool
}
impl YamlConfig for TestConfig {
fn from_yaml(yaml: Vec<yaml_rust::Yaml>) -> Self {
// fetch "test" value of the first yaml document using yaml_rust crate
// NOTE: this code is not error-safe, will panic if the correct file formatting is not used
Self { test: *&yaml[0]["test"].as_bool().unwrap() }
}
fn save_yaml(&self, path: &str) -> Result<()> {
// might want to do this differently for config with more fields
let mut data = "test: ".to_string();
// add the value to the file data
data.push_str(self.test.to_string().as_str());
// write to the file
fs::write(path, data).unwrap();
// return an Ok result
// required because fs::write could fail, which would pass on an Err(()).
Ok(())
}
}
fn main() {
/*
NOTE: for a situation where you don't know the filetype,
you can impl FileConfig and use files::load_from_file, which
works for multiple different types of files.
*/
let mut config: TestConfig = files::load_from_yaml();
// should output TestConfig { test: true } if test: true in the yml file
// otherwise, it will print TestConfig { test: false }
println!("{:?}", config);
// you can change the value of the config
config.test = !config.test;
}
Json配置
// import JsonConfig and files
// files has some useful load functions
use rsconfig::JsonConfig;
use rsconfig::files;
use serde_json;
use std::fs;
// our config class that we can expand upon to add different values
// to expand upon it, simply add more fields and update the import function(s)
#[derive(Debug)]
struct TestConfig {
test: bool
}
impl JsonConfig for TestConfig {
fn from_json(val: serde_json::Value) -> Self {
// look for "test" val
// NOTE: this code is not error-safe, will panic if the json does not contain a bool named "test"
Self { test: val["test"].as_bool().unwrap() }
}
fn save_json(&self, path: &str) -> io::Result<()> {
// convert to json pretty format and save
let data = serde_json::to_string_pretty(&Value::from(self.test)).unwrap();
fs::write(path, data).unwrap();
Ok(())
}
}
fn main() {
/*
NOTE: for a situation where you don't know the filetype,
you can impl FileConfig and use files::load_from_file, which
works for multiple different types of files.
*/
let mut config: TestConfig = files::load_from_json();
// should output TestConfig { test: true } if {"test": true} in the json file
// otherwise, it will print TestConfig { test: false }
println!("{:?}", config);
// you can change the value of the config
config.test = !config.test;
}
文件配置
#[derive(Debug)]
struct TestConfig {
test: bool
}
impl YamlConfig for TestConfig {
fn from_yaml(yaml: Vec<yaml_rust::Yaml>) -> Self {
Self { test: *&yaml[0]["test"].as_bool().unwrap() }
}
fn save_yaml(&self, path: &str) -> Result<()> {
let mut data = "test: ".to_string();
data.push_str(self.test.to_string().as_str());
fs::write(path, data).unwrap();
Ok(())
}
}
impl JsonConfig for TestConfig {
fn from_json(val: Value) -> Self {
Self { test: val["test"].as_bool().unwrap() }
}
fn save_json(&self, path: &str) -> io::Result<()> {
// convert to json pretty format and save
let mut m: Hashmap<&str, Value> = Hashmap::new();
m.insert("test", &Value::from(self.test));
let data = serde_json::to_string_pretty(m).unwrap();
fs::write(path, data).unwrap();
Ok(())
}
}
impl FileConfig for TestConfig {}
许可证
根据您的选择,许可协议为Apache许可证版本2.0或MIT许可证。
除非您明确说明,否则根据Apache-2.0许可证定义的,您有意提交以包含在此crate中的任何贡献,都将按照上述方式双重许可,不附加任何额外的条款或条件。
依赖项
~0.5–1MB
~22K SLoC