feat: Optimize KPM file processing logic, use is_some_and method to simplify extension checking, and enhance readability and stability of event processing.

This commit is contained in:
ShirkNeko
2025-04-07 22:28:22 +08:00
parent 138dec35c7
commit b850336872
2 changed files with 37 additions and 34 deletions

View File

@@ -103,7 +103,7 @@ pub fn on_post_data_fs() -> Result<()> {
for entry in std::fs::read_dir(kpm::KPM_DIR)? { for entry in std::fs::read_dir(kpm::KPM_DIR)? {
let path = entry?.path(); let path = entry?.path();
if path.extension().map_or(false, |ext| ext == "kpm") { if path.extension().is_some_and(|ext| ext == "kpm") {
let _ = kpm::load_kpm(&path); let _ = kpm::load_kpm(&path);
} }
} }

View File

@@ -37,26 +37,26 @@ pub fn start_kpm_watcher() -> Result<()> {
Ok(()) Ok(())
} }
// 处理 KPM 文件事件 pub fn handle_kpm_event(event: notify::Event) {
fn handle_kpm_event(event: notify::Event) { match event.kind {
if event.kind.is_create() { notify::EventKind::Create(_) => {
event.paths.iter().for_each(|path| { event.paths.iter().for_each(|path| {
if path.extension().map_or(false, |ext| ext == "kpm") { if path.extension().is_some_and(|ext| ext == "kpm") {
let _ = load_kpm(path); let _ = load_kpm(path);
} }
}); });
} }
notify::EventKind::Remove(_) => {
if event.kind.is_remove() {
event.paths.iter().for_each(|path| { event.paths.iter().for_each(|path| {
if let Some(name) = path.file_stem() { if let Some(name) = path.file_stem() {
let _ = unload_kpm(name.to_string_lossy().as_ref()); let _ = unload_kpm(name.to_string_lossy().as_ref());
} }
}); });
} }
_ => {}
}
} }
// 加载 KPM 模块
pub fn load_kpm(path: &Path) -> Result<()> { pub fn load_kpm(path: &Path) -> Result<()> {
let status = std::process::Command::new(KPMMGR_PATH) let status = std::process::Command::new(KPMMGR_PATH)
.args(["load", path.to_str().unwrap(), ""]) .args(["load", path.to_str().unwrap(), ""])
@@ -68,35 +68,38 @@ pub fn load_kpm(path: &Path) -> Result<()> {
Ok(()) Ok(())
} }
// 卸载 KPM 模块并删除文件
pub fn unload_kpm(name: &str) -> Result<()> { pub fn unload_kpm(name: &str) -> Result<()> {
let status = std::process::Command::new(KPMMGR_PATH) let status = std::process::Command::new(KPMMGR_PATH)
.args(["unload", name]) .args(["unload", name])
.status()?; .status()
.map_err(|e| anyhow!("Failed to execute kpmmgr: {}", e))?;
if status.success() {
let kpm_path = Path::new(KPM_DIR).join(format!("{}.kpm", name)); let kpm_path = Path::new(KPM_DIR).join(format!("{}.kpm", name));
if kpm_path.exists() { if kpm_path.exists() {
if let Err(e) = fs::remove_file(&kpm_path) { fs::remove_file(&kpm_path)
log::error!("Failed to delete KPM file: {}", e); .map_err(|e| anyhow!("Failed to delete KPM file: {}", e))
.map(|_| log::info!("Deleted KPM file: {}", kpm_path.display()))?;
}
if status.success() {
log::info!("Successfully unloaded KPM: {}", name);
} else { } else {
log::info!("Deleted KPM files: {}", kpm_path.display()); log::warn!("KPM unloading may have failed: {}", name);
}
}
log::info!("Uninstalled KPM: {}", name);
} }
Ok(()) Ok(())
} }
// 删除所有 KPM 模块
pub fn remove_all_kpms() -> Result<()> { pub fn remove_all_kpms() -> Result<()> {
ensure_kpm_dir()?; ensure_kpm_dir()?;
for entry in fs::read_dir(KPM_DIR)? { for entry in fs::read_dir(KPM_DIR)? {
let path = entry?.path(); let path = entry?.path();
if path.extension().map_or(false, |ext| ext == "kpm") { if path.extension().is_some_and(|ext| ext == "kpm") {
if let Some(name) = path.file_stem() { if let Some(name) = path.file_stem() {
let _ = unload_kpm(name.to_string_lossy().as_ref()); unload_kpm(name.to_string_lossy().as_ref())
.unwrap_or_else(|e| log::error!("Failed to remove KPM: {}", e));
let _ = fs::remove_file(&path);
} }
} }
} }