mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-28 17:25:54 +08:00
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:
+5
-5
@@ -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<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();
|
||||
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}");
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ fn backup_base_dir() -> PathBuf {
|
||||
}
|
||||
|
||||
/// 获取 APPDATA 路径下的备份目录
|
||||
|
||||
pub fn get_appdata_dir() -> String {
|
||||
backup_base_dir().to_string_lossy().to_string()
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ struct DisabledState {
|
||||
}
|
||||
|
||||
/// 保存禁用路径列表(即时持久化,不依赖注册表保存按钮)
|
||||
|
||||
pub fn save_disabled_state(system: Vec<String>, user: Vec<String>) -> Result<(), String> {
|
||||
let state = DisabledState { system, user };
|
||||
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)
|
||||
|
||||
pub fn load_disabled_state() -> Result<(Vec<String>, Vec<String>), String> {
|
||||
let path = disabled_file_path();
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/// 读取文本文件内容(供前端原生对话框选择文件后使用)
|
||||
|
||||
pub fn read_text_file(path: &str) -> Result<String, String> {
|
||||
std::fs::read_to_string(path).map_err(|e| format!("无法读取文件: {}", e))
|
||||
}
|
||||
|
||||
+18
-12
@@ -37,7 +37,6 @@ pub struct ProfileData {
|
||||
}
|
||||
|
||||
/// 列出所有配置文件的元数据
|
||||
|
||||
pub fn list_profiles() -> Result<Vec<ProfileMeta>, String> {
|
||||
let dir = profiles_dir();
|
||||
if !dir.exists() {
|
||||
@@ -68,9 +67,8 @@ pub fn list_profiles() -> Result<Vec<ProfileMeta>, String> {
|
||||
}
|
||||
|
||||
/// 保存当前 PATH 为配置文件
|
||||
|
||||
pub fn save_profile(
|
||||
name: String,
|
||||
name: &str,
|
||||
sys: Vec<ProfilePathEntry>,
|
||||
user: Vec<ProfilePathEntry>,
|
||||
) -> 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::<ProfileData>(&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<ProfileData, String> {
|
||||
pub fn load_profile(name: &str) -> Result<ProfileData, String> {
|
||||
let path = profile_path(&name);
|
||||
if !path.exists() {
|
||||
return Err(format!("配置文件不存在: {}", name));
|
||||
@@ -110,8 +118,7 @@ pub fn load_profile(name: String) -> Result<ProfileData, String> {
|
||||
}
|
||||
|
||||
/// 删除配置文件
|
||||
|
||||
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);
|
||||
|
||||
@@ -70,7 +70,6 @@ pub fn scan_conflicts(paths: Vec<String>) -> Result<Vec<ConflictEntry>, String>
|
||||
/// 扫描 PATH 中各目录提供的可执行文件
|
||||
///
|
||||
/// query 非空时只返回文件名包含关键词的结果
|
||||
|
||||
pub fn scan_tools(paths: Vec<String>, query: String) -> Result<Vec<ToolGroup>, String> {
|
||||
let query_lower = query.to_lowercase();
|
||||
let mut groups: Vec<ToolGroup> = Vec::new();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -3,10 +3,10 @@ use path_editor_core::profiles;
|
||||
#[tauri::command]
|
||||
pub fn list_profiles() -> Result<Vec<profiles::ProfileMeta>, String> { profiles::list_profiles() }
|
||||
#[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]
|
||||
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]
|
||||
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) }
|
||||
|
||||
Reference in New Issue
Block a user