pub struct MimeMapper { /* private fields */ }Expand description
MIME type mapper for converting file descriptions to MIME types
Provides case-insensitive matching of file type descriptions to their corresponding MIME types.
Internally backed by a process-wide LazyLock table, so construction is
effectively free and the underlying mapping is shared across all instances.
§Examples
use libmagic_rs::mime::MimeMapper;
let mapper = MimeMapper::new();
assert_eq!(mapper.get_mime_type("ELF 64-bit executable"), Some("application/x-executable"));
assert_eq!(mapper.get_mime_type("PNG image data"), Some("image/png"));
assert_eq!(mapper.get_mime_type("unknown format"), None);Implementations§
Source§impl MimeMapper
impl MimeMapper
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new MIME mapper.
This is a zero-cost constructor: all mappings are shared through a
process-wide LazyLock table initialized on first use. Repeated calls
to new do not rebuild the mapping.
Includes mappings for common file types:
- Executables (ELF, PE32, Mach-O)
- Archives (ZIP, GZIP, TAR, RAR, 7Z)
- Images (JPEG, PNG, GIF, BMP, WEBP, TIFF, ICO)
- Documents (PDF, PostScript)
- Audio/Video (MP3, MP4, AVI, WAV)
- Web (HTML, XML, JSON, JavaScript, CSS)
- Text formats
Sourcepub fn get_mime_type(&self, description: &str) -> Option<&'static str>
pub fn get_mime_type(&self, description: &str) -> Option<&'static str>
Get MIME type for a file description
Performs case-insensitive matching against known file type keywords. Returns the best matching MIME type found in the description, preferring longer (more specific) keyword matches.
§Arguments
description- The file type description to match
§Returns
Some(&'static str) with the MIME type if a match is found, None otherwise.
§Examples
use libmagic_rs::mime::MimeMapper;
let mapper = MimeMapper::new();
// Case-insensitive matching
assert_eq!(mapper.get_mime_type("ELF executable"), Some("application/x-executable"));
assert_eq!(mapper.get_mime_type("elf executable"), Some("application/x-executable"));
// Matches within longer descriptions
assert_eq!(mapper.get_mime_type("PNG image data, 800x600"), Some("image/png"));Trait Implementations§
Source§impl Clone for MimeMapper
impl Clone for MimeMapper
Source§fn clone(&self) -> MimeMapper
fn clone(&self) -> MimeMapper
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more