6 个版本
0.1.6 | 2023 年 7 月 1 日 |
---|---|
0.1.5 | 2023 年 5 月 27 日 |
0.1.3 | 2022 年 7 月 16 日 |
#1144 in 开发工具
64 每月下载量
在 renvy 中使用
29KB
225 代码行
librenvy
此 crate 提供了用于管理基于模板(例如 .env 和 .env.dist 文件)的设置文件的易于使用的功能。
此 crate 假设它管理的文件是它理解的键值对(例如 key=value
)。如果键存在于模板文件中,它将添加键到设置中;如果键在模板文件中不存在,它将可选地从设置文件中删除键。
安装
❯ cargo install librenvy
解析文件
此示例展示了如何使用 deserialize
函数将现有文件轻松读取到 Settings
结构体中。 Settings
是此 crate 进一步处理的基础。
// read_file returns a std::io::Result<String>
let settings = renvy::read_file("./.env");
assert!(settings.is_ok());
if let Ok(settings) = settings {
// renvy::deserialize consumes this String and returns an instance of renvy::Settings
let settings: renvy::Settings = renvy::deserialize(&settings);
println!("Number of settings read: {}", &settings.len());
settings.iter().for_each(|(key, value)| {
println!("{:?}: {:?}\n", key, value);
});
} else {
println!("Unable to read settings file!");
}
// you can use the same function for reading settings and template files
let defaults = renvy::read_file("./.env.dist");
assert!(defaults.is_ok());
if let Ok(defaults) = defaults {
// we're reusing the same data structure for defaults
let defaults: renvy::Settings = renvy::deserialize(&defaults);
println!("Number of defaults read: {}", &defaults.len());
defaults.iter().for_each(|(key, value)| {
println!("{:?}: {:?}\n", key, value);
});
} else {
println!("Unable to read defaults file!");
}
根据模板更新设置
使用 merge
函数可以基于现有模板更新设置。模板中的新键将以模板中给出的默认值添加到设置中。
// settings file contains 1 key-value pair
let settings = renvy::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);
// defaults file contains 1 other key-value pair
let defaults = renvy::Settings::from([("port".into(), Some("433".into()))]);
// merging defaults with settings will result in a new object merge::settings
// that contains 2 key-value pairs:
//
// - "domain" because it was already present in `settings`
// - "port" because it was present in defaults
let merged = renvy::merge(settings, defaults, None);
assert!(merged.get("domain").is_some());
assert_eq!(merged.get("domain").unwrap(), &Some(String::from("https://benjaminsattler.net")));
assert!(merged.get("port").is_some());
assert_eq!(merged.get("port").unwrap(), &Some(String::from("433")));
清理多余的设置
您还可以通过将 Some(true)
传递给 merge
函数的第三个可选参数来从用户设置中删除任何在默认设置中缺失的键。
// settings file contains 1 key-value pair
let settings = renvy::Settings::from([("domain".into(), Some("https://benjaminsattler.net".into()))]);
// defaults file contains 1 other key-value pair
let defaults = renvy::Settings::from([("port".into(), Some("433".into()))]);
// merging defaults with settings will result in a new object merge::settings
// that contains only 1 key-value pair. The key "domain" will be removed because
// it does not exist in the defaults:
//
// - "port" because it was present in defaults
let merged = renvy::merge(settings, defaults, Some(true));
assert!(merged.get("domain").is_none());
assert!(merged.get("port").is_some());
assert_eq!(merged.get("port").unwrap(), &Some(String::from("433")));
将合并的结果写回文件
最后一步是通过调用 serialize
和 write_file
将合并后的结果持久化到设置文件。
// first we're serializing the object renvy::Settings into a String
let merged = renvy::serialize(&merged);
// then we take that String and write it back into the original settings file
let result = renvy::write_file("./.env", &merged);
// write_file returns a std::io::Result<()>
assert!(result.is_ok());
许可证:MIT