From bfb18cf003ec6cac9bc7404f0120f5953e460827 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 26 Dec 2025 22:15:09 +0800 Subject: [PATCH] refactor(profile): improve error handling for file not found case refactor(merge): simplify deep_merge function signature chore: remove unused fmt_bytes function and related tests chore: clean up help module by removing unused macros chore: remove format module from utils --- src-tauri/src/cmd/profile.rs | 3 +-- src-tauri/src/enhance/merge.rs | 6 +++--- src-tauri/src/utils/format.rs | 26 -------------------------- src-tauri/src/utils/help.rs | 15 --------------- src-tauri/src/utils/mod.rs | 1 - 5 files changed, 4 insertions(+), 47 deletions(-) delete mode 100644 src-tauri/src/utils/format.rs diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index c11531d2d..60bdb09c1 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -13,7 +13,6 @@ use crate::{ feat, module::auto_backup::{AutoBackupManager, AutoBackupTrigger}, process::AsyncHandler, - ret_err, utils::{dirs, help}, }; use clash_verge_draft::SharedDraft; @@ -455,7 +454,7 @@ pub async fn view_profile(index: String) -> CmdResult { let path = dirs::app_profiles_dir().stringify_err()?.join(file.as_str()); if !path.exists() { - ret_err!("the file not found"); + return CmdResult::Err(format!("file not found \"{}\"", path.display()).into()); } help::open_file(path).stringify_err() diff --git a/src-tauri/src/enhance/merge.rs b/src-tauri/src/enhance/merge.rs index 7579b585d..8e063d093 100644 --- a/src-tauri/src/enhance/merge.rs +++ b/src-tauri/src/enhance/merge.rs @@ -3,14 +3,14 @@ use clash_verge_logging::{Type, logging}; use super::use_lowercase; use serde_yaml_ng::{self, Mapping, Value}; -fn deep_merge(a: &mut Value, b: &Value) { +fn deep_merge(a: &mut Value, b: Value) { match (a, b) { (&mut Value::Mapping(ref mut a), Value::Mapping(b)) => { for (k, v) in b { deep_merge(a.entry(k.clone()).or_insert(Value::Null), v); } } - (a, b) => *a = b.clone(), + (a, b) => *a = b, } } @@ -18,7 +18,7 @@ pub fn use_merge(merge: &Mapping, config: Mapping) -> Mapping { let mut config = Value::from(config); let merge = use_lowercase(merge); - deep_merge(&mut config, &Value::from(merge)); + deep_merge(&mut config, Value::from(merge)); config.as_mapping().cloned().unwrap_or_else(|| { logging!( diff --git a/src-tauri/src/utils/format.rs b/src-tauri/src/utils/format.rs deleted file mode 100644 index 86437a496..000000000 --- a/src-tauri/src/utils/format.rs +++ /dev/null @@ -1,26 +0,0 @@ -/// Format bytes into human readable string (B, KB, MB, GB) -#[allow(unused)] -pub fn fmt_bytes(bytes: u64) -> String { - const UNITS: &[&str] = &["B", "KB", "MB", "GB"]; - let (mut val, mut unit) = (bytes as f64, 0); - while val >= 1024.0 && unit < 3 { - val /= 1024.0; - unit += 1; - } - format!("{:.1}{}", val, UNITS[unit]) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_fmt_bytes() { - assert_eq!(fmt_bytes(0), "0.0B"); - assert_eq!(fmt_bytes(512), "512.0B"); - assert_eq!(fmt_bytes(1024), "1.0KB"); - assert_eq!(fmt_bytes(1536), "1.5KB"); - assert_eq!(fmt_bytes(1024 * 1024), "1.0MB"); - assert_eq!(fmt_bytes(1024 * 1024 * 1024), "1.0GB"); - } -} diff --git a/src-tauri/src/utils/help.rs b/src-tauri/src/utils/help.rs index d676b31c7..7431a3870 100644 --- a/src-tauri/src/utils/help.rs +++ b/src-tauri/src/utils/help.rs @@ -134,18 +134,3 @@ pub fn linux_elevator() -> String { Err(_) => "sudo".to_string(), } } - -/// return the string literal error -#[macro_export] -macro_rules! ret_err { - ($str: expr) => { - return Err($str.into()) - }; -} - -#[macro_export] -macro_rules! t { - ($en:expr, $zh:expr, $use_zh:expr) => { - if $use_zh { $zh } else { $en } - }; -} diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs index faf542fe3..0746fbd3f 100644 --- a/src-tauri/src/utils/mod.rs +++ b/src-tauri/src/utils/mod.rs @@ -1,5 +1,4 @@ pub mod dirs; -pub mod format; pub mod help; pub mod i18n; pub mod init;