1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::{borrow::Cow, path::PathBuf};

use xdg::BaseDirectories;

/// Enum that provides a list of directories to [`IconLoader`] to search for icons in.
///
/// [`IconLoader`]: struct.IconLoader.html
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SearchPaths {
    /// Uses the `xdg` crate for system icon paths.
    System,

    /// A custom set of paths.
    Custom(Vec<PathBuf>),
}

impl SearchPaths {
    /// Creates a custom `SearchPaths` from a list of directories.
    pub fn custom<I, P>(iter: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        SearchPaths::Custom(iter.into_iter().map(P::into).collect())
    }

    pub(crate) fn paths(&self) -> Cow<[PathBuf]> {
        match self {
            SearchPaths::System => Cow::Owned(BaseDirectories::with_prefix("icons").map_or_else(
                |_| vec![PathBuf::from("/usr/share/icons")],
                |bd| bd.get_data_dirs(),
            )),
            SearchPaths::Custom(dirs) => Cow::Borrowed(dirs),
        }
    }
}

impl<I, P> From<I> for SearchPaths
where
    I: IntoIterator<Item = P>,
    P: Into<PathBuf>,
{
    fn from(iter: I) -> Self {
        SearchPaths::custom(iter)
    }
}