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§
- Media
Type - A parsed media type:
type/subtypewith an optional structured-syntaxsuffix.
Enums§
- Error
- The error type for invalid media types.