mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
refactor: update function signatures to use references for Mapping and String
This commit is contained in:
@@ -25,14 +25,14 @@ pub const DEFAULT_FIELDS: [&str; 5] = [
|
||||
"rules",
|
||||
];
|
||||
|
||||
pub fn use_lowercase(config: Mapping) -> Mapping {
|
||||
pub fn use_lowercase(config: &Mapping) -> Mapping {
|
||||
let mut ret = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
if let Some(key_str) = key.as_str() {
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
ret.insert(Value::from(key_str.as_str()), value);
|
||||
ret.insert(Value::from(key_str.as_str()), value.clone());
|
||||
}
|
||||
}
|
||||
ret
|
||||
|
||||
@@ -14,7 +14,7 @@ fn deep_merge(a: &mut Value, b: &Value) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn use_merge(merge: Mapping, config: Mapping) -> Mapping {
|
||||
pub fn use_merge(merge: &Mapping, config: Mapping) -> Mapping {
|
||||
let mut config = Value::from(config);
|
||||
let merge = use_lowercase(merge);
|
||||
|
||||
@@ -61,7 +61,7 @@ fn test_merge() -> anyhow::Result<()> {
|
||||
let merge = serde_yaml_ng::from_str::<Mapping>(merge)?;
|
||||
let config = serde_yaml_ng::from_str::<Mapping>(config)?;
|
||||
|
||||
let _ = serde_yaml_ng::to_string(&use_merge(merge, config))?;
|
||||
let _ = serde_yaml_ng::to_string(&use_merge(&merge, config))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -325,19 +325,19 @@ fn process_global_items(
|
||||
mut config: Mapping,
|
||||
global_merge: ChainItem,
|
||||
global_script: ChainItem,
|
||||
profile_name: String,
|
||||
profile_name: &String,
|
||||
) -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
let mut result_map = HashMap::new();
|
||||
let mut exists_keys = use_keys(&config);
|
||||
|
||||
if let ChainType::Merge(merge) = global_merge.data {
|
||||
exists_keys.extend(use_keys(&merge));
|
||||
config = use_merge(merge, config.to_owned());
|
||||
config = use_merge(&merge, config.to_owned());
|
||||
}
|
||||
|
||||
if let ChainType::Script(script) = global_script.data {
|
||||
let mut logs = vec![];
|
||||
match use_script(script, config.to_owned(), profile_name) {
|
||||
match use_script(script, &config, profile_name) {
|
||||
Ok((res_config, res_logs)) => {
|
||||
exists_keys.extend(use_keys(&res_config));
|
||||
config = res_config;
|
||||
@@ -361,7 +361,7 @@ fn process_profile_items(
|
||||
groups_item: ChainItem,
|
||||
merge_item: ChainItem,
|
||||
script_item: ChainItem,
|
||||
profile_name: String,
|
||||
profile_name: &String,
|
||||
) -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
if let ChainType::Rules(rules) = rules_item.data {
|
||||
config = use_seq(rules, config.to_owned(), "rules");
|
||||
@@ -377,12 +377,12 @@ fn process_profile_items(
|
||||
|
||||
if let ChainType::Merge(merge) = merge_item.data {
|
||||
exists_keys.extend(use_keys(&merge));
|
||||
config = use_merge(merge, config.to_owned());
|
||||
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(), profile_name) {
|
||||
match use_script(script, &config, profile_name) {
|
||||
Ok((res_config, res_logs)) => {
|
||||
exists_keys.extend(use_keys(&res_config));
|
||||
config = res_config;
|
||||
@@ -486,7 +486,7 @@ fn apply_builtin_scripts(
|
||||
.for_each(|item| {
|
||||
logging!(debug, Type::Core, "run builtin script {}", item.uid);
|
||||
if let ChainType::Script(script) = item.data {
|
||||
match use_script(script, config.to_owned(), "".into()) {
|
||||
match use_script(script, &config, &String::from("")) {
|
||||
Ok((res_config, _)) => {
|
||||
config = res_config;
|
||||
}
|
||||
@@ -646,7 +646,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
|
||||
// process globals
|
||||
let (config, exists_keys, result_map) =
|
||||
process_global_items(config, global_merge, global_script, profile_name.clone());
|
||||
process_global_items(config, global_merge, global_script, &profile_name);
|
||||
|
||||
// process profile-specific items
|
||||
let (config, exists_keys, result_map) = process_profile_items(
|
||||
@@ -658,7 +658,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
groups_item,
|
||||
merge_item,
|
||||
script_item,
|
||||
profile_name,
|
||||
&profile_name,
|
||||
);
|
||||
|
||||
// merge default clash config
|
||||
|
||||
@@ -5,8 +5,8 @@ use smartstring::alias::String;
|
||||
|
||||
pub fn use_script(
|
||||
script: String,
|
||||
config: Mapping,
|
||||
name: String,
|
||||
config: &Mapping,
|
||||
name: &String,
|
||||
) -> Result<(Mapping, Vec<(String, String)>)> {
|
||||
use boa_engine::{Context, JsString, JsValue, Source, native_function::NativeFunction};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
@@ -66,7 +66,7 @@ pub fn use_script(
|
||||
let config_str = serde_json::to_string(&config)?;
|
||||
|
||||
// 仅处理 name 参数中的特殊字符
|
||||
let safe_name = escape_js_string_for_single_quote(&name);
|
||||
let safe_name = escape_js_string_for_single_quote(name);
|
||||
|
||||
let code = format!(
|
||||
r"try{{
|
||||
@@ -93,7 +93,7 @@ pub fn use_script(
|
||||
|
||||
let mut out = outputs.borrow_mut();
|
||||
match res {
|
||||
Ok(config) => Ok((use_lowercase(config), out.to_vec())),
|
||||
Ok(config) => Ok((use_lowercase(&config), out.to_vec())),
|
||||
Err(err) => {
|
||||
out.push(("exception".into(), err.to_string().into()));
|
||||
Ok((config, out.to_vec()))
|
||||
@@ -150,8 +150,8 @@ fn test_script() {
|
||||
enable: false
|
||||
";
|
||||
|
||||
let config = serde_yaml_ng::from_str(config).expect("Failed to parse test config YAML");
|
||||
let (config, results) = use_script(script.into(), config, "".into())
|
||||
let config = &serde_yaml_ng::from_str(config).expect("Failed to parse test config YAML");
|
||||
let (config, results) = use_script(script.into(), config, &String::from(""))
|
||||
.expect("Script execution should succeed in test");
|
||||
|
||||
let _ = serde_yaml_ng::to_string(&config).expect("Failed to serialize config to YAML");
|
||||
|
||||
Reference in New Issue
Block a user