mrmime 0.0.2

Small, explicit MIME type registry with fast lookup by type or extension.
docs.rs failed to build mrmime-0.0.2
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: mrmime-0.0.1

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