mrmime

Crates.iomrmime
lib.rsmrmime
version0.0.2
created_at2025-09-21 05:05:29.962579+00
updated_at2025-09-21 06:03:04.324417+00
descriptionSmall, explicit MIME type registry with fast lookup by type or extension.
homepage
repositoryhttps://github.com/hoangvvo/mrmime
max_upload_size
id1848457
size45,663
Hoang (hoangvvo)

documentation

https://docs.rs/mrmime

README

mrmime

A small MIME type registry for Rust that allows easy lookup by MIME type string or file extension.

Why? Most apps only handle a small number of MIME types. This crate lets you declare exactly what you support and treats everything else as invalid. This is important in web applications that receive input from untrusted sources.

TODO: Generate built-in list from IANA media types or mime-db

Installation

Add this to your Cargo.toml:

[dependencies]
mrmime = "0.1"

A small curated set of common types ships by default via the builtin feature. If you want to fully control which types are accepted, disable default features:

[dependencies]
mrmime = { version = "0.1", default-features = false } # no built-ins

We also support optional features for serde and diesel.

[dependencies.mrmime]
version = "0.1"
features = ["serde", "diesel"]

Usage

use mrmime::{MimeType, register_mime};
use std::str::FromStr;

// Built-ins are already present by default; you can add or replace by
// disabling default features (see above) and registering explicitly:
register_mime! { mod my_mimes {
    "text/html"           => [html, htm];
    "application/json"    => [json, map];
    "image/png"           => [png];
    "image/jpeg"          => [jpeg, jpg];
    "video/mp4"           => [mp4];
    "application/pdf"     => [pdf];
    "application/x-7z-compressed" => ["7z"];
}}

fn main() {
    let m: MimeType = "text/html; charset=UTF-8".parse().unwrap();
    assert_eq!(m.as_str(), "text/html");
    assert_eq!(m.extension(), "html");
    assert_eq!(m.extensions(), &["html", "htm"]);
    assert_eq!(m.to_string(), "text/html");
    assert_eq!(MimeType::from_extension("HTM").unwrap(), m);
}

Built-in constants

When the builtin feature is enabled (default), you can refer to common types via exported constants:

use mrmime::builtin::{TEXT_HTML, APPLICATION_JSON, IMAGE_PNG};

assert_eq!(TEXT_HTML.as_str(), "text/html");
assert_eq!(APPLICATION_JSON.extensions(), &["json", "map"]);
assert_eq!(MimeType::from_extension("png").unwrap(), IMAGE_PNG);

Tests

cargo test --all-features

License

MIT

Commit count: 0

cargo fmt