Skip to main content

parse

Function parse 

Source
pub fn parse<'a>(
    value: &'a [u8],
    limits: &Limits,
) -> Result<MediaType<'a>, ParseError>
Expand description

Parses a media type, as in Content-Type.

Type and subtype are tokens around /; OWS is allowed only before ; and around the whole value; parameters follow the RFC 9110 grammar with the duplicate policy of limits (refusal by default, so charset or boundary given twice is refused).

§Errors

ErrorKind::InvalidMediaType for a missing /, an empty or non-token type or subtype, whitespace inside the essence, or bytes after the parameters; ErrorKind::Wildcard for * as type or subtype; a control kind for a control byte; the parameter errors of param::parse.

§Examples

use parse_net::{media, ErrorKind, Limits};

let err = media::parse(b"text/ html", &Limits::DEFAULT).unwrap_err();
assert_eq!((err.kind(), err.offset()), (ErrorKind::InvalidMediaType, 5));
assert_eq!(err.to_string(), "invalid media type at offset 5");