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
use std::{convert::TryFrom, error::Error};

#[cfg(feature = "static_icons")]
include!(concat!(env!("OUT_DIR"), "/icons_map.rs"));

#[cfg(feature = "static_icons")]
pub fn icon_exists(icon_name: &str) -> bool {
    SYMBOLS.contains_key(icon_name)
}

#[cfg(feature = "static_icons")]
pub fn icon_keys() -> Vec<&'static str> {
    SYMBOLS.keys().map(|&k| k).collect()
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Icon<'a> {
    name: &'a str,
    symbol: &'a str,
}

impl<'a> Icon<'a> {
    pub fn new(name: &'a str, symbol: &'a str) -> Icon<'a> {
        Icon { name, symbol }
    }
    pub fn name(&self) -> &'a str {
        self.name
    }
    pub fn symbol(&self) -> &'a str {
        self.symbol
    }
}

#[cfg(feature = "static_icons")]
impl<'a> TryFrom<&'a str> for Icon<'a> {
    type Error = Box<dyn Error>;

    fn try_from(name: &'a str) -> Result<Self, Self::Error> {
        SYMBOLS
            .get(name)
            .map(|&symbol| Icon { name, symbol })
            .ok_or("Icon does not exist".into())
    }
}

#[cfg(test)]
mod tests {

    #[cfg(feature = "static_icons")]
    use super::{icon_keys, Icon, SYMBOLS};
    use std::convert::TryFrom;

    #[test]
    fn get_icon_symbol() {
        let icon = Icon::try_from("bluetooth");
        assert!(icon.is_ok());
        assert!(icon.unwrap().symbol.len() > 0);
    }

    #[test]
    fn get_icon_keys() {
        assert!(icon_keys().len() > 0);
        assert!(SYMBOLS.contains_key(icon_keys()[0]))
    }
}