summaryrefslogtreecommitdiffstats
path: root/icons/build.rs
blob: a9ae2de1d77c470d7f71b90771a287e7f3194542 (plain)
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
use std::hash::{Hash, Hasher};
use std::io::Write;

fn main() {
    println!("cargo:rerun-if-changed=res");
    let mut file =
        std::fs::File::create(format!("{}/icons.rs", std::env::var("OUT_DIR").unwrap())).unwrap();
    let mut mapping = Vec::new();

    writeln!(file, "use super::Icon;").unwrap();

    for res in std::fs::read_dir("res").unwrap() {
        let path = res.unwrap().path();
        if path.is_dir() {
            continue;
        }

        println!("{:?}", path);

        let content = std::fs::read(&path).unwrap();

        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        content.hash(&mut hasher);
        let hash = hasher.finish();

        let key = format!("{}.svg", hash);

        let name = path.file_name().unwrap().to_str().unwrap();
        let name = (&name[0..name.len() - 4]).to_ascii_uppercase();

        writeln!(
            file,
            "pub const {}: Icon=Icon{{path:\"{}\",content:include_str!(\"{}\")}};",
            name,
            key,
            path.canonicalize().unwrap().to_str().unwrap()
        )
        .unwrap();

        mapping.push((key, name));
    }

    writeln!(
        file,
        "pub const ICONS_MAP: phf::Map<&'static str, &'static Icon> = phf::phf_map! {{"
    )
    .unwrap();
    for (key, name) in mapping {
        writeln!(file, "\"{}\" => &{},", key, name).unwrap();
    }
    writeln!(file, "}};").unwrap();
}