Debian Codegraph @

build/named.rs

raw symbols SCIP
1 use std::fs::File;
2
3 pub fn build() {
4 use std::path::Path;
5
6 let out_dir = ::std::env::var("OUT_DIR").unwrap();
7 let dest_path = Path::new(&out_dir).join("named.rs");
8 let mut writer = File::create(dest_path).expect("couldn't create named.rs");
9 build_colors(&mut writer);
10 }
11
12 #[cfg(feature = "named")]
13 pub fn build_colors(writer: &mut File) {
14 use std::io::{BufRead, BufReader, Write};
15
16 let reader =
17 BufReader::new(File::open("build/svg_colors.txt").expect("could not open svg_colors.txt"));
18 let mut entries = vec![];
19
20 for line in reader.lines() {
21 let line = line.unwrap();
22 let mut parts = line.split('\t');
23 let name = parts.next().expect("couldn't get the color name");
24 let mut rgb = parts
25 .next()
26 .unwrap_or_else(|| panic!("couldn't get color for {}", name))
27 .split(", ");
28 let red: u8 = rgb
29 .next()
30 .and_then(|r| r.trim().parse().ok())
31 .unwrap_or_else(|| panic!("couldn't get red for {}", name));
32 let green: u8 = rgb
33 .next()
34 .and_then(|r| r.trim().parse().ok())
35 .unwrap_or_else(|| panic!("couldn't get green for {}", name));
36 let blue: u8 = rgb
37 .next()
38 .and_then(|r| r.trim().parse().ok())
39 .unwrap_or_else(|| panic!("couldn't get blue for {}", name));
40
41 writeln!(writer, "\n///<div style=\"display: inline-block; width: 3em; height: 1em; border: 1px solid black; background: {0};\"></div>", name).unwrap();
42 writeln!(
43 writer,
44 "pub const {}: crate::rgb::Srgb<u8> = crate::rgb::Srgb::new({}, {}, {});",
45 name.to_uppercase(),
46 red,
47 green,
48 blue
49 )
50 .unwrap();
51
52 entries.push((name.to_owned(), name.to_uppercase()));
53 }
54
55 gen_from_str(writer, &entries)
56 }
57
58 #[cfg(feature = "named_from_str")]
59 fn gen_from_str(writer: &mut File, entries: &[(String, String)]) {
60 use std::io::Write;
61
62 writer
63 .write_all(
64 "static COLORS: ::phf::Map<&'static str, crate::rgb::Srgb<u8>> = phf::phf_map! {\n"
65 .as_bytes(),
66 )
67 .unwrap();
68
69 for (key, value) in entries {
70 writeln!(writer, " \"{}\" => {},", key, value).unwrap();
71 }
72
73 writer.write_all("};\n".as_bytes()).unwrap();
74 }
75
76 #[cfg(not(feature = "named"))]
77 pub fn build_colors(_writer: &mut File) {}
78
79 #[allow(unused)]
80 #[cfg(not(feature = "named_from_str"))]
81 fn gen_from_str(_writer: &mut File, _entries: &[(String, String)]) {}