Skip to content

mime_to_ext is a no_std crate for MIME ↔ extension lookup with no runtime dependency on OS mime files.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

GaneshJadhavOnGitHub/mime_to_ext

Repository files navigation

mime_to_ext

Rust 1.85+ no_std Crates.io GitHub Docs.rs

no_std MIME ↔ extension lookup using lazily-loaded embedded JSON with no runtime dependency on OS mime files.

MSRV: 1.85 (2024 edition)

🏷️ Crate Overview

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_ext offers one of the most complete MIME–extension mappings available in Rust — covering more than 1,100 MIME entries from diverse, reputable sources.


Why mime_to_ext?

Most crates that handle MIME lookups rely on operating system's limited datasets which can lead to missing mappings.

mime_to_ext eliminates 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

⚙️ How it works

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.


Usage

Add this to your Cargo.toml:

[dependencies]
mime_to_ext = "0.1.12"

Example

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");
    }
}

Test

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);

Quick Links

🔗 Source code: GitHub
📦 Rust crate: crates.io
📚 Documentation: Docs.rs

License

Licensed under either of Apache-2.0 or MIT.

About

mime_to_ext is a no_std crate for MIME ↔ extension lookup with no runtime dependency on OS mime files.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published