From 36e1c89b2e13e311a7a7a34084e4db0e2e86d5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Thu, 28 May 2026 23:26:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=A1=E6=9F=A5=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?=E2=80=94=20save=5Fprofile=20=E4=BF=9D=E7=95=99=E5=8E=9F?= =?UTF-8?q?=E5=A7=8B=20created=E3=80=81&str=20=E5=8F=82=E6=95=B0=E3=80=81c?= =?UTF-8?q?lippy=20=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CRITICAL: save_profile 覆盖已有配置时保留原始创建时间 - HIGH: profiles.rs 函数参数 String → &str(减少不必要的克隆) - MEDIUM: 修复 18 个 clippy警告(空行 + map_or + collapsible-if) - CLI: 移除不必要的 name.clone() 调用 Co-Authored-By: Claude Opus 4.7 --- cli/src/main.rs | 10 +++++----- core/src/backup.rs | 1 - core/src/disabled.rs | 2 -- core/src/fs.rs | 1 - core/src/profiles.rs | 30 ++++++++++++++++++------------ core/src/scanner.rs | 1 - core/src/system.rs | 3 --- gui/src/commands/profiles.rs | 8 ++++---- 8 files changed, 27 insertions(+), 29 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 3260819..0942668 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -61,7 +61,7 @@ fn exit_err(msg: &str) -> ! { std::process::exit(1); } -fn pick_target(system: bool, user: bool) -> &'static str { +fn pick_target(system: bool, _user: bool) -> &'static str { if system { "system" } else { "user" } } @@ -183,12 +183,12 @@ fn profile_save(name: String) { let usr = core::registry::load_user_paths().unwrap_or_else(|e| exit_err(&e)); let sys_entries = sys.into_iter().map(|p| core::profiles::ProfilePathEntry { path: p, enabled: true }).collect(); let usr_entries = usr.into_iter().map(|p| core::profiles::ProfilePathEntry { path: p, enabled: true }).collect(); - core::profiles::save_profile(name.clone(), sys_entries, usr_entries).unwrap_or_else(|e| exit_err(&e)); + core::profiles::save_profile(&name, sys_entries, usr_entries).unwrap_or_else(|e| exit_err(&e)); println!("已保存配置: {name}"); } fn profile_load(name: String) { - let data = core::profiles::load_profile(name.clone()).unwrap_or_else(|e| exit_err(&e)); + let data = core::profiles::load_profile(&name).unwrap_or_else(|e| exit_err(&e)); println!("═══ 系统 PATH ({} 条) ═══", data.sys.len()); for e in &data.sys { println!(" [{}] {}", if e.enabled { "✓" } else { "✗" }, e.path); } println!("═══ 用户 PATH ({} 条) ═══", data.user.len()); @@ -196,7 +196,7 @@ fn profile_load(name: String) { } fn profile_apply(name: String) { - let data = core::profiles::load_profile(name.clone()).unwrap_or_else(|e| exit_err(&e)); + let data = core::profiles::load_profile(&name).unwrap_or_else(|e| exit_err(&e)); let sys: Vec = data.sys.into_iter().filter(|e| e.enabled).map(|e| e.path).collect(); let usr: Vec = data.user.into_iter().filter(|e| e.enabled).map(|e| e.path).collect(); core::registry::save_system_paths(sys).unwrap_or_else(|e| exit_err(&e)); @@ -206,7 +206,7 @@ fn profile_apply(name: String) { } fn profile_delete(name: String) { - core::profiles::delete_profile(name.clone()).unwrap_or_else(|e| exit_err(&e)); + core::profiles::delete_profile(&name).unwrap_or_else(|e| exit_err(&e)); println!("已删除配置: {name}"); } diff --git a/core/src/backup.rs b/core/src/backup.rs index 1471c20..20c6ad3 100644 --- a/core/src/backup.rs +++ b/core/src/backup.rs @@ -12,7 +12,6 @@ fn backup_base_dir() -> PathBuf { } /// 获取 APPDATA 路径下的备份目录 - pub fn get_appdata_dir() -> String { backup_base_dir().to_string_lossy().to_string() } diff --git a/core/src/disabled.rs b/core/src/disabled.rs index 791bcd9..bd6d144 100644 --- a/core/src/disabled.rs +++ b/core/src/disabled.rs @@ -19,7 +19,6 @@ struct DisabledState { } /// 保存禁用路径列表(即时持久化,不依赖注册表保存按钮) - pub fn save_disabled_state(system: Vec, user: Vec) -> Result<(), String> { let state = DisabledState { system, user }; let path = disabled_file_path(); @@ -40,7 +39,6 @@ pub fn save_disabled_state(system: Vec, user: Vec) -> Result<(), } /// 加载禁用路径列表,返回 (system_disabled, user_disabled) - pub fn load_disabled_state() -> Result<(Vec, Vec), String> { let path = disabled_file_path(); diff --git a/core/src/fs.rs b/core/src/fs.rs index b2ddea2..7ab2788 100644 --- a/core/src/fs.rs +++ b/core/src/fs.rs @@ -1,5 +1,4 @@ /// 读取文本文件内容(供前端原生对话框选择文件后使用) - pub fn read_text_file(path: &str) -> Result { std::fs::read_to_string(path).map_err(|e| format!("无法读取文件: {}", e)) } diff --git a/core/src/profiles.rs b/core/src/profiles.rs index 62cd9a6..c76ab3b 100644 --- a/core/src/profiles.rs +++ b/core/src/profiles.rs @@ -37,7 +37,6 @@ pub struct ProfileData { } /// 列出所有配置文件的元数据 - pub fn list_profiles() -> Result, String> { let dir = profiles_dir(); if !dir.exists() { @@ -68,9 +67,8 @@ pub fn list_profiles() -> Result, String> { } /// 保存当前 PATH 为配置文件 - pub fn save_profile( - name: String, + name: &str, sys: Vec, user: Vec, ) -> Result<(), String> { @@ -80,11 +78,22 @@ pub fn save_profile( let path = profile_path(&name); let now = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string(); + // 覆盖已有配置时保留原始创建时间 + let created = if path.exists() { + fs::read_to_string(&path) + .ok() + .and_then(|c| serde_json::from_str::(&c).ok()) + .map(|d| d.created) + .unwrap_or_else(|| now.clone()) + } else { + now.clone() + }; + let data = ProfileData { - name, + name: name.to_string(), sys, user, - created: now.clone(), + created, modified: now, }; @@ -97,8 +106,7 @@ pub fn save_profile( } /// 加载配置文件 - -pub fn load_profile(name: String) -> Result { +pub fn load_profile(name: &str) -> Result { let path = profile_path(&name); if !path.exists() { return Err(format!("配置文件不存在: {}", name)); @@ -110,8 +118,7 @@ pub fn load_profile(name: String) -> Result { } /// 删除配置文件 - -pub fn delete_profile(name: String) -> Result<(), String> { +pub fn delete_profile(name: &str) -> Result<(), String> { let path = profile_path(&name); fs::remove_file(&path).map_err(|e| format!("无法删除配置文件: {}", e))?; log::info!("已删除配置: {}", path.display()); @@ -119,8 +126,7 @@ pub fn delete_profile(name: String) -> Result<(), String> { } /// 重命名配置文件 - -pub fn rename_profile(old_name: String, new_name: String) -> Result<(), String> { +pub fn rename_profile(old_name: &str, new_name: &str) -> Result<(), String> { let old_path = profile_path(&old_name); if !old_path.exists() { return Err(format!("配置文件不存在: {}", old_name)); @@ -129,7 +135,7 @@ pub fn rename_profile(old_name: String, new_name: String) -> Result<(), String> let mut data: ProfileData = serde_json::from_str(&fs::read_to_string(&old_path).map_err(|e| format!("无法读取配置文件: {}", e))?).map_err(|e| format!("JSON 解析失败: {}", e))?; - data.name = new_name.clone(); + data.name = new_name.to_string(); data.modified = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string(); let new_path = profile_path(&new_name); diff --git a/core/src/scanner.rs b/core/src/scanner.rs index 31cecf1..ebb3c1c 100644 --- a/core/src/scanner.rs +++ b/core/src/scanner.rs @@ -70,7 +70,6 @@ pub fn scan_conflicts(paths: Vec) -> Result, String> /// 扫描 PATH 中各目录提供的可执行文件 /// /// query 非空时只返回文件名包含关键词的结果 - pub fn scan_tools(paths: Vec, query: String) -> Result, String> { let query_lower = query.to_lowercase(); let mut groups: Vec = Vec::new(); diff --git a/core/src/system.rs b/core/src/system.rs index fb838b2..4c41cb1 100644 --- a/core/src/system.rs +++ b/core/src/system.rs @@ -2,7 +2,6 @@ use winreg::enums::*; use winreg::RegKey; /// 检测当前进程是否有管理员权限(尝试写入系统注册表键) - pub fn check_admin() -> bool { let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); hklm.open_subkey_with_flags( @@ -23,7 +22,6 @@ pub fn validate_path(path: &str) -> bool { } /// 展开路径中的环境变量(如 %JAVA_HOME%\bin → C:\Program Files\Java\jdk-17\bin) - pub fn expand_env_vars(path: &str) -> String { if !path.contains('%') { return path.to_string(); @@ -64,7 +62,6 @@ pub fn expand_env_vars(path: &str) -> String { } /// 广播环境变量更改通知(WM_SETTINGCHANGE) - pub fn broadcast_env_change() { const HWND_BROADCAST: isize = 0xFFFF; const WM_SETTINGCHANGE: u32 = 0x001A; diff --git a/gui/src/commands/profiles.rs b/gui/src/commands/profiles.rs index 7260b41..2bf8b32 100644 --- a/gui/src/commands/profiles.rs +++ b/gui/src/commands/profiles.rs @@ -3,10 +3,10 @@ use path_editor_core::profiles; #[tauri::command] pub fn list_profiles() -> Result, String> { profiles::list_profiles() } #[tauri::command] -pub fn save_profile(name: String, sys: Vec, user: Vec) -> Result<(), String> { profiles::save_profile(name, sys, user) } +pub fn save_profile(name: String, sys: Vec, user: Vec) -> Result<(), String> { profiles::save_profile(&name, sys, user) } #[tauri::command] -pub fn load_profile(name: String) -> Result { profiles::load_profile(name) } +pub fn load_profile(name: String) -> Result { profiles::load_profile(&name) } #[tauri::command] -pub fn delete_profile(name: String) -> Result<(), String> { profiles::delete_profile(name) } +pub fn delete_profile(name: String) -> Result<(), String> { profiles::delete_profile(&name) } #[tauri::command] -pub fn rename_profile(old_name: String, new_name: String) -> Result<(), String> { profiles::rename_profile(old_name, new_name) } +pub fn rename_profile(old_name: String, new_name: String) -> Result<(), String> { profiles::rename_profile(&old_name, &new_name) }