From 3e81dcd78fbc5a171753e09a85775fffcae745cb Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sun, 9 Nov 2025 16:59:43 +0800 Subject: [PATCH] refactor: simplify icon file search logic in find_target_icons function Try with 1.91.0 new API --- src-tauri/src/utils/dirs.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/utils/dirs.rs b/src-tauri/src/utils/dirs.rs index 56c74f2bc..f83af90f4 100644 --- a/src-tauri/src/utils/dirs.rs +++ b/src-tauri/src/utils/dirs.rs @@ -103,31 +103,18 @@ pub fn app_icons_dir() -> Result { pub fn find_target_icons(target: &str) -> Result> { let icons_dir = app_icons_dir()?; - let mut matching_files = Vec::new(); + let icon_path = fs::read_dir(&icons_dir)? + .filter_map(|entry| entry.ok().map(|e| e.path())) + .find(|path| { + path.file_prefix().is_some_and(|prefix| prefix == target) + && path + .extension() + .is_some_and(|ext| ext == "ico" || ext == "png") + }); - for entry in fs::read_dir(icons_dir)? { - let entry = entry?; - let path = entry.path(); - - if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) - && file_name.starts_with(target) - && (file_name.ends_with(".ico") || file_name.ends_with(".png")) - { - matching_files.push(path); - } - } - - if matching_files.is_empty() { - Ok(None) - } else { - match matching_files.first() { - Some(first_path) => { - let first = path_to_str(first_path)?; - Ok(Some(first.into())) - } - None => Ok(None), - } - } + icon_path + .map(|path| path_to_str(&path).map(|s| s.into())) + .transpose() } /// logs dir