From 58164cea150bc993eaecf7895c3e867e19c99717 Mon Sep 17 00:00:00 2001 From: Ridan Vandenbergh Date: Fri, 20 Jun 2025 20:48:44 +0200 Subject: [PATCH] Rename some structs for clarity --- src/lib.rs | 9 ++-- src/{search_dir.rs => search.rs} | 80 +++++++++++++++++++------------- src/theme.rs | 8 ++-- 3 files changed, 56 insertions(+), 41 deletions(-) rename src/{search_dir.rs => search.rs} (86%) diff --git a/src/lib.rs b/src/lib.rs index 96aa669..edd6830 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ //! # Quick start //! //! ``` -//! let dirs = icon::SearchDirectories::default(); +//! let dirs = icon::IconSearch::default(); //! // TODO. //! ``` //! @@ -24,7 +24,7 @@ //! While a number of directories should always be scanned for icons, the user or application is //! allowed to search additional directories as it sees fit. //! -//! [SearchDirectories] handles this part, and is also the main entrypoint for `icon`. +//! [IconSearch] handles this part, and is also the main entrypoint for `icon`. //! //! 2. *Parsing icon themes*: //! @@ -56,9 +56,8 @@ //! - it only supports a rust-native icon cache, which you cannot opt out of. //! - it provides only icon loading—you cannot use it to obtain information about Icon Themes. - mod icon; -mod search_dir; +mod search; pub mod theme; -pub use search_dir::*; +pub use search::*; diff --git a/src/search_dir.rs b/src/search.rs similarity index 86% rename from src/search_dir.rs rename to src/search.rs index 7eebf11..b8b9ea7 100644 --- a/src/search_dir.rs +++ b/src/search.rs @@ -1,5 +1,5 @@ use crate::icon::IconFile; -use crate::theme::{Theme, ThemeDescriptor, ThemeParseError}; +use crate::theme::{Icons, Theme, ThemeInfo, ThemeParseError}; use std::collections::HashMap; use std::ffi::{OsStr, OsString}; use std::path::PathBuf; @@ -9,40 +9,46 @@ use std::sync::Arc; /// /// By default, that is `$HOME/.icons`, `$XDG_DATA_DIRS/icons` and `/usr/share/pixmaps`. /// Applications may further add their own icon directories to this list, and users may extend or change the list. -/// The default list may be obtained using the `Default` implementation on `SearchDirectories` or its `default` method. +/// The default list may be obtained using the `Default` implementation on `IconSearch` or its `default` method. /// -/// To add directories to the instance, use [SearchDirectories::append]. +/// To add directories to the instance, use [IconSearch::add_directories]. /// -/// To construct a new `SearchDirectories` from a list, use the `From` implementation or construct it by hand. +/// To construct a new `IconSearch` from a list, use the `From` implementation or construct it by hand. /// /// # Example /// /// ``` -/// use icon::SearchDirectories; +/// use icon::IconSearch; /// -/// let dirs = SearchDirectories::default(); +/// let dirs = IconSearch::default(); /// // TODO /// ``` #[derive(Debug, Clone)] -pub struct SearchDirectories { +pub struct IconSearch { pub dirs: Vec, } -impl SearchDirectories { +impl IconSearch { + pub const fn new_empty() -> Self { + Self { + dirs: Vec::new() + } + } + pub fn default() -> Self { ::default() } - /// Add a list of directories to this `SearchDirectories` + /// Add a list of directories to this `IconSearch` /// /// # Example /// /// ``` - /// use icon::SearchDirectories; + /// use icon::IconSearch; /// - /// let dirs = SearchDirectories::default().append(["/home/root/.icons"]); + /// let dirs = IconSearch::default().add_directories(["/home/root/.icons"]); /// ``` - pub fn append(mut self, directories: I) -> Self + pub fn add_directories(mut self, directories: I) -> Self where I: IntoIterator, P: Into, @@ -100,11 +106,20 @@ pub struct IconLocations { } impl IconLocations { - pub fn resolve(&self) -> Vec> { + pub fn icons(self) -> Icons { + let themes = self.resolve(); + + Icons { + standalone_icons: self.standalone_icons, + themes, + } + } + + pub fn resolve(&self) -> HashMap> { self.resolve_only(self.themes_directories.keys()) } - pub fn resolve_only(&self, theme_names: I) -> Vec> + pub fn resolve_only(&self, theme_names: I) -> HashMap> where I: IntoIterator, S: AsRef, @@ -115,14 +130,14 @@ impl IconLocations { // To accommodate this, either one has to keep a list of visited icon themes every time they // perform a lookup, or avoid the issue altogether by removing redundant parents up-front. - // That second option is what this function does, paying a (rather small) one-time cost to - // make the rest of the API cleaner and smaller by guaranteeing that the returned icon themes + // That second option is what this function does, being to pay a (rather small) one-time cost to + // make the rest of the API cleaner and smaller. It guarantees that the returned icon themes // have dependencies that form a direct acyclic graph without redundant paths. fn collect_themes( name: &OsStr, locations: &IconLocations, - themes: &mut HashMap>, + themes: &mut HashMap>, ) { // Skip if we already have this theme. if themes.contains_key(name) { @@ -262,7 +277,7 @@ impl IconLocations { let theme = Theme { description: theme_desc, - parents, + inherits_from: parents, }; full_themes[theme_idx] = Some(Arc::new(theme)); @@ -279,10 +294,14 @@ impl IconLocations { // - all themes required by the inheritance tree of those themes, without duplicates, // - and an optimal chain (inheritance tree search order) for each theme. - full_themes + // and to wrap things up, let's zip the themes back up with their names + theme_names + .into_iter() + .zip(full_themes) + .collect::>() } - pub fn theme_description(&self, internal_name: S) -> std::io::Result + pub fn theme_description(&self, internal_name: S) -> std::io::Result where S: AsRef, { @@ -293,10 +312,7 @@ impl IconLocations { .get(internal_name) .ok_or_else(|| std::io::Error::other(ThemeParseError::NotAnIconTheme))?; - ThemeDescriptor::new_from_folders( - internal_name.to_string_lossy().into_owned(), - theme.clone(), - ) + ThemeInfo::new_from_folders(internal_name.to_string_lossy().into_owned(), theme.clone()) } pub fn standalone_icon(&self, icon_name: S) -> Option<&IconFile> @@ -311,8 +327,8 @@ impl IconLocations { } } -/// Anything that turns into an iterator of things that can become paths, can be turned into a `SearchDirectories`. -impl From for SearchDirectories +/// Anything that turns into an iterator of things that can become paths, can be turned into a `IconSearch`. +impl From for IconSearch where I: IntoIterator, P: Into, @@ -320,11 +336,11 @@ where fn from(value: I) -> Self { let dirs = value.into_iter().map(Into::into).collect(); - SearchDirectories { dirs } + IconSearch { dirs } } } -impl Default for SearchDirectories { +impl Default for IconSearch { fn default() -> Self { // "By default, apps should look in $HOME/.icons (for backwards compatibility), // in $XDG_DATA_DIRS/icons @@ -351,13 +367,13 @@ impl Default for SearchDirectories { #[cfg(test)] mod test { - use crate::search_dir::SearchDirectories; + use crate::search::IconSearch; // these tests assume certain applications are installed on the system they are ran on. #[test] fn test_find_standard_theme_and_icon() { - let dirs = SearchDirectories::default(); + let dirs = IconSearch::default(); let locations = dirs.find_icon_locations(); @@ -370,7 +386,7 @@ mod test { #[test] fn test_2() { - let result = SearchDirectories::default() + let result = IconSearch::default() .find_icon_locations() .theme_description("breeze") .unwrap(); @@ -380,7 +396,7 @@ mod test { #[test] fn test() { - let _dirs = SearchDirectories::default().find_icon_locations().resolve(); + let _dirs = IconSearch::default().find_icon_locations().resolve(); // it didn't panic. } diff --git a/src/theme.rs b/src/theme.rs index 2a06e94..f8bbdad 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -12,11 +12,11 @@ pub struct Icons { } pub struct Theme { - pub description: ThemeDescriptor, - pub parents: Vec>, + pub description: ThemeInfo, + pub inherits_from: Vec>, } -pub struct ThemeDescriptor { +pub struct ThemeInfo { pub internal_name: String, pub base_dirs: Vec, pub index_location: PathBuf, @@ -42,7 +42,7 @@ pub enum ThemeParseError { ParseError(#[from] freedesktop_entry_parser::ParseError), } -impl ThemeDescriptor { +impl ThemeInfo { pub fn new_from_folders(internal_name: String, folders: Vec) -> std::io::Result { let index_location = folders .iter()