mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
109 lines
3.0 KiB
Rust
109 lines
3.0 KiB
Rust
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
|
|
",
|
|
);
|
|
}
|
|
}
|