windjammer_runtime/
mime.rs1use mime_guess;
6use std::path::Path;
7
8pub fn from_filename<P: AsRef<Path>>(path: P) -> String {
10 mime_guess::from_path(path)
11 .first_or_octet_stream()
12 .to_string()
13}
14
15pub fn from_path<P: AsRef<Path>>(path: P) -> String {
17 from_filename(path)
18}
19
20pub fn from_extension(ext: &str) -> String {
22 mime_guess::from_ext(ext)
23 .first_or_octet_stream()
24 .to_string()
25}
26
27pub fn is_text(mime_type: &str) -> bool {
29 mime_type.starts_with("text/")
30}
31
32pub fn is_image(mime_type: &str) -> bool {
34 mime_type.starts_with("image/")
35}
36
37pub fn is_video(mime_type: &str) -> bool {
39 mime_type.starts_with("video/")
40}
41
42pub fn is_audio(mime_type: &str) -> bool {
44 mime_type.starts_with("audio/")
45}
46
47pub fn is_application(mime_type: &str) -> bool {
49 mime_type.starts_with("application/")
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_from_filename() {
58 assert_eq!(from_filename("test.html"), "text/html");
59 assert_eq!(from_filename("test.js"), "text/javascript");
60 assert_eq!(from_filename("test.json"), "application/json");
61 assert_eq!(from_filename("test.png"), "image/png");
62 assert_eq!(from_filename("test.wasm"), "application/wasm");
63 }
64
65 #[test]
66 fn test_from_extension() {
67 assert_eq!(from_extension("html"), "text/html");
68 assert_eq!(from_extension("css"), "text/css");
69 assert_eq!(from_extension("jpg"), "image/jpeg");
70 }
71
72 #[test]
73 fn test_type_checks() {
74 assert!(is_text("text/html"));
75 assert!(is_image("image/png"));
76 assert!(is_application("application/json"));
77 assert!(!is_text("image/png"));
78 }
79}