refactor: simplify icon file search logic in find_target_icons function

Try with 1.91.0 new API
This commit is contained in:
Tunglies 2025-11-09 16:59:43 +08:00
parent f644036e08
commit 3e81dcd78f
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8

View File

@ -103,31 +103,18 @@ pub fn app_icons_dir() -> Result<PathBuf> {
pub fn find_target_icons(target: &str) -> Result<Option<String>> {
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