icon_loader/
search_paths.rs

1use std::{borrow::Cow, path::PathBuf};
2
3use xdg::BaseDirectories;
4
5/// Enum that provides a list of directories to [`IconLoader`](crate::IconLoader) to search for icons in.
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
7pub enum SearchPaths {
8    /// Uses the `xdg` crate for system icon paths.
9    #[default]
10    System,
11
12    /// A custom set of paths.
13    Custom(Vec<PathBuf>),
14}
15
16impl SearchPaths {
17    /// Creates a custom `SearchPaths` from a list of directories.
18    pub fn custom<I, P>(iter: I) -> Self
19    where
20        I: IntoIterator<Item = P>,
21        P: Into<PathBuf>,
22    {
23        SearchPaths::Custom(iter.into_iter().map(P::into).collect())
24    }
25
26    pub(crate) fn paths(&self) -> Cow<[PathBuf]> {
27        match self {
28            SearchPaths::System => {
29                Cow::Owned(BaseDirectories::with_prefix("icons").get_data_dirs())
30            }
31            SearchPaths::Custom(dirs) => Cow::Borrowed(dirs),
32        }
33    }
34}
35
36impl<I, P> From<I> for SearchPaths
37where
38    I: IntoIterator<Item = P>,
39    P: Into<PathBuf>,
40{
41    fn from(iter: I) -> Self {
42        SearchPaths::custom(iter)
43    }
44}