multi_mime_guess/lib.rs
1//! Multi-Mime-Guess
2//!
3//! A Rust library for determining MIME types based on file extensions.
4//!
5//! This library provides a fast and efficient way to look up MIME types for
6//! common file extensions. It uses a pre-generated database built from multiple
7//! authoritative sources including jshttp/mime-db, Apache mime.types,
8//! and NGINX mime.types.
9//!
10//! # Features
11//!
12//! - Fast lookup using a pre-generated perfect hash for O(1) lookup time
13//! - Comprehensive coverage of hundreds of file extensions
14//! - Static data with no runtime dependencies and zero allocations
15//! - Cross-platform compatibility
16//!
17//! # Example
18//!
19//! ```
20//! use multi_mime_guess::lookup;
21//!
22//! let mime_type = lookup("html");
23//! assert_eq!(mime_type, Some("text/html"));
24//!
25//! let mime_type = lookup("unknown");
26//! assert_eq!(mime_type, None);
27//! ```
28
29include!(concat!(env!("OUT_DIR"), "/db.rs"));
30
31/// Looks up the MIME type for a given file extension.
32///
33/// This function performs a case-insensitive lookup of the MIME type for the
34/// specified file extension. The extension can be provided with or without
35/// a leading dot, and will be normalized to lowercase.
36///
37/// # Arguments
38///
39/// * `mime` - The file extension to look up (e.g., "html", ".HTML", "htm")
40///
41/// # Returns
42///
43/// * `Some(&'static str)` - The MIME type if found (e.g., "text/html")
44/// * `None` - If the extension is not recognized
45///
46/// # Examples
47///
48/// ```
49/// assert_eq!(multi_mime_guess::lookup("html"), Some("text/html"));
50/// assert_eq!(multi_mime_guess::lookup("HTML"), Some("text/html"));
51/// assert_eq!(multi_mime_guess::lookup(".html"), Some("text/html"));
52/// assert_eq!(multi_mime_guess::lookup("unknown"), None);
53/// ```
54pub fn lookup(mime: &str) -> Option<&'static str> {
55 MIME_TYPES
56 .get(mime.to_lowercase().trim_start_matches("."))
57 .copied()
58}