1 | use unicase::UniCase; |
2 | |
3 | include!("mime_types.rs" ); |
4 | include!(concat!(env!("OUT_DIR" ), "/mime_types_generated.rs" )); |
5 | |
6 | #[cfg (feature = "rev-mappings" )] |
7 | #[derive (Copy, Clone)] |
8 | struct TopLevelExts { |
9 | start: usize, |
10 | end: usize, |
11 | subs: &'static [(UniCase<&'static str>, (usize, usize))], |
12 | } |
13 | |
14 | pub fn get_mime_types(ext: &str) -> Option<&'static [&'static str]> { |
15 | let ext: UniCase<&str> = UniCase::new(ext); |
16 | |
17 | map_lookup(map:MIME_TYPES, &ext) |
18 | } |
19 | |
20 | #[cfg (feature = "rev-mappings" )] |
21 | pub fn get_extensions(toplevel: &str, sublevel: &str) -> Option<&'static [&'static str]> { |
22 | if toplevel == "*" { |
23 | return Some(EXTS); |
24 | } |
25 | |
26 | let top: TopLevelExts = map_lookup(map:REV_MAPPINGS, key:toplevel)?; |
27 | |
28 | if sublevel == "*" { |
29 | return Some(&EXTS[top.start..top.end]); |
30 | } |
31 | |
32 | let sub: (usize, usize) = map_lookup(&top.subs, key:sublevel)?; |
33 | Some(&EXTS[sub.0..sub.1]) |
34 | } |
35 | |
36 | fn map_lookup<K, V>(map: &'static [(K, V)], key: &str) -> Option<V> |
37 | where K: Copy + Into<UniCase<&'static str>>, V: Copy { |
38 | mapOption.binary_search_by_key(&UniCase::new(key), |(k: &K, _)| (*k).into()) |
39 | .ok() |
40 | .map(|i: usize| map[i].1) |
41 | } |
42 | |