refactor: migrate from serde_yaml to serde_yaml_ng for improved YAML handling (#4568)

* refactor: migrate from serde_yaml to serde_yaml_ng for improved YAML handling

* refactor: format code for better readability in DNS configuration
This commit is contained in:
Tunglies
2025-08-30 02:24:47 +08:00
committed by GitHub
parent f86a1816e0
commit 3939741a06
24 changed files with 70 additions and 50 deletions

View File

@@ -3,7 +3,7 @@ use crate::{
config::PrfItem,
utils::{dirs, help},
};
use serde_yaml::Mapping;
use serde_yaml_ng::Mapping;
use std::fs;
#[derive(Debug, Clone)]

View File

@@ -1,4 +1,4 @@
use serde_yaml::{Mapping, Value};
use serde_yaml_ng::{Mapping, Value};
use std::collections::HashSet;
pub const HANDLE_FIELDS: [&str; 12] = [

View File

@@ -1,5 +1,5 @@
use super::use_lowercase;
use serde_yaml::{self, Mapping, Value};
use serde_yaml_ng::{self, Mapping, Value};
fn deep_merge(a: &mut Value, b: &Value) {
match (a, b) {
@@ -54,10 +54,10 @@ fn test_merge() -> anyhow::Result<()> {
script1: test
";
let merge = serde_yaml::from_str::<Mapping>(merge)?;
let config = serde_yaml::from_str::<Mapping>(config)?;
let merge = serde_yaml_ng::from_str::<Mapping>(merge)?;
let config = serde_yaml_ng::from_str::<Mapping>(config)?;
let _ = serde_yaml::to_string(&use_merge(merge, config))?;
let _ = serde_yaml_ng::to_string(&use_merge(merge, config))?;
Ok(())
}

View File

@@ -7,7 +7,7 @@ mod tun;
use self::{chain::*, field::*, merge::*, script::*, seq::*, tun::*};
use crate::{config::Config, utils::tmpl};
use serde_yaml::Mapping;
use serde_yaml_ng::Mapping;
use std::collections::{HashMap, HashSet};
type ResultLog = Vec<(String, String)>;
@@ -386,7 +386,9 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
if dns_path.exists() {
if let Ok(dns_yaml) = fs::read_to_string(&dns_path) {
if let Ok(dns_config) = serde_yaml::from_str::<serde_yaml::Mapping>(&dns_yaml) {
if let Ok(dns_config) =
serde_yaml_ng::from_str::<serde_yaml_ng::Mapping>(&dns_yaml)
{
// 处理hosts配置
if let Some(hosts_value) = dns_config.get("hosts") {
if hosts_value.is_mapping() {

View File

@@ -1,6 +1,6 @@
use super::use_lowercase;
use anyhow::{Error, Result};
use serde_yaml::Mapping;
use serde_yaml_ng::Mapping;
pub fn use_script(
script: String,
@@ -149,11 +149,11 @@ fn test_script() {
enable: false
";
let config = serde_yaml::from_str(config).expect("Failed to parse test config YAML");
let config = serde_yaml_ng::from_str(config).expect("Failed to parse test config YAML");
let (config, results) = use_script(script.into(), config, "".to_string())
.expect("Script execution should succeed in test");
let _ = serde_yaml::to_string(&config).expect("Failed to serialize config to YAML");
let _ = serde_yaml_ng::to_string(&config).expect("Failed to serialize config to YAML");
let yaml_config_size = std::mem::size_of_val(&config);
let box_yaml_config_size = std::mem::size_of_val(&Box::new(config));
assert!(box_yaml_config_size < yaml_config_size);

View File

@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_yaml::{Mapping, Sequence, Value};
use serde_yaml_ng::{Mapping, Sequence, Value};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SeqMap {
@@ -86,7 +86,7 @@ pub fn use_seq(seq: SeqMap, mut config: Mapping, field: &str) -> Mapping {
mod tests {
use super::*;
#[allow(unused_imports)]
use serde_yaml::Value;
use serde_yaml_ng::Value;
#[test]
#[allow(clippy::unwrap_used)]
@@ -110,7 +110,7 @@ proxy-groups:
- "proxy1"
"#;
let mut config: Mapping =
serde_yaml::from_str(config_str).expect("Failed to parse test config YAML");
serde_yaml_ng::from_str(config_str).expect("Failed to parse test config YAML");
let seq = SeqMap {
prepend: Sequence::new(),

View File

@@ -1,4 +1,4 @@
use serde_yaml::{Mapping, Value};
use serde_yaml_ng::{Mapping, Value};
#[cfg(target_os = "macos")]
use crate::process::AsyncHandler;