mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
Render from inputs supplied by the app, reuse the script crate, and preserve the default template fast path.
78 lines
2.0 KiB
Rust
78 lines
2.0 KiB
Rust
use serde_yaml_ng::{Mapping, Value};
|
|
use tristr::alias::String;
|
|
|
|
pub const HANDLE_FIELDS: [&str; 12] = [
|
|
"mode",
|
|
"redir-port",
|
|
"tproxy-port",
|
|
"mixed-port",
|
|
"socks-port",
|
|
"port",
|
|
"allow-lan",
|
|
"log-level",
|
|
"ipv6",
|
|
"external-controller",
|
|
"secret",
|
|
"unified-delay",
|
|
];
|
|
|
|
pub const DEFAULT_FIELDS: [&str; 5] = ["proxies", "proxy-providers", "proxy-groups", "rule-providers", "rules"];
|
|
|
|
pub fn use_lowercase(config: Mapping) -> Mapping {
|
|
let mut lowercased = Mapping::new();
|
|
|
|
for (key, value) in config {
|
|
let mut key = match key {
|
|
Value::String(key) => key,
|
|
key => {
|
|
let Some(key) = key.as_str() else {
|
|
continue;
|
|
};
|
|
key.to_owned()
|
|
}
|
|
};
|
|
key.make_ascii_lowercase();
|
|
lowercased.insert(Value::String(key), value);
|
|
}
|
|
lowercased
|
|
}
|
|
|
|
pub fn use_sort(mut config: Mapping) -> Mapping {
|
|
let mut sorted = Mapping::new();
|
|
HANDLE_FIELDS.into_iter().for_each(|key| {
|
|
let key = Value::from(key);
|
|
if let Some(value) = config.remove(&key) {
|
|
sorted.insert(key, value);
|
|
}
|
|
});
|
|
|
|
let mut default_field_values = Mapping::new();
|
|
for (key, value) in config {
|
|
if let Some(key_str) = key.as_str() {
|
|
if DEFAULT_FIELDS.contains(&key_str) {
|
|
default_field_values.insert(key, value);
|
|
} else if !HANDLE_FIELDS.contains(&key_str) {
|
|
sorted.insert(key, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
DEFAULT_FIELDS.into_iter().for_each(|key| {
|
|
let key = Value::from(key);
|
|
if let Some(value) = default_field_values.remove(&key) {
|
|
sorted.insert(key, value);
|
|
}
|
|
});
|
|
|
|
sorted
|
|
}
|
|
|
|
#[inline]
|
|
pub fn use_keys<'a>(config: &'a Mapping) -> impl Iterator<Item = String> + 'a {
|
|
config.iter().filter_map(|(key, _)| key.as_str()).map(|s: &str| {
|
|
let mut s: String = s.into();
|
|
s.make_ascii_lowercase();
|
|
s
|
|
})
|
|
}
|