media-typer
Parse and format media types per RFC 6838.
media-typer splits a media type — type/subtype+suffix — into its parts and validates
each against the RFC 6838 restricted-name grammar, and re-assembles them. A faithful Rust
port of the widely-used media-typer npm
package (v2), used throughout the Node HTTP stack (type-is, body-parser, …).
This is the strict, lower-level media-type grammar — it does not parse parameters
(; charset=utf-8). For full Content-Type header handling (with parameters), see the
content-type crate.
- Zero dependencies
#![no_std]- Differential-tested against the reference
media-typerimplementation
Install
[]
= "0.1"
Usage
use ;
// Parse splits out type / subtype / suffix and lower-cases them.
let mt = parse.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Format validates each part and re-assembles.
assert_eq!;
// Test reports validity (no parameters allowed).
assert!;
assert!;
Behavior notes
parselower-cases thetype,subtype, andsuffix, and takes the suffix from after the last+in the subtype (application/a+b+c→ subtypea+b, suffixc).- A media type with parameters is rejected —
text/html; charset=utf-8is not a valid bare media type. Strip parameters first if you have a fullContent-Typeheader. - Names follow
restricted-name: an ASCII letter or digit, then up to 126 more ofA-Za-z0-9!#$&^_-(subtypes and suffixes also allow.and+), so 1–127 characters. - As in the reference, a degenerate subtype like
x++parses to subtypex+with an empty suffixSome(""), which then fails toformatagain (an empty suffix is not a valid name). This faithfully mirrors the npm package.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of MIT or Apache-2.0 at your option.