From 93904b8278f2789b68df0c7078cb04b5970944b0 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Sun, 30 Jun 2024 07:52:30 +0800 Subject: [PATCH] refactor: pure merge --- src-tauri/src/enhance/field.rs | 15 +------- src-tauri/src/enhance/merge.rs | 54 +++------------------------- src-tauri/src/enhance/mod.rs | 66 +++++++++++++++------------------- src-tauri/src/utils/tmpl.rs | 13 ++----- 4 files changed, 36 insertions(+), 112 deletions(-) diff --git a/src-tauri/src/enhance/field.rs b/src-tauri/src/enhance/field.rs index adb9228..d45cc14 100644 --- a/src-tauri/src/enhance/field.rs +++ b/src-tauri/src/enhance/field.rs @@ -11,8 +11,8 @@ pub const HANDLE_FIELDS: [&str; 11] = [ "allow-lan", "log-level", "ipv6", - "secret", "external-controller", + "secret", ]; pub const DEFAULT_FIELDS: [&str; 5] = [ @@ -23,19 +23,6 @@ pub const DEFAULT_FIELDS: [&str; 5] = [ "rules", ]; -pub fn use_filter(config: Mapping, filter: &Vec) -> Mapping { - let mut ret = Mapping::new(); - - for (key, value) in config.into_iter() { - if let Some(key) = key.as_str() { - if filter.contains(&key.to_string()) { - ret.insert(Value::from(key), value); - } - } - } - ret -} - pub fn use_lowercase(config: Mapping) -> Mapping { let mut ret = Mapping::new(); diff --git a/src-tauri/src/enhance/merge.rs b/src-tauri/src/enhance/merge.rs index 7a283ab..dccd65c 100644 --- a/src-tauri/src/enhance/merge.rs +++ b/src-tauri/src/enhance/merge.rs @@ -1,14 +1,5 @@ -use super::{use_filter, use_lowercase}; -use serde_yaml::{self, Mapping, Sequence, Value}; - -const MERGE_FIELDS: [&str; 6] = [ - "prepend-rules", - "append-rules", - "prepend-proxies", - "append-proxies", - "prepend-proxy-groups", - "append-proxy-groups", -]; +use super::use_lowercase; +use serde_yaml::{self, Mapping, Value}; fn deep_merge(a: &mut Value, b: &Value) { match (a, b) { @@ -23,47 +14,12 @@ fn deep_merge(a: &mut Value, b: &Value) { pub fn use_merge(merge: Mapping, config: Mapping) -> Mapping { let mut config = Value::from(config); - let mut merge_without_append = use_lowercase(merge.clone()); - for key in MERGE_FIELDS { - merge_without_append.remove(key).unwrap_or_default(); - } - deep_merge(&mut config, &Value::from(merge_without_append)); + let merge = use_lowercase(merge.clone()); - let mut config = config.as_mapping().unwrap().clone(); - let merge_list = MERGE_FIELDS.iter().map(|s| s.to_string()); - let merge = use_filter(merge, &merge_list.collect()); + deep_merge(&mut config, &Value::from(merge)); - ["rules", "proxies", "proxy-groups"] - .iter() - .for_each(|key_str| { - let key_val = Value::from(key_str.to_string()); + let config = config.as_mapping().unwrap().clone(); - let mut list = Sequence::default(); - list = config.get(&key_val).map_or(list.clone(), |val| { - val.as_sequence().map_or(list, |v| v.clone()) - }); - - let pre_key = Value::from(format!("prepend-{key_str}")); - let post_key = Value::from(format!("append-{key_str}")); - - if let Some(pre_val) = merge.get(&pre_key) { - if pre_val.is_sequence() { - let mut pre_val = pre_val.as_sequence().unwrap().clone(); - pre_val.extend(list); - list = pre_val; - } - } - - if let Some(post_val) = merge.get(&post_key) { - if post_val.is_sequence() { - list.extend(post_val.as_sequence().unwrap().clone()); - } - } - - if !list.is_empty() { - config.insert(key_val, Value::from(list)); - } - }); config } diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index ee4f99c..9af9ebd 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -103,47 +103,37 @@ pub async fn enhance() -> (Mapping, Vec, HashMap) { let mut exists_keys = use_keys(&config); // 保存出现过的keys // 处理用户的profile - match rules_item.data { - ChainType::Rules(rules) => { - config = use_seq(rules, config.to_owned(), "rules"); - } - _ => {} - } - match proxies_item.data { - ChainType::Proxies(proxies) => { - config = use_seq(proxies, config.to_owned(), "proxies"); - } - _ => {} - } - match groups_item.data { - ChainType::Groups(groups) => { - config = use_seq(groups, config.to_owned(), "proxy-groups"); - } - _ => {} - } - match merge_item.data { - ChainType::Merge(merge) => { - exists_keys.extend(use_keys(&merge)); - config = use_merge(merge, config.to_owned()); - } - _ => {} - } - match script_item.data { - ChainType::Script(script) => { - let mut logs = vec![]; - match use_script(script, config.to_owned()) { - Ok((res_config, res_logs)) => { - exists_keys.extend(use_keys(&res_config)); - config = res_config; - logs.extend(res_logs); - } - Err(err) => logs.push(("exception".into(), err.to_string())), + if let ChainType::Rules(rules) = rules_item.data { + config = use_seq(rules, config.to_owned(), "rules"); + } + + if let ChainType::Proxies(proxies) = proxies_item.data { + config = use_seq(proxies, config.to_owned(), "proxies"); + } + + if let ChainType::Groups(groups) = groups_item.data { + config = use_seq(groups, config.to_owned(), "proxy-groups"); + } + + if let ChainType::Merge(merge) = merge_item.data { + exists_keys.extend(use_keys(&merge)); + config = use_merge(merge, config.to_owned()); + } + + if let ChainType::Script(script) = script_item.data { + let mut logs = vec![]; + + match use_script(script, config.to_owned()) { + Ok((res_config, res_logs)) => { + exists_keys.extend(use_keys(&res_config)); + config = res_config; + logs.extend(res_logs); } - - result_map.insert(script_item.uid, logs); + Err(err) => logs.push(("exception".into(), err.to_string())), } - _ => {} + + result_map.insert(script_item.uid, logs); } // 合并默认的config diff --git a/src-tauri/src/utils/tmpl.rs b/src-tauri/src/utils/tmpl.rs index 45a018b..be83ca5 100644 --- a/src-tauri/src/utils/tmpl.rs +++ b/src-tauri/src/utils/tmpl.rs @@ -13,17 +13,8 @@ rules: [] /// enhanced profile pub const ITEM_MERGE: &str = "# Profile Enhancement Merge Template for Clash Verge -prepend-rules: [] - -prepend-proxies: [] - -prepend-proxy-groups: [] - -append-rules: [] - -append-proxies: [] - -append-proxy-groups: [] +profile: + store-selected: true "; /// enhanced profile