pub struct MediaType<'a> { /* private fields */ }Expand description
A validated media type.
§Examples
let ld = parse_net::parse_media_type(b"application/ld+json")?;
assert_eq!(ld.suffix(), Some(&b"json"[..]));
assert!(ld.is_json());Implementations§
Source§impl<'a> MediaType<'a>
impl<'a> MediaType<'a>
Sourcepub const fn type_(&self) -> &'a [u8] ⓘ
pub const fn type_(&self) -> &'a [u8] ⓘ
The type, as sent.
§Examples
let mt = parse_net::parse_media_type(b"Text/HTML")?;
assert_eq!(mt.type_(), b"Text");Sourcepub const fn subtype(&self) -> &'a [u8] ⓘ
pub const fn subtype(&self) -> &'a [u8] ⓘ
The subtype, as sent.
§Examples
let mt = parse_net::parse_media_type(b"Text/HTML; charset=utf-8")?;
assert_eq!(mt.subtype(), b"HTML");Sourcepub fn suffix(&self) -> Option<&'a [u8]>
pub fn suffix(&self) -> Option<&'a [u8]>
The structured syntax suffix: the part of the subtype after its last
+ (json of ld+json), when there is one.
§Examples
use parse_net::parse_media_type;
assert_eq!(parse_media_type(b"image/svg+xml")?.suffix(), Some(&b"xml"[..]));
assert_eq!(parse_media_type(b"text/plain")?.suffix(), None);Sourcepub fn essence_is(&self, essence: &str) -> bool
pub fn essence_is(&self, essence: &str) -> bool
Whether type/subtype equals essence, ignoring ASCII case.
§Examples
let mt = parse_net::parse_media_type(b"Application/JSON; charset=utf-8")?;
assert!(mt.essence_is("application/json"));
assert!(!mt.essence_is("application"));Sourcepub fn is(&self, type_: &str, subtype: &str) -> bool
pub fn is(&self, type_: &str, subtype: &str) -> bool
Whether the type and subtype equal type_ and subtype, ignoring
ASCII case.
§Examples
let mt = parse_net::parse_media_type(b"TEXT/html")?;
assert!(mt.is("text", "HTML") && !mt.is("text", "plain"));Sourcepub const fn params(&self) -> Params<'a>
pub const fn params(&self) -> Params<'a>
The parameters.
§Examples
let mt = parse_net::parse_media_type(b"text/plain; charset=utf-8; format=flowed")?;
assert_eq!(mt.params().len(), 2);Sourcepub fn param(&self, name: &str) -> Option<Word<'a>>
pub fn param(&self, name: &str) -> Option<Word<'a>>
The value of the parameter name (ASCII case-insensitive).
§Examples
let mt = parse_net::parse_media_type(b"text/plain; format=flowed")?;
assert_eq!(mt.param("Format").and_then(|w| w.as_str()), Some("flowed"));
assert!(mt.param("delsp").is_none());Sourcepub fn charset(&self) -> Option<Word<'a>>
pub fn charset(&self) -> Option<Word<'a>>
The charset parameter. Compare it case-insensitively.
§Examples
let mt = parse_net::parse_media_type(b"text/html; charset=\"UTF-8\"")?;
assert!(mt.charset().is_some_and(|c| c.eq_ignore_ascii_case(b"utf-8")));Sourcepub fn boundary(&self) -> Result<Option<Boundary<'a>>, ParseError>
pub fn boundary(&self) -> Result<Option<Boundary<'a>>, ParseError>
The boundary parameter, validated by RFC 2046 section 5.1.1: 1 to 70
bchars, the last not a space. Ok(None) when absent.
§Errors
ErrorKind::InvalidBoundary, with the offset inside the boundary
value.
§Examples
use parse_net::parse_media_type;
let mt = parse_media_type(b"multipart/form-data; boundary=\"a b\"")?;
assert_eq!(mt.boundary()?.map(|b| b.as_bytes()), Some(&b"a b"[..]));
assert_eq!(parse_media_type(b"text/plain")?.boundary()?, None);
assert!(parse_media_type(b"multipart/mixed; boundary=\"ab \"")?.boundary().is_err());Sourcepub fn is_json(&self) -> bool
pub fn is_json(&self) -> bool
Whether this is application/json or has the +json suffix.
§Examples
use parse_net::parse_media_type;
assert!(parse_media_type(b"application/json")?.is_json());
assert!(parse_media_type(b"application/problem+json")?.is_json());
assert!(!parse_media_type(b"text/json")?.is_json());Sourcepub fn is_form_urlencoded(&self) -> bool
pub fn is_form_urlencoded(&self) -> bool
Whether this is application/x-www-form-urlencoded.
§Examples
let mt = parse_net::parse_media_type(b"application/x-www-form-urlencoded; charset=utf-8")?;
assert!(mt.is_form_urlencoded());Sourcepub fn is_multipart_form_data(&self) -> bool
pub fn is_multipart_form_data(&self) -> bool
Whether this is multipart/form-data.
§Examples
use parse_net::parse_media_type;
assert!(parse_media_type(b"multipart/form-data; boundary=x")?.is_multipart_form_data());
assert!(!parse_media_type(b"multipart/mixed; boundary=x")?.is_multipart_form_data());Sourcepub fn write_canonical(&self, out: &mut impl Sink) -> Result<(), WriteError>
pub fn write_canonical(&self, out: &mut impl Sink) -> Result<(), WriteError>
Writes the media type with the type, subtype, and parameter names in
lowercase and the values as sent, separated by ; .
§Errors
WriteError::Full; nothing is written when the sink reports its
room.
§Examples
use parse_net::{parse_media_type, SliceSink};
let mt = parse_media_type(b"Text/HTML;Charset=\"UTF-8\"")?;
let mut buf = [0u8; 64];
let mut out = SliceSink::new(&mut buf);
mt.write_canonical(&mut out).expect("room");
assert_eq!(out.written(), b"text/html; charset=\"UTF-8\"");Source§impl MediaType<'_>
impl MediaType<'_>
Sourcepub fn to_owned(&self) -> OwnedMediaType
Available on crate feature alloc only.
pub fn to_owned(&self) -> OwnedMediaType
alloc only.Copies the media type into an OwnedMediaType (one allocation).
§Examples
let input = b"application/json; charset=utf-8".to_vec();
let owned = parse_net::parse_media_type(&input)?.to_owned();
drop(input);
assert!(owned.as_view().is("application", "json"));