diff --git a/src/icon.rs b/src/icon.rs index efec134..0f9b551 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -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 } } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 5d2ce3e..8f146eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -mod search_dir; mod icon; +mod search_dir; mod theme; - diff --git a/src/search_dir.rs b/src/search_dir.rs index f72716c..b92da14 100644 --- a/src/search_dir.rs +++ b/src/search_dir.rs @@ -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, HashMap>) { 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::, _>(|(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::>(); - + // "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")) + ) } } diff --git a/src/theme.rs b/src/theme.rs index 91d071e..cb2bca2 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -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 { 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()) }