~vpzom/hitide

ref: 7115b96d95181b352d09b803ca2b0138005ba28e hitide/icons/build.rs -rw-r--r-- 1.4 KiB
7115b96dColin Reeder Add French translation from https://dev.narwhal.city/comments/80 10 months ago
                                                                                
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();
}