From 69c0a0960465b99e2983260eee73458b2cad4de8 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 6 Nov 2025 16:22:05 +0800 Subject: [PATCH] refactor(cleanup): remove dead code and optimize Option checks - Remove 5 unused functions that were left over from migration refactoring: - get_archive_root, ensure_unique_path, archive_file (config.rs) - read_config_text_from_path, read_and_validate_config_from_path (codex_config.rs) - Simplify is_v1 check using is_some_and instead of map_or for better readability - Remove outdated comments about removed functions This eliminates all dead_code warnings and improves code maintainability. --- src-tauri/src/app_config.rs | 2 +- src-tauri/src/codex_config.rs | 18 ------------- src-tauri/src/config.rs | 50 ----------------------------------- 3 files changed, 1 insertion(+), 69 deletions(-) diff --git a/src-tauri/src/app_config.rs b/src-tauri/src/app_config.rs index 5eea813..4219ca8 100644 --- a/src-tauri/src/app_config.rs +++ b/src-tauri/src/app_config.rs @@ -106,7 +106,7 @@ impl MultiAppConfig { // 满足:顶层同时包含 providers(object) + current(string),且不包含 version/apps/mcp 关键键,即视为 v1 let value: serde_json::Value = serde_json::from_str(&content).map_err(|e| AppError::json(&config_path, e))?; - let is_v1 = value.as_object().map_or(false, |map| { + let is_v1 = value.as_object().is_some_and(|map| { let has_providers = map .get("providers") .map(|v| v.is_object()) diff --git a/src-tauri/src/codex_config.rs b/src-tauri/src/codex_config.rs index 3d5f9c7..ac13628 100644 --- a/src-tauri/src/codex_config.rs +++ b/src-tauri/src/codex_config.rs @@ -56,8 +56,6 @@ pub fn delete_codex_provider_config( Ok(()) } -//(移除未使用的备份/保存/恢复/导入函数,避免 dead_code 告警) - /// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步 pub fn write_codex_live_atomic( auth: &Value, @@ -118,15 +116,6 @@ pub fn read_codex_config_text() -> Result { } } -/// 从给定路径读取 config.toml 文本(路径存在时);路径不存在则返回空字符串 -pub fn read_config_text_from_path(path: &Path) -> Result { - if path.exists() { - std::fs::read_to_string(path).map_err(|e| AppError::io(path, e)) - } else { - Ok(String::new()) - } -} - /// 对非空的 TOML 文本进行语法校验 pub fn validate_config_toml(text: &str) -> Result<(), AppError> { if text.trim().is_empty() { @@ -143,10 +132,3 @@ pub fn read_and_validate_codex_config_text() -> Result { validate_config_toml(&s)?; Ok(s) } - -/// 从指定路径读取并校验 config.toml,返回文本(可能为空) -pub fn read_and_validate_config_from_path(path: &Path) -> Result { - let s = read_config_text_from_path(path)?; - validate_config_toml(&s)?; - Ok(s) -} diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 99192bb..84aca36 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -78,54 +78,6 @@ pub fn get_app_config_path() -> PathBuf { get_app_config_dir().join("config.json") } -/// 归档根目录 ~/.cc-switch/archive -pub fn get_archive_root() -> PathBuf { - get_app_config_dir().join("archive") -} - -fn ensure_unique_path(dest: PathBuf) -> PathBuf { - if !dest.exists() { - return dest; - } - let file_name = dest - .file_stem() - .map(|s| s.to_string_lossy().to_string()) - .unwrap_or_else(|| "file".into()); - let ext = dest - .extension() - .map(|s| format!(".{}", s.to_string_lossy())) - .unwrap_or_default(); - let parent = dest.parent().map(|p| p.to_path_buf()).unwrap_or_default(); - for i in 2..1000 { - let mut candidate = parent.clone(); - candidate.push(format!("{}-{}{}", file_name, i, ext)); - if !candidate.exists() { - return candidate; - } - } - dest -} - -/// 将现有文件归档到 `~/.cc-switch/archive///` 下,返回归档路径 -pub fn archive_file(ts: u64, category: &str, src: &Path) -> Result, AppError> { - if !src.exists() { - return Ok(None); - } - let mut dest_dir = get_archive_root(); - dest_dir.push(ts.to_string()); - dest_dir.push(category); - fs::create_dir_all(&dest_dir).map_err(|e| AppError::io(&dest_dir, e))?; - - let file_name = src - .file_name() - .map(|s| s.to_string_lossy().to_string()) - .unwrap_or_else(|| "file".into()); - let mut dest = dest_dir.join(file_name); - dest = ensure_unique_path(dest); - - copy_file(src, &dest)?; - Ok(Some(dest)) -} /// 清理供应商名称,确保文件名安全 pub fn sanitize_provider_name(name: &str) -> String { @@ -304,5 +256,3 @@ pub fn get_claude_config_status() -> ConfigStatus { path: path.to_string_lossy().to_string(), } } - -//(移除未使用的备份/导入函数,避免 dead_code 告警)