/
build.rs
105 lines (92 loc) · 3.13 KB
/
build.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
use std::env;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;
use uncased::UncasedStr;
use unicase::UniCase;
fn main() -> io::Result<()> {
let file = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut file = BufWriter::new(File::create(&file)?);
writeln!(
&mut file,
"static MAP: ::phf::Map<u32, &'static str> = \n{};",
phf_codegen::Map::new()
.entry(1u32, "\"a\"")
.entry(2u32, "\"b\"")
.entry(3u32, "\"c\"")
.build()
)?;
writeln!(
&mut file,
"static SET: ::phf::Set<u32> = \n{};",
phf_codegen::Set::new()
.entry(1u32)
.entry(2u32)
.entry(3u32)
.build()
)?;
writeln!(
&mut file,
"static ORDERED_MAP: ::phf::OrderedMap<u32, &'static str> = \n{};",
phf_codegen::OrderedMap::new()
.entry(1u32, "\"a\"")
.entry(2u32, "\"b\"")
.entry(3u32, "\"c\"")
.build()
)?;
writeln!(
&mut file,
"static ORDERED_SET: ::phf::OrderedSet<u32> = \n{};",
phf_codegen::OrderedSet::new()
.entry(1u32)
.entry(2u32)
.entry(3u32)
.build()
)?;
writeln!(
&mut file,
"static STR_KEYS: ::phf::Map<&'static str, u32> = \n{};",
phf_codegen::Map::new()
.entry("a", "1")
.entry("b", "2")
.entry("c", "3")
.build()
)?;
write!(
&mut file,
"static UNICASE_MAP: ::phf::Map<::unicase::UniCase<&'static str>, &'static str> = \n{};",
phf_codegen::Map::new()
.entry(UniCase::new("abc"), "\"a\"")
.entry(UniCase::new("DEF"), "\"b\"")
.build()
)?;
write!(
&mut file,
"static UNCASED_MAP: ::phf::Map<&'static ::uncased::UncasedStr, &'static str> = \n{};",
phf_codegen::Map::new()
.entry(UncasedStr::new("abc"), "\"a\"")
.entry(UncasedStr::new("DEF"), "\"b\"")
.build()
)?;
//u32 is used here purely for a type that impls `Hash+PhfHash+Eq+fmt::Debug`, but is not required for the empty test itself
writeln!(&mut file,
"static EMPTY: ::phf::Map<u32, u32> = \n{};",
phf_codegen::Map::<u32>::new().build())?;
writeln!(&mut file,
"static EMPTY_ORDERED: ::phf::OrderedMap<u32, u32> = \n{};",
phf_codegen::OrderedMap::<u32>::new().build())?;
writeln!(&mut file, "static ARRAY_KEYS: ::phf::Map<[u8; 3], u32> = \n{};",
phf_codegen::Map::<[u8; 3]>::new()
.entry(*b"foo", "0")
.entry(*b"bar", "1")
.entry(*b"baz", "2")
.build())?;
// key type required here as it will infer `&'static [u8; 3]` instead
writeln!(&mut file, "static BYTE_STR_KEYS: ::phf::Map<&[u8], u32> = \n{};",
phf_codegen::Map::<&[u8]>::new()
.entry(b"foo", "0")
.entry(b"bar", "1")
.entry(b"baz", "2")
.entry(b"quux", "3")
.build())
}