# 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](https://www.iana.org/assignments/media-types/media-types.xhtml) or [mime-db](https://github.com/jshttp/mime-db)
## Installation
Add this to your `Cargo.toml`:
```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:
```toml
[dependencies]
mrmime = { version = "0.1", default-features = false } # no built-ins
```
We also support optional features for `serde` and `diesel`.
```toml
[dependencies.mrmime]
version = "0.1"
features = ["serde", "diesel"]
```
## Usage
```rust
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:
```rust
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](LICENSE)