windjammer_runtime/
mime.rs

1//! MIME type detection
2//!
3//! Windjammer's `std::mime` module maps to these functions.
4
5use mime_guess;
6use std::path::Path;
7
8/// Guess MIME type from file path
9pub 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
15/// Alias for from_filename (for consistency with mime_guess API)
16pub fn from_path<P: AsRef<Path>>(path: P) -> String {
17    from_filename(path)
18}
19
20/// Guess MIME type from file extension
21pub fn from_extension(ext: &str) -> String {
22    mime_guess::from_ext(ext)
23        .first_or_octet_stream()
24        .to_string()
25}
26
27/// Check if MIME type is text
28pub fn is_text(mime_type: &str) -> bool {
29    mime_type.starts_with("text/")
30}
31
32/// Check if MIME type is image
33pub fn is_image(mime_type: &str) -> bool {
34    mime_type.starts_with("image/")
35}
36
37/// Check if MIME type is video
38pub fn is_video(mime_type: &str) -> bool {
39    mime_type.starts_with("video/")
40}
41
42/// Check if MIME type is audio
43pub fn is_audio(mime_type: &str) -> bool {
44    mime_type.starts_with("audio/")
45}
46
47/// Check if MIME type is application
48pub 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}