icon_loader/icon/
icon_file.rs

1use super::{IconDir, IconSizeType};
2
3use std::{
4    borrow::Borrow,
5    path::{Path, PathBuf},
6    sync::Arc,
7};
8
9/// Enum representing the different file types an icon can be.
10#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
11pub enum IconFileType {
12    /// PNG file type
13    PNG,
14
15    /// SVG file type
16    SVG,
17
18    /// XPM file type
19    XPM,
20}
21
22impl IconFileType {
23    pub(crate) const fn types() -> &'static [IconFileType; 3] {
24        const TYPES: [IconFileType; 3] = [IconFileType::PNG, IconFileType::SVG, IconFileType::XPM];
25
26        &TYPES
27    }
28}
29
30impl AsRef<str> for IconFileType {
31    fn as_ref(&self) -> &'static str {
32        match self {
33            IconFileType::PNG => "png",
34            IconFileType::SVG => "svg",
35            IconFileType::XPM => "xpm",
36        }
37    }
38}
39
40/// Struct containing information about a single icon file on disk.
41#[derive(Clone, Debug, Hash, PartialEq, Eq)]
42pub struct IconFile {
43    dir_info: Arc<IconDir>,
44    path: PathBuf,
45    icon_type: IconFileType,
46}
47
48impl IconFile {
49    /// Returns information about the directory the icon file lives in.
50    pub fn dir_info(&self) -> &IconDir {
51        &self.dir_info
52    }
53
54    /// Returns this icon's path.
55    pub fn path(&self) -> &Path {
56        &self.path
57    }
58
59    /// Returns this icon's type.
60    pub const fn icon_type(&self) -> IconFileType {
61        self.icon_type
62    }
63
64    /// Returns this icon's size.
65    pub fn size(&self) -> u16 {
66        self.dir_info.size()
67    }
68
69    /// Returns this icon's scale.
70    pub fn scale(&self) -> u16 {
71        self.dir_info.scale()
72    }
73
74    /// Returns this icon's context.
75    pub fn context(&self) -> Option<&str> {
76        self.dir_info.context()
77    }
78
79    /// Returns this icon's type.
80    pub fn size_type(&self) -> IconSizeType {
81        self.dir_info.size_type()
82    }
83
84    /// Returns this icon's max size.
85    pub fn max_size(&self) -> u16 {
86        self.dir_info.max_size()
87    }
88
89    /// Returns this icon's min size.
90    pub fn min_size(&self) -> u16 {
91        self.dir_info.min_size()
92    }
93
94    /// Returns this icon's size threshold.
95    pub fn threshold(&self) -> u16 {
96        self.dir_info.threshold()
97    }
98
99    pub(crate) const fn new(
100        dir_info: Arc<IconDir>,
101        path: PathBuf,
102        icon_type: IconFileType,
103    ) -> Self {
104        Self {
105            dir_info,
106            path,
107            icon_type,
108        }
109    }
110}
111
112impl AsRef<Path> for IconFile {
113    fn as_ref(&self) -> &Path {
114        self.path()
115    }
116}
117
118impl Borrow<Path> for IconFile {
119    fn borrow(&self) -> &Path {
120        self.path()
121    }
122}