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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#![cfg_attr(feature = "bench", feature(test))]
extern crate mime;
extern crate phf;
extern crate unicase;
use mime::Mime;
use unicase::UniCase;
use std::ffi::OsStr;
use std::path::Path;
include!("mime_types_generated.rs");
#[cfg(test)]
#[path = "mime_types.rs"]
mod mime_types_src;
pub fn guess_mime_type<P: AsRef<Path>>(path: P) -> Mime {
guess_mime_type_opt(path)
.unwrap_or_else(octet_stream)
}
pub fn guess_mime_type_opt<P: AsRef<Path>>(path: P) -> Option<Mime> {
let ext = path.as_ref().extension().and_then(OsStr::to_str).unwrap_or("");
get_mime_type_opt(ext)
}
pub fn get_mime_type(search_ext: &str) -> Mime {
get_mime_type_opt(search_ext)
.unwrap_or_else(octet_stream)
}
pub fn get_mime_type_opt(search_ext: &str) -> Option<Mime> {
get_mime_type_str(search_ext)
.map(|mime| mime.parse::<Mime>().unwrap())
}
pub fn get_mime_type_str(search_ext: &str) -> Option<&'static str> {
if search_ext.is_empty() { return None; }
let search_ext = unsafe { ::std::mem::transmute::<&str, &'static str>(search_ext) };
MIME_TYPES.get(&UniCase(search_ext)).cloned()
}
pub fn octet_stream() -> Mime {
"application/octet-stream".parse().unwrap()
}
#[cfg(test)]
mod tests {
use mime::Mime;
use std::ascii::AsciiExt;
use std::path::Path;
use super::{get_mime_type, guess_mime_type, MIME_TYPES};
use super::{get_mime_type_opt, guess_mime_type_opt};
#[test]
fn test_mime_type_guessing() {
assert_eq!(get_mime_type("gif").to_string(), "image/gif".to_string());
assert_eq!(get_mime_type("TXT").to_string(), "text/plain".to_string());
assert_eq!(get_mime_type("blahblah").to_string(), "application/octet-stream".to_string());
assert_eq!(guess_mime_type(Path::new("/path/to/file.gif")).to_string(), "image/gif".to_string());
assert_eq!(guess_mime_type("/path/to/file.gif").to_string(), "image/gif".to_string());
}
#[test]
fn test_mime_type_guessing_opt() {
assert_eq!(get_mime_type_opt("gif").unwrap().to_string(), "image/gif".to_string());
assert_eq!(get_mime_type_opt("TXT").unwrap().to_string(), "text/plain".to_string());
assert_eq!(get_mime_type_opt("blahblah"), None);
assert_eq!(guess_mime_type_opt("/path/to/file.gif").unwrap().to_string(), "image/gif".to_string());
assert_eq!(guess_mime_type_opt("/path/to/file"), None);
}
#[test]
fn test_are_mime_types_parseable() {
for (_, mime) in &MIME_TYPES {
mime.parse::<Mime>().unwrap();
}
}
#[test]
fn test_are_extensions_ascii() {
for (ext, _) in &MIME_TYPES {
assert!(ext.is_ascii(), "Extension not ASCII: {:?}", ext);
}
}
#[test]
fn test_are_extensions_sorted() {
use mime_types_src::MIME_TYPES;
for (&(ext, _), &(n_ext, _)) in MIME_TYPES.iter().zip(MIME_TYPES.iter().skip(1)) {
assert!(
ext <= n_ext,
"Extensions in src/mime_types should be sorted alphabetically
in ascending order. Failed assert: {:?} <= {:?}",
ext, n_ext
);
}
}
}
#[cfg(feature = "bench")]
mod bench {
extern crate test;
use self::test::Bencher;
use super::{get_mime_type_str, MIME_TYPES};
#[bench]
fn bench_mime_str(b: &mut Bencher) {
for (mime_ext, _) in &MIME_TYPES {
b.iter(|| {
get_mime_type_str(mime_ext).expect(mime_ext);
});
}
}
#[bench]
fn bench_mime_str_uppercase(b: &mut Bencher) {
let uppercased : Vec<_> = MIME_TYPES.into_iter().map(|(s, _)| s.to_uppercase()).collect();
for mime_ext in &uppercased {
b.iter(|| {
get_mime_type_str(mime_ext).expect(mime_ext);
});
}
}
}