| Crates.io | mrmime |
| lib.rs | mrmime |
| version | 0.0.2 |
| created_at | 2025-09-21 05:05:29.962579+00 |
| updated_at | 2025-09-21 06:03:04.324417+00 |
| description | Small, explicit MIME type registry with fast lookup by type or extension. |
| homepage | |
| repository | https://github.com/hoangvvo/mrmime |
| max_upload_size | |
| id | 1848457 |
| size | 45,663 |
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
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"]
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);
}
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);
cargo test --all-features