Skip to main content

Crate media_typer

Crate media_typer 

Source
Expand description

§media-typer — RFC 6838 media type parsing and formatting

Parse and format media types — type/subtype+suffix — per the RFC 6838 grammar, splitting out the type, subtype, and structured-syntax suffix. 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 see the content-type crate. Zero dependencies and #![no_std].

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

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"));

// The type, subtype, and suffix are lower-cased on parse.
assert_eq!(parse("IMAGE/SVG+XML").unwrap(), MediaType::new("image", "svg", Some("xml")));

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

// `test` reports whether a string is a valid (parameter-free) media type.
assert!(test("application/json"));
assert!(!test("application/json; charset=utf-8"));

Structs§

MediaType
A parsed media type: type/subtype with an optional structured-syntax suffix.

Enums§

Error
The error type for invalid media types.

Functions§

format
Validate the parts of a MediaType and render it as type/subtype[+suffix].
parse
Parse a media type string into its MediaType parts.
test
Report whether media_type is a valid (parameter-free) RFC 6838 media type.