Debian Codegraph @

third_party/rust/mime_guess/build.rs

raw
1 #[cfg(feature = "phf")]
2 extern crate phf_codegen;
3 extern crate unicase;
4
5 use unicase::UniCase;
6
7 use std::env;
8 use std::fs::File;
9 use std::io::prelude::*;
10 use std::io::BufWriter;
11 use std::path::Path;
12
13 use std::collections::BTreeMap;
14
15 use mime_types::MIME_TYPES;
16
17 #[path = "src/mime_types.rs"]
18 mod mime_types;
19
20 #[cfg(feature = "phf")]
21 const PHF_PATH: &str = "::impl_::phf";
22
23 fn main() {
24 let out_dir = env::var("OUT_DIR").unwrap();
25 let dest_path = Path::new(&out_dir).join("mime_types_generated.rs");
26 let mut outfile = BufWriter::new(File::create(dest_path).unwrap());
27
28 #[cfg(feature = "phf")]
29 build_forward_map(&mut outfile);
30
31 #[cfg(feature = "rev-mappings")]
32 build_rev_map(&mut outfile);
33 }
34
35 // Build forward mappings (ext -> mime type)
36 #[cfg(feature = "phf")]
37 fn build_forward_map<W: Write>(out: &mut W) {
38 use phf_codegen::Map as PhfMap;
39
40 let mut forward_map = PhfMap::new();
41 forward_map.phf_path(PHF_PATH);
42
43 let mut map_entries: Vec<(&str, Vec<&str>)> = Vec::new();
44
45 for &(key, types) in MIME_TYPES {
46 if let Some(&mut (key_, ref mut values)) = map_entries.last_mut() {
47 // deduplicate extensions
48 if key == key_ {
49 values.extend_from_slice(types);
50 continue;
51 }
52 }
53
54 map_entries.push((key, types.into()));
55 }
56
57 for (key, values) in map_entries {
58 forward_map.entry(
59 UniCase::new(key),
60 &format!("&{:?}", values),
61 );
62 }
63
64 writeln!(
65 out,
66 "static MIME_TYPES: phf::Map<UniCase<&'static str>, &'static [&'static str]> = \n{};",
67 forward_map.build()
68 )
69 .unwrap();
70 }
71
72 // Build reverse mappings (mime type -> ext)
73 #[cfg(all(feature = "phf", feature = "rev-mappings"))]
74 fn build_rev_map<W: Write>(out: &mut W) {
75 use phf_codegen::Map as PhfMap;
76
77 let dyn_map = get_rev_mappings();
78
79 let mut rev_map = PhfMap::new();
80 rev_map.phf_path(PHF_PATH);
81
82 let mut exts = Vec::new();
83
84 for (top, subs) in dyn_map {
85 let top_start = exts.len();
86
87 let mut sub_map = PhfMap::new();
88 sub_map.phf_path(PHF_PATH);
89
90 for (sub, sub_exts) in subs {
91 let sub_start = exts.len();
92 exts.extend(sub_exts);
93 let sub_end = exts.len();
94
95 sub_map.entry(sub, &format!("({}, {})", sub_start, sub_end));
96 }
97
98 let top_end = exts.len();
99
100 rev_map.entry(
101 top,
102 &format!(
103 "TopLevelExts {{ start: {}, end: {}, subs: {} }}",
104 top_start, top_end, sub_map.build()
105 ),
106 );
107 }
108
109 writeln!(
110 out,
111 "static REV_MAPPINGS: phf::Map<UniCase<&'static str>, TopLevelExts> = \n{};",
112 rev_map.build()
113 ).unwrap();
114
115 writeln!(out, "const EXTS: &'static [&'static str] = &{:?};", exts).unwrap();
116 }
117
118 #[cfg(all(not(feature = "phf"), feature = "rev-mappings"))]
119 fn build_rev_map<W: Write>(out: &mut W) {
120 use std::fmt::Write as _;
121
122 macro_rules! unicase_const {
123 ($s:expr) => ({
124 format_args!("{}({:?})", (if $s.is_ascii() {
125 "UniCase::ascii"
126 } else {
127 "UniCase::unicode"
128 }), $s)
129 })
130 }
131
132 let dyn_map = get_rev_mappings();
133
134 write!(out, "static REV_MAPPINGS: &'static [(UniCase<&'static str>, TopLevelExts)] = &[").unwrap();
135
136 let mut exts = Vec::new();
137
138 for (top, subs) in dyn_map {
139 let top_start = exts.len();
140
141 let mut sub_map = String::new();
142
143 for (sub, sub_exts) in subs {
144 let sub_start = exts.len();
145 exts.extend(sub_exts);
146 let sub_end = exts.len();
147
148 write!(
149 sub_map,
150 "({}, ({}, {})),",
151 unicase_const!(sub), sub_start, sub_end
152 ).unwrap();
153 }
154
155 let top_end = exts.len();
156
157 write!(
158 out,
159 "({}, TopLevelExts {{ start: {}, end: {}, subs: &[{}] }}),",
160 unicase_const!(top), top_start, top_end, sub_map
161 ).unwrap();
162 }
163
164 writeln!(out, "];").unwrap();
165
166 writeln!(out, "const EXTS: &'static [&'static str] = &{:?};", exts).unwrap();
167 }
168
169 #[cfg(feature = "rev-mappings")]
170 fn get_rev_mappings(
171 ) -> BTreeMap<UniCase<&'static str>, BTreeMap<UniCase<&'static str>, Vec<&'static str>>> {
172 // First, collect all the mime type -> ext mappings)
173 let mut dyn_map = BTreeMap::new();
174 for &(key, types) in MIME_TYPES {
175 for val in types {
176 let (top, sub) = split_mime(val);
177 dyn_map
178 .entry(UniCase::new(top))
179 .or_insert_with(BTreeMap::new)
180 .entry(UniCase::new(sub))
181 .or_insert_with(Vec::new)
182 .push(key);
183 }
184 }
185 dyn_map
186 }
187
188 fn split_mime(mime: &str) -> (&str, &str) {
189 let split_idx = mime.find('/').unwrap();
190 (&mime[..split_idx], &mime[split_idx + 1..])
191 }