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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use super::{IconDir, IconSizeType};

use std::{
    borrow::Borrow,
    path::{Path, PathBuf},
    sync::Arc,
};

/// Enum representing the different file types an icon can be.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum IconFileType {
    /// PNG file type
    PNG,

    /// SVG file type
    SVG,

    /// XPM file type
    XPM,
}

impl IconFileType {
    pub(crate) const fn types() -> &'static [IconFileType; 3] {
        const TYPES: [IconFileType; 3] = [IconFileType::PNG, IconFileType::SVG, IconFileType::XPM];

        &TYPES
    }
}

impl AsRef<str> for IconFileType {
    fn as_ref(&self) -> &'static str {
        match self {
            IconFileType::PNG => "png",
            IconFileType::SVG => "svg",
            IconFileType::XPM => "xpm",
        }
    }
}

/// Struct containing information about a single icon file on disk.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct IconFile {
    dir_info: Arc<IconDir>,
    path: PathBuf,
    icon_type: IconFileType,
}

impl IconFile {
    /// Returns information about the directory the icon file lives in.
    pub fn dir_info(&self) -> &IconDir {
        &self.dir_info
    }

    /// Returns this icon's path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns this icon's type.
    pub const fn icon_type(&self) -> IconFileType {
        self.icon_type
    }

    /// Returns this icon's size.
    pub fn size(&self) -> u16 {
        self.dir_info.size()
    }

    /// Returns this icon's scale.
    pub fn scale(&self) -> u16 {
        self.dir_info.scale()
    }

    /// Returns this icon's context.
    pub fn context(&self) -> Option<&str> {
        self.dir_info.context()
    }

    /// Returns this icon's type.
    pub fn size_type(&self) -> IconSizeType {
        self.dir_info.size_type()
    }

    /// Returns this icon's max size.
    pub fn max_size(&self) -> u16 {
        self.dir_info.max_size()
    }

    /// Returns this icon's min size.
    pub fn min_size(&self) -> u16 {
        self.dir_info.min_size()
    }

    /// Returns this icon's size threshold.
    pub fn threshold(&self) -> u16 {
        self.dir_info.threshold()
    }

    pub(crate) const fn new(
        dir_info: Arc<IconDir>,
        path: PathBuf,
        icon_type: IconFileType,
    ) -> Self {
        Self {
            dir_info,
            path,
            icon_type,
        }
    }
}

impl AsRef<Path> for IconFile {
    fn as_ref(&self) -> &Path {
        self.path()
    }
}

impl Borrow<Path> for IconFile {
    fn borrow(&self) -> &Path {
        self.path()
    }
}