pub struct MediaType { /* private fields */ }Expand description
A parsed media type (formerly known as a MIME type).
The type and subtype (including any +suffix) are compared case-insensitively, as are the
parameter names and the value of the charset parameter. Other parameter values are compared
case-sensitively.
Values of this type are always stored in their canonical form: the type, subtype and parameter
names are lowercase, the parameters are separated by ; , and parameter values are unquoted.
§Examples
use media_type::MediaType;
let media_type: MediaType = "IMAGE/SVG+XML; Charset=UTF-8".parse().unwrap();
assert_eq!(media_type.type_(), "image");
assert_eq!(media_type.subtype(), "svg");
assert_eq!(media_type.suffix(), Some("xml"));
assert_eq!(media_type.essence(), "image/svg+xml");
assert_eq!(media_type.to_string(), "image/svg+xml; charset=utf-8");Implementations§
Source§impl MediaType
impl MediaType
Sourcepub const fn from_static(source: &'static str) -> MediaType
pub const fn from_static(source: &'static str) -> MediaType
Creates a media type from a 'static string without validating it.
The string must be a valid media type in canonical lowercase form, otherwise the
accessors may return unexpected results. Prefer MediaType::from_str
for untrusted input.
This is mostly useful to define your own constants:
use media_type::MediaType;
const TEXT_X_LOG: MediaType = MediaType::from_static("text/x-log");
assert_eq!(TEXT_X_LOG.subtype(), "x-log");Sourcepub fn type_(&self) -> &str
pub fn type_(&self) -> &str
Get the top-level media type for this MediaType.
§Examples
assert_eq!(media_type::TEXT_PLAIN.type_(), "text");
assert_eq!(media_type::TEXT_PLAIN.type_(), media_type::TEXT);Sourcepub fn subtype(&self) -> &str
pub fn subtype(&self) -> &str
Get the subtype of this MediaType, without its +suffix.
§Examples
use media_type::MediaType;
assert_eq!(media_type::TEXT_PLAIN.subtype(), "plain");
let svg: MediaType = "image/svg+xml".parse().unwrap();
assert_eq!(svg.subtype(), "svg");Sourcepub fn suffix(&self) -> Option<&str>
pub fn suffix(&self) -> Option<&str>
Get the optional +suffix of this MediaType.
§Examples
use media_type::MediaType;
assert_eq!(media_type::TEXT_PLAIN.suffix(), None);
let svg: MediaType = "image/svg+xml".parse().unwrap();
assert_eq!(svg.suffix(), Some("xml"));Sourcepub fn essence(&self) -> &str
pub fn essence(&self) -> &str
Get the “essence” of this MediaType, that is the type and subtype without any parameter.
See the WHATWG definition.
§Examples
assert_eq!(media_type::TEXT_PLAIN_UTF_8.essence(), "text/plain");Sourcepub fn parameters(&self) -> Parameters<'_> ⓘ
pub fn parameters(&self) -> Parameters<'_> ⓘ
Return an iterator over the parameters of this MediaType.
§Examples
use media_type::MediaType;
let media_type: MediaType = "text/plain; charset=utf-8; foo=bar".parse().unwrap();
let params: Vec<_> = media_type.parameters().collect();
assert_eq!(params, [("charset", "utf-8"), ("foo", "bar")]);Sourcepub fn get_parameter<'a>(&'a self, name: &str) -> Option<&'a str>
pub fn get_parameter<'a>(&'a self, name: &str) -> Option<&'a str>
Look up a parameter by name.
The name is matched case-insensitively. Parameter values are returned verbatim, except for
the charset parameter whose value is lowercased.
§Examples
assert_eq!(media_type::TEXT_PLAIN_UTF_8.get_parameter("charset"), Some("utf-8"));
assert_eq!(media_type::TEXT_PLAIN_UTF_8.get_parameter("boundary"), None);