no_std MIME ↔ extension lookup using lazily-loaded embedded JSON with no runtime dependency on OS mime files.
MSRV: 1.85 (2024 edition)
mime_to_ext is a no_std crate that ships an embedded, lazily-loaded JSON mapping of verified MIME-type ↔ extension pairs, giving you robust, cross-platform runtime lookup without touching OS mime files. Unlike other crates that rely on system files or partial datasets,
mime_to_extoffers one of the most complete MIME–extension mappings available in Rust — covering more than 1,100 MIME entries from diverse, reputable sources.
Most crates that handle MIME lookups rely on operating system's limited datasets which can lead to missing mappings.
mime_to_exteliminates this by combining data from multiple independent and widely used MIME databases into a single unified dataset.This approach ensures:
- Cross-platform reliability - No dependency on operating systems's limited datasets
- Broadest coverage - Over 1,100 verified MIME–extension pairs
- Runtime lookups - Without I/O overhead as the dataset is already embedded
The crate uses a precompiled JSON database that merges and normalizes data from several trusted MIME registries and open datasets. The merging process removes duplicates, resolves conflicts, and standardizes both MIME types and extensions to provide clean bidirectional lookup — all accessible at runtime.
Add this to your Cargo.toml:
[dependencies]
mime_to_ext = "0.1.12"use mime_to_ext::{mime_to_ext, ext_to_mime};
fn main() {
// MIME → All extensions
if let Some(exts) = mime_to_ext("audio/mpeg") {
println!("Extensions for audio/mpeg: {}", exts.join(", "));
} else {
println!("No extensions found for audio/mpeg");
}
// Extension → Single canonical MIME type
if let Some(mime) = ext_to_mime("png") {
println!("The MIME type for .png is: {}", mime);
} else {
println!("No MIME type found for .png");
}
}assert_eq!(mime_to_ext("audio/mpeg"), Some(&["mp3", "mp1", "mp2"][..]));
assert_eq!(mime_to_ext("image/png"), Some(&["png"][..]));
assert_eq!(ext_to_mime("png"), Some("image/png"));
assert_eq!(ext_to_mime("mp1"), Some("audio/mpeg"));
assert_eq!(mime_to_ext("foo/bar"), None);
assert_eq!(ext_to_mime("qqq"), None);🔗 Source code: GitHub
📦 Rust crate: crates.io
📚 Documentation: Docs.rs
Licensed under either of Apache-2.0 or MIT.