做没有好的网站你懂的,南山做网站的公司,网站 app设计开发建设,自己可以模拟装修app本文提供了一种读取yaml配置文件#xff0c;转换为结构体的方法。
安装依赖
cargo add serde
cargo add serde_json
cargo add serde_yaml
cargo add schemars配置文件示例
test:debug: true设置需要转换的结构体
需要定义Default trait#xff0c;用于配置文件不存在的情…本文提供了一种读取yaml配置文件转换为结构体的方法。
安装依赖
cargo add serde
cargo add serde_json
cargo add serde_yaml
cargo add schemars配置文件示例
test:debug: true设置需要转换的结构体
需要定义Default trait用于配置文件不存在的情况下所需要的默认配置值。
use schemars::schema::RootSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::env;
use std::path::PathBuf;#[derive(Debug, Serialize, Deserialize)]
struct TestConf {debug: bool,
}impl Default for TestConf {fn default() - Self {Self { debug: false }}
}#[derive(Debug, Serialize, Deserialize)]
struct GlobalConf {test: TestConf,
}impl Default for GlobalConf {fn default() - Self {Self { test: TestConf::default() }}
}读取配置文件
根据指定的文件路径读取yaml格式的文件将其转换为结构体对象。
/// 读取yaml配置文件
fn load_yaml_configT(path: str) - OptionT
whereT: DeserializeOwned,
{// 将yaml解析为json对象match serde_yaml::from_str::RootSchema(std::fs::read_to_string(path).expect(format!(failure read config file {}, path)),) {Ok(root_schema) {// 将json对象转换指定的结构体实例let data serde_json::to_string_pretty(root_schema).expect(failure to parse RootSchema);let config serde_json::from_str::T(*data).expect(format!(failure to read yaml file));Some(config)}Err(_err) None,}
}/// 读取并加载配置文件
fn fetch_conf(path: OptionPathBuf) - GlobalConf {match path {Some(path) {// 从指定的文件路径加载配置load_yaml_config::GlobalConf(path.to_str().unwrap()).unwrap()}None {// 配置文件不存在则使用默认配置GlobalConf::default()}}
}单元测试
#[test]
fn test_read_yaml_conf() {let config_path env::current_dir().unwrap().join(tests/data.yaml);let global_conf fetch_conf(Some(config_path));assert_eq!(global_conf.test.debug, true);
}