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.
This commit is contained in:
Jason
2025-11-06 16:22:05 +08:00
parent 4e23250755
commit 69c0a09604
3 changed files with 1 additions and 69 deletions

View File

@@ -106,7 +106,7 @@ impl MultiAppConfig {
// 满足:顶层同时包含 providers(object) + current(string),且不包含 version/apps/mcp 关键键,即视为 v1 // 满足:顶层同时包含 providers(object) + current(string),且不包含 version/apps/mcp 关键键,即视为 v1
let value: serde_json::Value = let value: serde_json::Value =
serde_json::from_str(&content).map_err(|e| AppError::json(&config_path, e))?; 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 let has_providers = map
.get("providers") .get("providers")
.map(|v| v.is_object()) .map(|v| v.is_object())

View File

@@ -56,8 +56,6 @@ pub fn delete_codex_provider_config(
Ok(()) Ok(())
} }
//(移除未使用的备份/保存/恢复/导入函数,避免 dead_code 告警)
/// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步 /// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步
pub fn write_codex_live_atomic( pub fn write_codex_live_atomic(
auth: &Value, auth: &Value,
@@ -118,15 +116,6 @@ pub fn read_codex_config_text() -> Result<String, AppError> {
} }
} }
/// 从给定路径读取 config.toml 文本(路径存在时);路径不存在则返回空字符串
pub fn read_config_text_from_path(path: &Path) -> Result<String, AppError> {
if path.exists() {
std::fs::read_to_string(path).map_err(|e| AppError::io(path, e))
} else {
Ok(String::new())
}
}
/// 对非空的 TOML 文本进行语法校验 /// 对非空的 TOML 文本进行语法校验
pub fn validate_config_toml(text: &str) -> Result<(), AppError> { pub fn validate_config_toml(text: &str) -> Result<(), AppError> {
if text.trim().is_empty() { if text.trim().is_empty() {
@@ -143,10 +132,3 @@ pub fn read_and_validate_codex_config_text() -> Result<String, AppError> {
validate_config_toml(&s)?; validate_config_toml(&s)?;
Ok(s) Ok(s)
} }
/// 从指定路径读取并校验 config.toml返回文本可能为空
pub fn read_and_validate_config_from_path(path: &Path) -> Result<String, AppError> {
let s = read_config_text_from_path(path)?;
validate_config_toml(&s)?;
Ok(s)
}

View File

@@ -78,54 +78,6 @@ pub fn get_app_config_path() -> PathBuf {
get_app_config_dir().join("config.json") 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/<ts>/<category>/` 下,返回归档路径
pub fn archive_file(ts: u64, category: &str, src: &Path) -> Result<Option<PathBuf>, 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 { 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(), path: path.to_string_lossy().to_string(),
} }
} }
//(移除未使用的备份/导入函数,避免 dead_code 告警)