fix: 审查修复 — save_profile 保留原始 created、&str 参数、clippy 清理

- 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 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 23:26:54 +08:00
parent 812f39b159
commit 36e1c89b2e
8 changed files with 27 additions and 29 deletions
+5 -5
View File
@@ -61,7 +61,7 @@ fn exit_err(msg: &str) -> ! {
std::process::exit(1); 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" } 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 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 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(); 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}"); println!("已保存配置: {name}");
} }
fn profile_load(name: String) { 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()); println!("═══ 系统 PATH ({} 条) ═══", data.sys.len());
for e in &data.sys { println!(" [{}] {}", if e.enabled { "" } else { "" }, e.path); } for e in &data.sys { println!(" [{}] {}", if e.enabled { "" } else { "" }, e.path); }
println!("═══ 用户 PATH ({} 条) ═══", data.user.len()); println!("═══ 用户 PATH ({} 条) ═══", data.user.len());
@@ -196,7 +196,7 @@ fn profile_load(name: String) {
} }
fn profile_apply(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<String> = data.sys.into_iter().filter(|e| e.enabled).map(|e| e.path).collect(); let sys: Vec<String> = data.sys.into_iter().filter(|e| e.enabled).map(|e| e.path).collect();
let usr: Vec<String> = data.user.into_iter().filter(|e| e.enabled).map(|e| e.path).collect(); let usr: Vec<String> = 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)); 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) { 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}"); println!("已删除配置: {name}");
} }
-1
View File
@@ -12,7 +12,6 @@ fn backup_base_dir() -> PathBuf {
} }
/// 获取 APPDATA 路径下的备份目录 /// 获取 APPDATA 路径下的备份目录
pub fn get_appdata_dir() -> String { pub fn get_appdata_dir() -> String {
backup_base_dir().to_string_lossy().to_string() backup_base_dir().to_string_lossy().to_string()
} }
-2
View File
@@ -19,7 +19,6 @@ struct DisabledState {
} }
/// 保存禁用路径列表(即时持久化,不依赖注册表保存按钮) /// 保存禁用路径列表(即时持久化,不依赖注册表保存按钮)
pub fn save_disabled_state(system: Vec<String>, user: Vec<String>) -> Result<(), String> { pub fn save_disabled_state(system: Vec<String>, user: Vec<String>) -> Result<(), String> {
let state = DisabledState { system, user }; let state = DisabledState { system, user };
let path = disabled_file_path(); let path = disabled_file_path();
@@ -40,7 +39,6 @@ pub fn save_disabled_state(system: Vec<String>, user: Vec<String>) -> Result<(),
} }
/// 加载禁用路径列表,返回 (system_disabled, user_disabled) /// 加载禁用路径列表,返回 (system_disabled, user_disabled)
pub fn load_disabled_state() -> Result<(Vec<String>, Vec<String>), String> { pub fn load_disabled_state() -> Result<(Vec<String>, Vec<String>), String> {
let path = disabled_file_path(); let path = disabled_file_path();
-1
View File
@@ -1,5 +1,4 @@
/// 读取文本文件内容(供前端原生对话框选择文件后使用) /// 读取文本文件内容(供前端原生对话框选择文件后使用)
pub fn read_text_file(path: &str) -> Result<String, String> { pub fn read_text_file(path: &str) -> Result<String, String> {
std::fs::read_to_string(path).map_err(|e| format!("无法读取文件: {}", e)) std::fs::read_to_string(path).map_err(|e| format!("无法读取文件: {}", e))
} }
+18 -12
View File
@@ -37,7 +37,6 @@ pub struct ProfileData {
} }
/// 列出所有配置文件的元数据 /// 列出所有配置文件的元数据
pub fn list_profiles() -> Result<Vec<ProfileMeta>, String> { pub fn list_profiles() -> Result<Vec<ProfileMeta>, String> {
let dir = profiles_dir(); let dir = profiles_dir();
if !dir.exists() { if !dir.exists() {
@@ -68,9 +67,8 @@ pub fn list_profiles() -> Result<Vec<ProfileMeta>, String> {
} }
/// 保存当前 PATH 为配置文件 /// 保存当前 PATH 为配置文件
pub fn save_profile( pub fn save_profile(
name: String, name: &str,
sys: Vec<ProfilePathEntry>, sys: Vec<ProfilePathEntry>,
user: Vec<ProfilePathEntry>, user: Vec<ProfilePathEntry>,
) -> Result<(), String> { ) -> Result<(), String> {
@@ -80,11 +78,22 @@ pub fn save_profile(
let path = profile_path(&name); let path = profile_path(&name);
let now = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string(); 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::<ProfileData>(&c).ok())
.map(|d| d.created)
.unwrap_or_else(|| now.clone())
} else {
now.clone()
};
let data = ProfileData { let data = ProfileData {
name, name: name.to_string(),
sys, sys,
user, user,
created: now.clone(), created,
modified: now, modified: now,
}; };
@@ -97,8 +106,7 @@ pub fn save_profile(
} }
/// 加载配置文件 /// 加载配置文件
pub fn load_profile(name: &str) -> Result<ProfileData, String> {
pub fn load_profile(name: String) -> Result<ProfileData, String> {
let path = profile_path(&name); let path = profile_path(&name);
if !path.exists() { if !path.exists() {
return Err(format!("配置文件不存在: {}", name)); return Err(format!("配置文件不存在: {}", name));
@@ -110,8 +118,7 @@ pub fn load_profile(name: String) -> Result<ProfileData, String> {
} }
/// 删除配置文件 /// 删除配置文件
pub fn delete_profile(name: &str) -> Result<(), String> {
pub fn delete_profile(name: String) -> Result<(), String> {
let path = profile_path(&name); let path = profile_path(&name);
fs::remove_file(&path).map_err(|e| format!("无法删除配置文件: {}", e))?; fs::remove_file(&path).map_err(|e| format!("无法删除配置文件: {}", e))?;
log::info!("已删除配置: {}", path.display()); log::info!("已删除配置: {}", path.display());
@@ -119,8 +126,7 @@ pub fn delete_profile(name: String) -> Result<(), String> {
} }
/// 重命名配置文件 /// 重命名配置文件
pub fn rename_profile(old_name: &str, new_name: &str) -> Result<(), String> {
pub fn rename_profile(old_name: String, new_name: String) -> Result<(), String> {
let old_path = profile_path(&old_name); let old_path = profile_path(&old_name);
if !old_path.exists() { if !old_path.exists() {
return Err(format!("配置文件不存在: {}", old_name)); 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 = 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))?; 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(); data.modified = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string();
let new_path = profile_path(&new_name); let new_path = profile_path(&new_name);
-1
View File
@@ -70,7 +70,6 @@ pub fn scan_conflicts(paths: Vec<String>) -> Result<Vec<ConflictEntry>, String>
/// 扫描 PATH 中各目录提供的可执行文件 /// 扫描 PATH 中各目录提供的可执行文件
/// ///
/// query 非空时只返回文件名包含关键词的结果 /// query 非空时只返回文件名包含关键词的结果
pub fn scan_tools(paths: Vec<String>, query: String) -> Result<Vec<ToolGroup>, String> { pub fn scan_tools(paths: Vec<String>, query: String) -> Result<Vec<ToolGroup>, String> {
let query_lower = query.to_lowercase(); let query_lower = query.to_lowercase();
let mut groups: Vec<ToolGroup> = Vec::new(); let mut groups: Vec<ToolGroup> = Vec::new();
-3
View File
@@ -2,7 +2,6 @@ use winreg::enums::*;
use winreg::RegKey; use winreg::RegKey;
/// 检测当前进程是否有管理员权限(尝试写入系统注册表键) /// 检测当前进程是否有管理员权限(尝试写入系统注册表键)
pub fn check_admin() -> bool { pub fn check_admin() -> bool {
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
hklm.open_subkey_with_flags( 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 /// 展开路径中的环境变量(如 %JAVA_HOME%\bin → C:\Program Files\Java\jdk-17\bin
pub fn expand_env_vars(path: &str) -> String { pub fn expand_env_vars(path: &str) -> String {
if !path.contains('%') { if !path.contains('%') {
return path.to_string(); return path.to_string();
@@ -64,7 +62,6 @@ pub fn expand_env_vars(path: &str) -> String {
} }
/// 广播环境变量更改通知(WM_SETTINGCHANGE /// 广播环境变量更改通知(WM_SETTINGCHANGE
pub fn broadcast_env_change() { pub fn broadcast_env_change() {
const HWND_BROADCAST: isize = 0xFFFF; const HWND_BROADCAST: isize = 0xFFFF;
const WM_SETTINGCHANGE: u32 = 0x001A; const WM_SETTINGCHANGE: u32 = 0x001A;
+4 -4
View File
@@ -3,10 +3,10 @@ use path_editor_core::profiles;
#[tauri::command] #[tauri::command]
pub fn list_profiles() -> Result<Vec<profiles::ProfileMeta>, String> { profiles::list_profiles() } pub fn list_profiles() -> Result<Vec<profiles::ProfileMeta>, String> { profiles::list_profiles() }
#[tauri::command] #[tauri::command]
pub fn save_profile(name: String, sys: Vec<profiles::ProfilePathEntry>, user: Vec<profiles::ProfilePathEntry>) -> Result<(), String> { profiles::save_profile(name, sys, user) } pub fn save_profile(name: String, sys: Vec<profiles::ProfilePathEntry>, user: Vec<profiles::ProfilePathEntry>) -> Result<(), String> { profiles::save_profile(&name, sys, user) }
#[tauri::command] #[tauri::command]
pub fn load_profile(name: String) -> Result<profiles::ProfileData, String> { profiles::load_profile(name) } pub fn load_profile(name: String) -> Result<profiles::ProfileData, String> { profiles::load_profile(&name) }
#[tauri::command] #[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] #[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) }