From 6331447dcd34632023d90e35fb13e725f27922f0 Mon Sep 17 00:00:00 2001 From: GyDi Date: Wed, 21 Sep 2022 22:15:24 +0800 Subject: [PATCH] feat: yaml merge key --- src-tauri/src/data/clash.rs | 2 +- src-tauri/src/data/prfitem.rs | 2 +- src-tauri/src/data/profiles.rs | 2 +- src-tauri/src/utils/config.rs | 24 ++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/data/clash.rs b/src-tauri/src/data/clash.rs index 627389a..56bbff3 100644 --- a/src-tauri/src/data/clash.rs +++ b/src-tauri/src/data/clash.rs @@ -125,7 +125,7 @@ impl Clash { /// get clash config pub fn read_config() -> Mapping { - config::read_yaml::(dirs::clash_path()) + config::read_merge_mapping(dirs::clash_path()) } /// save the clash config diff --git a/src-tauri/src/data/prfitem.rs b/src-tauri/src/data/prfitem.rs index 2f8f69e..41be194 100644 --- a/src-tauri/src/data/prfitem.rs +++ b/src-tauri/src/data/prfitem.rs @@ -352,7 +352,7 @@ impl PrfItem { }), "merge" => Some(ChainItem { uid, - data: ChainType::Merge(config::read_yaml::(path)), + data: ChainType::Merge(config::read_merge_mapping(path)), }), _ => None, } diff --git a/src-tauri/src/data/profiles.rs b/src-tauri/src/data/profiles.rs index 6b4d6e1..09ac557 100644 --- a/src-tauri/src/data/profiles.rs +++ b/src-tauri/src/data/profiles.rs @@ -283,7 +283,7 @@ impl Profiles { bail!("failed to read the file \"{}\"", file_path.display()); } - return Ok(config::read_yaml::(file_path.clone())); + return Ok(config::read_merge_mapping(file_path.clone())); } } bail!("failed to find current profile \"uid:{current}\""); diff --git a/src-tauri/src/utils/config.rs b/src-tauri/src/utils/config.rs index 12e3d6d..add4d8b 100644 --- a/src-tauri/src/utils/config.rs +++ b/src-tauri/src/utils/config.rs @@ -1,5 +1,6 @@ use anyhow::{Context, Result}; use serde::{de::DeserializeOwned, Serialize}; +use serde_yaml::{Mapping, Value}; use std::{fs, path::PathBuf}; /// read data from yaml as struct T @@ -20,6 +21,29 @@ pub fn read_yaml(path: PathBuf) -> T { } } +/// read mapping from yaml fix #165 +pub fn read_merge_mapping(path: PathBuf) -> Mapping { + let map = Mapping::new(); + + if !path.exists() { + log::error!(target: "app", "file not found \"{}\"", path.display()); + return map; + } + + let yaml_str = fs::read_to_string(&path).unwrap_or("".into()); + + match serde_yaml::from_str::(&yaml_str) { + Ok(mut val) => { + crate::log_if_err!(val.apply_merge()); + val.as_mapping().unwrap_or(&map).to_owned() + } + Err(_) => { + log::error!(target: "app", "failed to read yaml file \"{}\"", path.display()); + map + } + } +} + /// save the data to the file /// can set `prefix` string to add some comments pub fn save_yaml(path: PathBuf, data: &T, prefix: Option<&str>) -> Result<()> {