mime_guess/
impl_bin_search.rs

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