Fix lints and formatting

This commit is contained in:
Ridan Vandenbergh 2025-06-15 17:54:55 +02:00
parent 4b36b2aa6f
commit 8812042414
No known key found for this signature in database
4 changed files with 26 additions and 25 deletions

View file

@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct IconFile {
pub path: PathBuf,
pub file_type: FileType
pub file_type: FileType,
}
impl IconFile {
@ -15,7 +15,6 @@ impl IconFile {
file_type,
})
}
}
#[derive(Debug, Copy, Clone)]
@ -40,4 +39,4 @@ impl FileType {
None
}
}
}
}

View file

@ -1,4 +1,3 @@
mod search_dir;
mod icon;
mod search_dir;
mod theme;

View file

@ -1,6 +1,6 @@
use crate::icon::IconFile;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use crate::icon::IconFile;
#[derive(Debug, Clone)]
pub struct SearchDirectories {
@ -14,26 +14,28 @@ impl SearchDirectories {
pub fn search_icons_and_theme_folders(&self) -> (Vec<IconFile>, HashMap<String, Vec<PathBuf>>) {
fn theme_name_from_path(path: &Path) -> Option<&str> {
let theme_name = path.components()
.nth_back(1); // get the second-to-last component (which should be the theme name)
let theme_name = path.components().nth_back(1); // get the second-to-last component (which should be the theme name)
Some(theme_name?.as_os_str().to_str()?)
theme_name?.as_os_str().to_str()
}
// "Each theme is stored as subdirectories of the base directories"
let (files, dirs) = self.dirs.iter()
let (files, dirs) = self
.dirs
.iter()
.flat_map(|base_dir| base_dir.read_dir()) // read the entries in each base dir
.flat_map(std::convert::identity) // merge all the iterators
.flat_map(std::convert::identity) // remove Err entries
.flatten() // merge all the iterators
.flatten() // remove Err entries
.filter_map(|entry| Some((entry.file_type().ok()?, entry))) // get file type for each entry and skip if fail
.partition::<Vec<_>, _>(|(ft, _)| ft.is_file());
// icons at the top-level in a base_dir don't belong to a theme, but must still be able to be found!
let files = files.into_iter()
let files = files
.into_iter()
.flat_map(|(_, entry)| IconFile::from_path(&entry.path()))
.collect::<Vec<_>>();
// "In at least one of the theme directories there must be a file called
// index.theme that describes the theme. The first index.theme found while
// searching the base directories in order is used"
@ -43,11 +45,12 @@ impl SearchDirectories {
for (_, dir) in dirs {
let theme_name = dir.file_name().to_string_lossy().into_owned();
theme_folders.entry(theme_name)
.or_insert(Default::default())
theme_folders
.entry(theme_name)
.or_default()
.push(dir.path());
}
(files, theme_folders)
}
}
@ -101,6 +104,10 @@ mod test {
let (icons, _indexes) = dirs.search_icons_and_theme_folders();
assert!(icons.iter().any(|i| i.path.file_name().and_then(|s| s.to_str()) == Some("htop.png")))
assert!(
icons
.iter()
.any(|i| i.path.file_name().and_then(|s| s.to_str()) == Some("htop.png"))
)
}
}

View file

@ -38,11 +38,8 @@ impl Theme<'_> {
let index_location = folders
.iter()
.map(|f| f.join("index.theme"))
.filter(|index_path| index_path.exists())
.next()
.ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::Other, ThemeParseError::NotAnIconTheme)
})?;
.find(|index_path| index_path.exists())
.ok_or_else(|| std::io::Error::other(ThemeParseError::NotAnIconTheme))?;
let index = ThemeIndex::parse_from_file(index_location.as_path())?;
@ -83,8 +80,7 @@ pub struct ThemeIndex<'a> {
impl<'a> ThemeIndex<'a> {
pub fn parse_from_file(path: &Path) -> std::io::Result<OwnedThemeIndex> {
let bytes = std::fs::read(path)?;
let index = ThemeIndex::parse(&bytes)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let index = ThemeIndex::parse(&bytes).map_err(std::io::Error::other)?;
Ok(index.into_owned())
}