fix: field sort for filter

This commit is contained in:
GyDi 2023-03-15 08:43:03 +08:00
parent d191877002
commit c5289dc0e8
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
2 changed files with 25 additions and 2 deletions

View File

@ -1,4 +1,5 @@
use serde_yaml::{Mapping, Value};
use std::collections::HashSet;
pub const HANDLE_FIELDS: [&str; 9] = [
"mode",
@ -102,7 +103,7 @@ pub fn use_lowercase(config: Mapping) -> Mapping {
ret
}
pub fn use_sort(config: Mapping) -> Mapping {
pub fn use_sort(config: Mapping, enable_filter: bool) -> Mapping {
let mut ret = Mapping::new();
HANDLE_FIELDS
@ -115,6 +116,28 @@ pub fn use_sort(config: Mapping) -> Mapping {
ret.insert(key, value.clone());
});
});
if !enable_filter {
let supported_keys: HashSet<&str> = HANDLE_FIELDS
.into_iter()
.chain(OTHERS_FIELDS)
.chain(DEFAULT_FIELDS)
.collect();
let config_keys: HashSet<&str> = config
.keys()
.filter_map(|e| e.as_str())
.into_iter()
.collect();
config_keys.difference(&supported_keys).for_each(|&key| {
let key = Value::from(key);
config.get(&key).map(|value| {
ret.insert(key, value.clone());
});
});
}
ret
}

View File

@ -116,7 +116,7 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
config = use_filter(config, &clash_fields, enable_filter);
config = use_tun(config, enable_tun);
config = use_sort(config);
config = use_sort(config, enable_filter);
let mut exists_set = HashSet::new();
exists_set.extend(exists_keys.into_iter().filter(|s| clash_fields.contains(s)));