-
Notifications
You must be signed in to change notification settings - Fork 59
/
named.rs
64 lines (53 loc) · 2.27 KB
/
named.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
use std::fs::File;
#[cfg(feature = "named")]
pub fn build() {
use std::path::Path;
use std::io::{BufRead, BufReader, Write};
let out_dir = ::std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("named.rs");
let reader =
BufReader::new(File::open("build/svg_colors.txt").expect("could not open svg_colors.txt"));
let mut writer = File::create(dest_path).expect("couldn't create named.rs");
let mut entries = vec![];
for line in reader.lines() {
let line = line.unwrap();
let mut parts = line.split('\t');
let name = parts.next().expect("couldn't get the color name");
let mut rgb = parts
.next()
.expect(&format!("couldn't get color for {}", name))
.split(", ");
let red: u8 = rgb.next()
.and_then(|r| r.trim().parse().ok())
.expect(&format!("couldn't get red for {}", name));
let green: u8 = rgb.next()
.and_then(|r| r.trim().parse().ok())
.expect(&format!("couldn't get green for {}", name));
let blue: u8 = rgb.next()
.and_then(|r| r.trim().parse().ok())
.expect(&format!("couldn't get blue for {}", name));
writeln!(writer, "\n///<div style=\"display: inline-block; width: 3em; height: 1em; border: 1px solid black; background: {0};\"></div>", name).unwrap();
writeln!(writer, "pub const {}: ::rgb::Srgb<u8> = ::rgb::Srgb {{ red: {}, green: {}, blue: {}, standard: ::core::marker::PhantomData }};", name.to_uppercase(), red, green, blue).unwrap();
entries.push((name.to_owned(), name.to_uppercase()));
}
gen_from_str(&mut writer, &entries)
}
#[cfg(feature = "named_from_str")]
fn gen_from_str(writer: &mut File, entries: &[(String, String)]) {
use std::io::Write;
write!(
writer,
"static COLORS: ::phf::Map<&'static str, ::rgb::Srgb<u8>> = "
).unwrap();
let mut map = ::phf_codegen::Map::new();
for &(ref key, ref value) in entries {
map.entry(&**key, value);
}
map.build(writer).unwrap();
writeln!(writer, ";").unwrap();
}
#[cfg(not(feature = "named"))]
pub fn build() {}
#[allow(unused)]
#[cfg(not(feature = "named_from_str"))]
fn gen_from_str(_writer: &mut File, _entries: &[(String, String)]) {}