media-typer 0.1.0

Parse and format media types (type/subtype+suffix) per RFC 6838. A faithful port of the `media-typer` npm package. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    15 out of 15 items documented4 out of 8 items with examples
  • Size
  • Source code size: 31.86 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 299.96 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/media-typer
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

media-typer

All Contributors

crates.io docs.rs CI license

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-typer implementation

Install

[dependencies]
media-typer = "0.1"

Usage

use media_typer::{parse, format, test, MediaType};

// Parse splits out type / subtype / suffix and lower-cases them.
let mt = parse("application/vnd.api+json").unwrap();
assert_eq!(mt.type_, "application");
assert_eq!(mt.subtype, "vnd.api");
assert_eq!(mt.suffix.as_deref(), Some("json"));

assert_eq!(parse("IMAGE/SVG+XML").unwrap(), MediaType::new("image", "svg", Some("xml")));

// Format validates each part and re-assembles.
assert_eq!(format(&MediaType::new("text", "html", None::<&str>)).unwrap(), "text/html");

// Test reports validity (no parameters allowed).
assert!(test("application/json"));
assert!(!test("application/json; charset=utf-8"));

Behavior notes

  • parse lower-cases the type, subtype, and suffix, and takes the suffix from after the last + in the subtype (application/a+b+c → subtype a+b, suffix c).
  • A media type with parameters is rejected — text/html; charset=utf-8 is not a valid bare media type. Strip parameters first if you have a full Content-Type header.
  • Names follow restricted-name: an ASCII letter or digit, then up to 126 more of A-Za-z0-9!#$&^_- (subtypes and suffixes also allow . and +), so 1–127 characters.
  • As in the reference, a degenerate subtype like x++ parses to subtype x+ with an empty suffix Some(""), which then fails to format again (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.