完成配置解析功能,并添加针对配置保存与解析的单元测试

This commit is contained in:
BlueFunny 2024-01-28 23:43:18 +08:00
parent b1d1ac6b8b
commit aef3a89af8
Signed by: BlueFunny
GPG Key ID: 44F303A0FDC58DC0

View File

@ -1,13 +1,17 @@
use { use {
crate::fatal, core::panic,
log::{error, info, warn}, log::{info, warn},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{env, fs, path::PathBuf}, std::{
env, fs,
path::{Path, PathBuf},
},
}; };
const CONFIG_PATH: &str = "config.toml"; const CONFIG_PATH: &str = "config.toml";
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
// TODO: 将除了 cluster_id, cluster_secret 之外的配置项可选化
pub struct Config { pub struct Config {
/// CENTER_URL /// CENTER_URL
pub center_url: String, pub center_url: String,
@ -112,14 +116,24 @@ impl Config {
}); });
} }
pub fn load(&mut self) {
pub fn load() { if Path::new(CONFIG_PATH).exists() {
if fs::canonicalize(CONFIG_PATH).is_ok() { let raw_data: Config = toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap())
let config: Config = toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap()).unwrap_or_else(|err| { .unwrap_or_else(|err| {
todo!("Not implemented yet"); panic!("Failed to load config: {}", err);
// TODO: Replace this with fatal!()
}); });
self.center_url = raw_data.center_url;
self.host_ip = raw_data.host_ip;
self.host_port = raw_data.host_port;
self.cluster_id = raw_data.cluster_id;
self.cluster_secret = raw_data.cluster_secret;
self.no_demaon = raw_data.no_demaon;
self.cache_dir = raw_data.cache_dir;
info!("Config loaded"); info!("Config loaded");
info!("{:#?}", config); } else {
panic!("Failed to find config file");
// TODO: Replace this with fatal!()
} }
} }
@ -127,3 +141,21 @@ impl Config {
format!("{}{}", self.center_url, path) format!("{}{}", self.center_url, path)
} }
} }
#[test]
fn test_save_and_load_config() {
let config: Config = Config::new(Some("https://example.com".to_string()), Some("0.0.0.0".to_string()), Some(23333), "0066ccff".to_string(), "123456789".to_string(), Some(true), Some(PathBuf::from("cache")));
let mut test_config: Config = Config::new(None, None, None, "111".to_string(), "222".to_string(), None, None);
config.save();
test_config.load();
assert_eq!(test_config.center_url, "https://example.com");
assert_eq!(test_config.host_ip, "0.0.0.0");
assert_eq!(test_config.host_port, 23333);
assert_eq!(test_config.cluster_id, "0066ccff");
assert_eq!(test_config.cluster_secret, "123456789");
assert_eq!(test_config.no_demaon, true);
assert_eq!(test_config.cache_dir, PathBuf::from("cache"));
// Clean up the temporary config file
fs::remove_file(CONFIG_PATH).unwrap();
}