mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 01:45:54 +08:00
feat: 新增 disabled.rs — 禁用路径 JSON 文件读写
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn disabled_file_path() -> PathBuf {
|
||||||
|
dirs::data_dir()
|
||||||
|
.or_else(dirs::home_dir)
|
||||||
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
|
.join("PathEditor")
|
||||||
|
.join("disabled.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
|
struct DisabledState {
|
||||||
|
#[serde(default)]
|
||||||
|
system: Vec<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
user: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 保存禁用路径列表(即时持久化,不依赖注册表保存按钮)
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn save_disabled_state(system: Vec<String>, user: Vec<String>) -> Result<(), String> {
|
||||||
|
let state = DisabledState { system, user };
|
||||||
|
let path = disabled_file_path();
|
||||||
|
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
fs::create_dir_all(parent)
|
||||||
|
.map_err(|e| format!("无法创建配置目录: {}", e))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let json = serde_json::to_string_pretty(&state)
|
||||||
|
.map_err(|e| format!("JSON 序列化失败: {}", e))?;
|
||||||
|
|
||||||
|
fs::write(&path, &json)
|
||||||
|
.map_err(|e| format!("无法写入 disabled.json: {}", e))?;
|
||||||
|
|
||||||
|
log::info!("已保存禁用状态到: {}", path.display());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 加载禁用路径列表,返回 (system_disabled, user_disabled)
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn load_disabled_state() -> Result<(Vec<String>, Vec<String>), String> {
|
||||||
|
let path = disabled_file_path();
|
||||||
|
|
||||||
|
if !path.exists() {
|
||||||
|
return Ok((vec![], vec![]));
|
||||||
|
}
|
||||||
|
|
||||||
|
let content = fs::read_to_string(&path)
|
||||||
|
.map_err(|e| format!("无法读取 disabled.json: {}", e))?;
|
||||||
|
|
||||||
|
if content.trim().is_empty() {
|
||||||
|
return Ok((vec![], vec![]));
|
||||||
|
}
|
||||||
|
|
||||||
|
let state: DisabledState = serde_json::from_str(&content)
|
||||||
|
.map_err(|e| format!("JSON 解析失败: {}", e))?;
|
||||||
|
|
||||||
|
Ok((state.system, state.user))
|
||||||
|
}
|
||||||
@@ -2,3 +2,4 @@ pub mod registry;
|
|||||||
pub mod system;
|
pub mod system;
|
||||||
pub mod backup;
|
pub mod backup;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
|
pub mod disabled;
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ pub fn run() {
|
|||||||
commands::backup::backup_registry,
|
commands::backup::backup_registry,
|
||||||
commands::backup::get_appdata_dir,
|
commands::backup::get_appdata_dir,
|
||||||
commands::fs::read_text_file,
|
commands::fs::read_text_file,
|
||||||
|
commands::disabled::save_disabled_state,
|
||||||
|
commands::disabled::load_disabled_state,
|
||||||
])
|
])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|||||||
Reference in New Issue
Block a user