refactor: pure merge
Some checks are pending
Alpha Build / alpha (macos-latest, aarch64-apple-darwin) (push) Waiting to run
Alpha Build / alpha (macos-latest, x86_64-apple-darwin) (push) Waiting to run
Alpha Build / alpha (windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha (windows-latest, i686-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha (windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, aarch64-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, armv7-unknown-linux-gnueabihf) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, i686-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (arm64, windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (x64, windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (x86, windows-latest, i686-pc-windows-msvc) (push) Waiting to run
Alpha Build / Update tag (push) Blocked by required conditions

This commit is contained in:
MystiPanda 2024-06-30 07:52:30 +08:00
parent 495580ae2b
commit 93904b8278
No known key found for this signature in database
4 changed files with 36 additions and 112 deletions

View File

@ -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<String>) -> 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();

View File

@ -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
}

View File

@ -103,47 +103,37 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
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

View File

@ -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