refactor(config): centralize Mihomo YAML output

This commit is contained in:
Tunglies
2026-12-01 22:10:13 +08:00
parent 840de9c4d1
commit 5c05722f9a
14 changed files with 69 additions and 74 deletions
+4 -1
View File
@@ -1,3 +1,4 @@
use super::mihomo_yaml;
use crate::constants::{network, tun as tun_const};
use crate::utils::dirs::{path_to_str, sidecar_ipc_path};
use crate::utils::{dirs, help};
@@ -150,7 +151,9 @@ impl IClashTemp {
}
pub async fn save_config(&self) -> Result<()> {
help::save_yaml(&dirs::clash_path()?, &self.0, Some("# Generated by Clash Verge")).await
let path = dirs::clash_path()?;
let yaml = mihomo_yaml::serialize(&self.0)?;
help::save_yaml_str(&path, &yaml, Some("# Generated by Clash Verge")).await
}
pub fn get_mixed_port(&self) -> u16 {
+2 -2
View File
@@ -1,4 +1,4 @@
use super::{IClashTemp, IProfiles, IVerge, MixedPort};
use super::{IClashTemp, IProfiles, IVerge, MixedPort, mihomo_yaml};
use crate::core::notify::NoticeStatus;
use crate::{
config::{
@@ -230,7 +230,7 @@ impl Config {
{
return Ok(std::sync::Arc::clone(cached));
}
let yaml_str = std::sync::Arc::new(serde_yaml_ng::to_string(config)?);
let yaml_str = std::sync::Arc::new(mihomo_yaml::serialize(config)?);
*RUNTIME_YAML_CACHE.lock() = Some((key, std::sync::Arc::clone(&yaml_str)));
Ok(yaml_str)
})
+108
View File
@@ -0,0 +1,108 @@
use serde::Serialize;
use serde_yaml_ng::Result;
pub(crate) use serde_yaml_ng::to_string as serialize;
pub(crate) fn serialize_for_export<T: Serialize>(data: &T) -> Result<String> {
Ok(quote_fake_ip_filter_wildcards(&serialize(data)?))
}
fn quote_fake_ip_filter_wildcards(yaml: &str) -> String {
let mut result = String::with_capacity(yaml.len());
let mut in_fake_ip_filter = false;
let mut fake_ip_filter_indent = 0;
for line in yaml.lines() {
let indent = line.len() - line.trim_start_matches(' ').len();
let trimmed = &line[indent..];
if in_fake_ip_filter && indent <= fake_ip_filter_indent && !trimmed.starts_with("- ") {
in_fake_ip_filter = false;
}
if !in_fake_ip_filter && trimmed == "fake-ip-filter:" {
in_fake_ip_filter = true;
fake_ip_filter_indent = indent;
}
if in_fake_ip_filter
&& indent >= fake_ip_filter_indent
&& let Some(item) = trimmed.strip_prefix("- ")
&& needs_quotes(item)
{
result.push_str(&line[..indent + 2]);
push_quoted(&mut result, item);
} else {
result.push_str(line);
}
result.push('\n');
}
result
}
fn needs_quotes(item: &str) -> bool {
!item.starts_with(['"', '\'']) && (item.contains('*') || item.starts_with(['+', '.']))
}
fn push_quoted(output: &mut String, item: &str) {
output.push('\'');
for (index, part) in item.split('\'').enumerate() {
if index != 0 {
output.push_str("''");
}
output.push_str(part);
}
output.push('\'');
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::serialize_for_export;
fn roundtrip(input: &str) {
let value: serde_yaml_ng::Value = serde_yaml_ng::from_str(input).expect("input yaml should parse");
let output = serialize_for_export(&value).expect("yaml should serialize");
let reparsed: serde_yaml_ng::Value = serde_yaml_ng::from_str(&output).expect("serialized yaml should parse");
assert_eq!(reparsed, value);
}
#[test]
fn quotes_fake_ip_filter_wildcards_without_changing_values() {
let input = r#"
dns:
fake-ip-filter:
- "*.lan"
- "+.market.xiaomi.com"
- ".example.com"
- "time.*.com"
- "plain.example.com"
"#;
roundtrip(input);
let value: serde_yaml_ng::Value = serde_yaml_ng::from_str(input).expect("input yaml should parse");
let output = serialize_for_export(&value).expect("yaml should serialize");
assert!(output.contains("- '*.lan'"));
assert!(output.contains("- '+.market.xiaomi.com'"));
assert!(output.contains("- '.example.com'"));
assert!(output.contains("- 'time.*.com'"));
assert!(output.contains("- plain.example.com"));
}
#[test]
fn nested_multiline_strings_roundtrip() {
roundtrip(
r"
items:
- name: demo
desc: |
line1
line2
",
);
}
}
+1
View File
@@ -3,6 +3,7 @@ mod clash;
mod config;
pub(crate) mod dns;
mod encrypt;
pub(crate) mod mihomo_yaml;
mod mixed_port;
mod port;
mod prfitem;