Fix fatal!

没啥嘛(
This commit is contained in:
shenjack-5600u 2024-01-29 11:24:20 +08:00
parent 70400f671c
commit dc1fecf735
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F
3 changed files with 44 additions and 20 deletions

View File

@ -118,7 +118,7 @@ mod tests {
Config::new( Config::new(
None, None,
None, "127.0.0.1".to_string(),
test_conf.cluster_port, test_conf.cluster_port,
test_conf.cluster_id, test_conf.cluster_id,
test_conf.cluster_secret, test_conf.cluster_secret,

View File

@ -1,5 +1,5 @@
use { use {
core::panic, crate::fatal,
log::{info, warn}, log::{info, warn},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{ std::{
@ -32,7 +32,7 @@ pub struct Config {
impl Config { impl Config {
pub fn new( pub fn new(
center_url: Option<String>, center_url: Option<String>,
host_ip: Option<String>, host_ip: String,
host_port: Option<u32>, host_port: Option<u32>,
cluster_id: String, cluster_id: String,
cluster_secret: String, cluster_secret: String,
@ -49,7 +49,7 @@ impl Config {
}; };
Self { Self {
center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()),
host_ip: host_ip.unwrap_or("0.0.0.0".to_string()), host_ip,
host_port: host_port.unwrap_or(8080), host_port: host_port.unwrap_or(8080),
cluster_id, cluster_id,
cluster_secret, cluster_secret,
@ -58,20 +58,24 @@ impl Config {
} }
} }
pub fn raw_new() {}
pub fn convert_from_env() { pub fn convert_from_env() {
// Load from env // Load from env
let center_url = env::var("CENTER_URL").ok(); let center_url = env::var("CENTER_URL").ok();
let host_ip = env::var("CLUSTER_IP").ok(); let host_ip = env::var("CLUSTER_IP").unwrap_or_else(|_| {
fatal!("CLUSTER_IP is required");
});
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok(); let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok(); let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
let cache_dir = env::var("CACHE_DIR").ok().map(|x| PathBuf::from(x)); let cache_dir = env::var("CACHE_DIR").ok().map(|x| PathBuf::from(x));
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| { let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| {
todo!("Not implemented yet"); fatal!("CLUSTER_ID is required");
}); });
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| {
todo!("Not implemented yet"); fatal!("CLUSTER_SECRET is required");
}); });
// Decrapated warning // Decrapated warning
@ -104,24 +108,24 @@ impl Config {
config.save(); config.save();
} }
/// 保存至文件
pub fn save(&self) { pub fn save(&self) {
if !fs::canonicalize(CONFIG_PATH).is_ok() { if !fs::canonicalize(CONFIG_PATH).is_ok() {
fs::File::create(CONFIG_PATH).unwrap_or_else(|err| { fs::File::create(CONFIG_PATH).unwrap_or_else(|err| {
todo!("Not implemented yet"); fatal!(("Failed to create config: {}", err), ("{}", err));
}); });
//TODO: Trigger initialization //TODO: Trigger initialization
} }
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| { fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| {
todo!("Not implemented yet"); fatal!(("Failed to save config: {}", err), ("{}", err));
}); });
} }
pub fn load(&mut self) { pub fn update_from_config(&mut self) {
if Path::new(CONFIG_PATH).exists() { if Path::new(CONFIG_PATH).exists() {
let raw_data: Config = toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap()) let raw_data: Config = toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap())
.unwrap_or_else(|err| { .unwrap_or_else(|err| {
panic!("Failed to load config: {}", err); fatal!(("Failed to load config: {}", err), ("{}", err));
// TODO: Replace this with fatal!()
}); });
self.center_url = raw_data.center_url; self.center_url = raw_data.center_url;
self.host_ip = raw_data.host_ip; self.host_ip = raw_data.host_ip;
@ -132,8 +136,7 @@ impl Config {
self.cache_dir = raw_data.cache_dir; self.cache_dir = raw_data.cache_dir;
info!("Config loaded"); info!("Config loaded");
} else { } else {
panic!("Failed to find config file"); fatal!("Config file {} not found", CONFIG_PATH);
// TODO: Replace this with fatal!()
} }
} }
@ -144,10 +147,26 @@ impl Config {
#[test] #[test]
fn test_save_and_load_config() { 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 config: Config = Config::new(
let mut test_config: Config = Config::new(None, None, None, "111".to_string(), "222".to_string(), None, None); Some("https://example.com".to_string()),
"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,
"0.0.0.0".to_string(),
None,
"111".to_string(),
"222".to_string(),
None,
None,
);
config.save(); config.save();
test_config.load(); test_config.update_from_config();
assert_eq!(test_config.center_url, "https://example.com"); assert_eq!(test_config.center_url, "https://example.com");
assert_eq!(test_config.host_ip, "0.0.0.0"); assert_eq!(test_config.host_ip, "0.0.0.0");
assert_eq!(test_config.host_port, 23333); assert_eq!(test_config.host_port, 23333);

View File

@ -102,7 +102,6 @@ pub fn avro_data_to_file_list(data: Vec<u8>) -> Option<Vec<SyncFile>> {
} }
} }
/// FATAL 级 Log /// FATAL 级 Log
/// 这个宏会输出一条 error 级的日志, 并且 panic! /// 这个宏会输出一条 error 级的日志, 并且 panic!
/// 这个宏应当接收两个参数, 分别定义为 arg1 和 arg2, 其应当均为 String 类型 /// 这个宏应当接收两个参数, 分别定义为 arg1 和 arg2, 其应当均为 String 类型
@ -115,6 +114,12 @@ pub fn avro_data_to_file_list(data: Vec<u8>) -> Option<Vec<SyncFile>> {
#[macro_export] #[macro_export]
macro_rules! fatal { macro_rules! fatal {
(($($arg1:tt)+), ($($arg2:tt)+)) => {
use log::error;
// error!() + panic!()
error!($($arg1)+);
panic!($($arg2)+);
};
($($arg:tt)+) => { ($($arg:tt)+) => {
use log::error; use log::error;
// error!() + panic!() // error!() + panic!()