mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 19:23:47 +08:00
feat: improve yaml file error log
This commit is contained in:
parent
35aee15b6d
commit
b428eff10e
@ -4,8 +4,20 @@ use std::{fs, path::PathBuf};
|
|||||||
|
|
||||||
/// read data from yaml as struct T
|
/// read data from yaml as struct T
|
||||||
pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
|
pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
|
||||||
let yaml_str = fs::read_to_string(path).unwrap_or("".into());
|
if !path.exists() {
|
||||||
serde_yaml::from_str::<T>(&yaml_str).unwrap_or(T::default())
|
log::error!("file not found \"{}\"", path.display());
|
||||||
|
return T::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
let yaml_str = fs::read_to_string(&path).unwrap_or("".into());
|
||||||
|
|
||||||
|
match serde_yaml::from_str::<T>(&yaml_str) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(_) => {
|
||||||
|
log::error!("failed to read yaml file \"{}\"", path.display());
|
||||||
|
T::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// save the data to the file
|
/// save the data to the file
|
||||||
|
Loading…
x
Reference in New Issue
Block a user