use mime_guess;
use std::path::Path;
pub fn from_filename<P: AsRef<Path>>(path: P) -> String {
mime_guess::from_path(path)
.first_or_octet_stream()
.to_string()
}
pub fn from_path<P: AsRef<Path>>(path: P) -> String {
from_filename(path)
}
pub fn from_extension(ext: &str) -> String {
mime_guess::from_ext(ext)
.first_or_octet_stream()
.to_string()
}
pub fn is_text(mime_type: &str) -> bool {
mime_type.starts_with("text/")
}
pub fn is_image(mime_type: &str) -> bool {
mime_type.starts_with("image/")
}
pub fn is_video(mime_type: &str) -> bool {
mime_type.starts_with("video/")
}
pub fn is_audio(mime_type: &str) -> bool {
mime_type.starts_with("audio/")
}
pub fn is_application(mime_type: &str) -> bool {
mime_type.starts_with("application/")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_filename() {
assert_eq!(from_filename("test.html"), "text/html");
assert_eq!(from_filename("test.js"), "text/javascript");
assert_eq!(from_filename("test.json"), "application/json");
assert_eq!(from_filename("test.png"), "image/png");
assert_eq!(from_filename("test.wasm"), "application/wasm");
}
#[test]
fn test_from_extension() {
assert_eq!(from_extension("html"), "text/html");
assert_eq!(from_extension("css"), "text/css");
assert_eq!(from_extension("jpg"), "image/jpeg");
}
#[test]
fn test_type_checks() {
assert!(is_text("text/html"));
assert!(is_image("image/png"));
assert!(is_application("application/json"));
assert!(!is_text("image/png"));
}
}