refactor: 提取 core 库 + 新增 CLI 版本

- 创建 Cargo workspace(core / src-tauri / cli 三 crate)
- core: 纯 Rust 库,零 Tauri 依赖,包含所有业务逻辑
- src-tauri/commands: 改为薄包装,调用 core 函数
- cli: 基于 clap 的命令行工具,支持 JSON 输出
- CLI 命令: list, add, remove, conflicts, scan, profile, check-admin

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 23:13:28 +08:00
parent 5a864c41b2
commit cd896d389b
22 changed files with 6307 additions and 650 deletions
+5 -122
View File
@@ -1,127 +1,10 @@
use winreg::enums::*;
use winreg::RegKey;
pub(crate) const SYS_REG_PATH: &str = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
pub(crate) const USER_REG_PATH: &str = "Environment";
pub(crate) const PATH_VALUE: &str = "Path";
pub(crate) fn load_paths(root: winreg::HKEY, sub_path: &str, label: &str) -> Result<Vec<String>, String> {
let key = RegKey::predef(root);
let env_key = key
.open_subkey_with_flags(sub_path, KEY_READ)
.map_err(|e| format!("无法打开{}注册表项: {}", label, e))?;
let value: String = env_key
.get_value(PATH_VALUE)
.map_err(|e| format!("无法读取{} PATH: {}", label, e))?;
Ok(split_path(&value))
}
pub(crate) fn save_paths(root: winreg::HKEY, sub_path: &str, label: &str, paths: &[String]) -> Result<(), String> {
let value = join_path(paths);
// Windows 注册表 REG_EXPAND_SZ 上限 32767 字符
const MAX_PATH_LEN: usize = 32767;
if value.len() > MAX_PATH_LEN {
return Err(format!(
"{} PATH 总长度 {} 超出 Windows 限制 {} 字符,请移除部分路径后再保存",
label, value.len(), MAX_PATH_LEN
));
}
let key = RegKey::predef(root);
let env_key = key
.open_subkey_with_flags(sub_path, KEY_WRITE)
.map_err(|e| format!("无法写入{}注册表(需要管理员权限): {}", label, e))?;
env_key
.set_value(PATH_VALUE, &value)
.map_err(|e| format!("无法写入{} PATH: {}", label, e))?;
log::info!("已保存{} PATH{} 个条目", label, paths.len());
Ok(())
}
use path_editor_core::registry;
#[tauri::command]
pub fn load_system_paths() -> Result<Vec<String>, String> {
load_paths(HKEY_LOCAL_MACHINE, SYS_REG_PATH, "系统")
}
pub fn load_system_paths() -> Result<Vec<String>, String> { registry::load_system_paths() }
#[tauri::command]
pub fn load_user_paths() -> Result<Vec<String>, String> {
load_paths(HKEY_CURRENT_USER, USER_REG_PATH, "用户")
}
pub fn load_user_paths() -> Result<Vec<String>, String> { registry::load_user_paths() }
#[tauri::command]
pub fn save_system_paths(paths: Vec<String>) -> Result<(), String> {
save_paths(HKEY_LOCAL_MACHINE, SYS_REG_PATH, "系统", &paths)
}
pub fn save_system_paths(paths: Vec<String>) -> Result<(), String> { registry::save_system_paths(paths) }
#[tauri::command]
pub fn save_user_paths(paths: Vec<String>) -> Result<(), String> {
save_paths(HKEY_CURRENT_USER, USER_REG_PATH, "用户", &paths)
}
/// 将分号分隔的 PATH 字符串拆分为数组。
/// 注意:TS 端 src/core/validation.ts 有相同逻辑的 split_path,修改时需同步两端。
pub(crate) fn split_path(raw: &str) -> Vec<String> {
raw.split(';')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
}
pub(crate) fn join_path(paths: &[String]) -> String {
paths
.iter()
.map(|p| p.trim())
.filter(|p| !p.is_empty())
.collect::<Vec<_>>()
.join(";")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_empty() {
assert_eq!(split_path(""), Vec::<String>::new());
}
#[test]
fn split_single() {
assert_eq!(split_path("C:\\Windows"), vec!["C:\\Windows"]);
}
#[test]
fn split_multiple() {
assert_eq!(
split_path("C:\\Windows;D:\\Projects"),
vec!["C:\\Windows", "D:\\Projects"]
);
}
#[test]
fn split_trims_and_filters_empty() {
assert_eq!(
split_path(" C:\\ ; ; D:\\ "),
vec!["C:\\", "D:\\"]
);
}
#[test]
fn join_and_split_roundtrip() {
let paths = vec!["C:\\Windows".to_string(), "D:\\Projects".to_string()];
let joined = join_path(&paths);
let split = split_path(&joined);
assert_eq!(split, paths);
}
#[test]
fn join_trims_entries() {
let paths = vec![" C:\\Windows ".to_string(), " D:\\ ".to_string()];
assert_eq!(join_path(&paths), "C:\\Windows;D:\\");
}
}
pub fn save_user_paths(paths: Vec<String>) -> Result<(), String> { registry::save_user_paths(paths) }