cosmic_freedesktop_icons/theme/
parse.rs

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::theme::directories::{Directory, DirectoryType};
use crate::theme::Theme;

fn icon_theme_section(file: &str) -> impl Iterator<Item = (&str, &str)> + '_ {
    ini_core::Parser::new(file)
        .skip_while(|item| *item != ini_core::Item::Section("Icon Theme"))
        .take_while(|item| match item {
            ini_core::Item::Section(value) => *value == "Icon Theme",
            _ => true,
        })
        .filter_map(|item| {
            if let ini_core::Item::Property(key, value) = item {
                Some((key, value?))
            } else {
                None
            }
        })
}

#[derive(Debug)]
enum DirectorySection<'a> {
    Property(&'a str, &'a str),
    EndSection,
    Section(&'a str),
}

fn sections(file: &str) -> impl Iterator<Item = DirectorySection> {
    ini_core::Parser::new(file).filter_map(move |item| match item {
        ini_core::Item::Property(key, Some(value)) => Some(DirectorySection::Property(key, value)),
        ini_core::Item::Section(section) => Some(DirectorySection::Section(section)),
        ini_core::Item::SectionEnd => Some(DirectorySection::EndSection),
        _ => None,
    })
}

impl Theme {
    pub(super) fn get_all_directories<'a>(
        &'a self,
        file: &'a str,
    ) -> impl Iterator<Item = Directory<'a>> + 'a {
        let mut iterator = sections(file);

        std::iter::from_fn(move || {
            let mut name = "";
            let mut size = None;
            let mut max_size = None;
            let mut min_size = None;
            let mut threshold = None;
            let mut scale = None;
            // let mut context = None;
            let mut dtype = DirectoryType::default();

            #[allow(clippy::while_let_on_iterator)]
            while let Some(event) = iterator.next() {
                match event {
                    DirectorySection::Property(key, value) => {
                        if name.is_empty() || name == "Icon Theme" {
                            continue;
                        }

                        match key {
                            "Size" => size = str::parse(value).ok(),
                            "Scale" => scale = str::parse(value).ok(),
                            // "Context" => context = Some(value),
                            "Type" => dtype = DirectoryType::from(value),
                            "MaxSize" => max_size = str::parse(value).ok(),
                            "MinSize" => min_size = str::parse(value).ok(),
                            "Threshold" => threshold = str::parse(value).ok(),
                            _ => (),
                        }
                    }

                    DirectorySection::Section(new_name) => {
                        name = new_name;
                        size = None;
                        max_size = None;
                        min_size = None;
                        threshold = None;
                        scale = None;
                        dtype = DirectoryType::default();
                    }

                    DirectorySection::EndSection => {
                        if name.is_empty() || name == "Icon Theme" {
                            continue;
                        }

                        let size = size.take()?;

                        return Some(Directory {
                            name,
                            size,
                            scale: scale.unwrap_or(1),
                            // context,
                            type_: dtype,
                            maxsize: max_size.unwrap_or(size),
                            minsize: min_size.unwrap_or(size),
                            threshold: threshold.unwrap_or(2),
                        });
                    }
                }
            }

            None
        })
    }

    pub fn inherits<'a>(&self, file: &'a str) -> Vec<&'a str> {
        icon_theme_section(file)
            .find(|&(key, _)| key == "Inherits")
            .map(|(_, parents)| {
                parents
                    .split(',')
                    // Filtering out 'hicolor' since we are going to fallback there anyway
                    .filter(|parent| parent != &"hicolor")
                    .collect()
            })
            .unwrap_or_default()
    }
}

#[cfg(test)]
mod test {
    use crate::THEMES;
    use speculoos::prelude::*;

    #[test]
    fn should_get_theme_parents() {
        for theme in THEMES.get("Arc").unwrap() {
            let file = crate::theme::read_ini_theme(&theme.index).ok().unwrap();
            let file = std::str::from_utf8(file.as_ref()).ok().unwrap();
            let parents = theme.inherits(file);

            assert_that!(parents).does_not_contain("hicolor");

            assert_that!(parents).is_equal_to(vec![
                "Moka",
                "Faba",
                "elementary",
                "Adwaita",
                "gnome",
            ]);
        }
    }
}