Debian Codegraph @

third_party/rust/mime_guess/src/impl_phf.rs

raw
1 extern crate phf;
2
3 use unicase::UniCase;
4
5 include!(concat!(env!("OUT_DIR"), "/mime_types_generated.rs"));
6
7 #[cfg(feature = "rev-mappings")]
8 struct TopLevelExts {
9 start: usize,
10 end: usize,
11 subs: phf::Map<UniCase<&'static str>, (usize, usize)>,
12 }
13
14 pub fn get_mime_types(ext: &str) -> Option<&'static [&'static str]> {
15 map_lookup(&MIME_TYPES, ext).cloned()
16 }
17
18 pub fn get_extensions(toplevel: &str, sublevel: &str) -> Option<&'static [&'static str]> {
19 if toplevel == "*" {
20 return Some(EXTS);
21 }
22
23 let top = map_lookup(&REV_MAPPINGS, toplevel)?;
24
25 if sublevel == "*" {
26 return Some(&EXTS[top.start..top.end]);
27 }
28
29 let sub = map_lookup(&top.subs, sublevel)?;
30 Some(&EXTS[sub.0..sub.1])
31 }
32
33 fn map_lookup<'key, 'map: 'key, V>(
34 map: &'map phf::Map<UniCase<&'static str>, V>,
35 key: &'key str,
36 ) -> Option<&'map V> {
37 // FIXME: this doesn't compile unless we transmute `key` to `UniCase<&'static str>`
38 // https://github.com/sfackler/rust-phf/issues/169
39 map.get(&UniCase::new(key))
40 }