pub struct MediaType { /* private fields */ }Expand description
A parsed media type (or “MIME”).
§Getting a MediaType
There are several constants exported for common media types:
let text = mime::TEXT_PLAIN;
let svg = mime::IMAGE_SVG;
let json = mime::APPLICATION_JSON;
// etcA MediaType can also be parsed from a string, such as from
a Content-Type HTTP header:
match mime::MediaType::parse("text/plain; charset=utf-8") {
Ok(text) => assert_eq!(text, mime::TEXT_PLAIN_UTF_8),
Err(err) => panic!("you should handle this parse error: {}", err),
}§Inspecting MediaTypes
Once you have a MediaType, you can inspect the various parts of it.
Since the type_() and subtype() methods return &str, you can make
easy-to-read match statements to handle different media types. To prevent
typos, many common type names are available as constants.
let mime = mime::TEXT_PLAIN;
match (mime.type_(), mime.subtype()) {
(mime::TEXT, mime::PLAIN) => println!("plain text!"),
(mime::TEXT, _) => println!("structured text"),
_ => println!("not text"),
}§Note about wildcards (*)
A MediaType represents an exact format type. The HTTP Accept header
can include “media ranges”, which can match multiple media types. Those
“media ranges” should be represented as MediaRange.
Implementations§
Source§impl MediaType
impl MediaType
Sourcepub fn parse(source: impl Parse) -> Result<Self, InvalidMime>
pub fn parse(source: impl Parse) -> Result<Self, InvalidMime>
Sourcepub fn type_(&self) -> &str
pub fn type_(&self) -> &str
Get the top level media type for this MediaType.
§Example
let mime = mime::TEXT_PLAIN;
assert_eq!(mime.type_(), "text");
assert_eq!(mime.type_(), mime::TEXT);Sourcepub fn subtype(&self) -> &str
pub fn subtype(&self) -> &str
Get the subtype of this MediaType.
§Example
let mime = mime::TEXT_PLAIN;
assert_eq!(mime.subtype(), "plain");
assert_eq!(mime.subtype(), mime::PLAIN);
let svg = mime::IMAGE_SVG;
assert_eq!(svg.subtype(), mime::SVG);
assert_eq!(svg.subtype(), "svg+xml");Sourcepub fn suffix(&self) -> Option<&str>
pub fn suffix(&self) -> Option<&str>
Get an optional +suffix for this MediaType.
§Example
let svg = mime::IMAGE_SVG;
assert_eq!(svg.suffix(), Some(mime::XML));
assert_eq!(svg.suffix(), Some("xml"));
assert!(mime::TEXT_PLAIN.suffix().is_none());Sourcepub fn param<'a>(&'a self, attr: &str) -> Option<Value<'a>>
pub fn param<'a>(&'a self, attr: &str) -> Option<Value<'a>>
Look up a parameter by name.
§Example
let mime = mime::TEXT_PLAIN_UTF_8;
assert_eq!(mime.param(mime::CHARSET), Some(mime::UTF_8));
assert_eq!(mime.param("charset").unwrap(), "utf-8");
assert!(mime.param("boundary").is_none());
let mime = "multipart/form-data; boundary=ABCDEFG".parse::<mime::MediaType>().unwrap();
assert_eq!(mime.param(mime::BOUNDARY).unwrap(), "ABCDEFG");Sourcepub fn params(&self) -> impl Iterator<Item = (&str, Value<'_>)>
pub fn params(&self) -> impl Iterator<Item = (&str, Value<'_>)>
Returns an iterator over the parameters.
§Example
let pkcs7 = mime::MediaType::parse(
"application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m"
).unwrap();
let mut params = pkcs7.params();
let (name, value) = params.next().unwrap();
assert_eq!(name, "smime-type");
assert_eq!(value, "enveloped-data");
let (name, value) = params.next().unwrap();
assert_eq!(name, "name");
assert_eq!(value, "smime.p7m");
assert!(params.next().is_none());Sourcepub fn has_params(&self) -> bool
pub fn has_params(&self) -> bool
Returns true if the media type has at last one parameter.
§Example
let plain_text: mime::MediaType = "text/plain".parse().unwrap();
assert_eq!(plain_text.has_params(), false);
let plain_text_utf8: mime::MediaType = "text/plain; charset=utf-8".parse().unwrap();
assert_eq!(plain_text_utf8.has_params(), true);Sourcepub fn without_params(self) -> Self
pub fn without_params(self) -> Self
Transforms the media type into its non-parametrized form.
§Example
use mime::MediaType;
let html_xml_utf8 = MediaType::parse("text/html+xml; charset=utf-8").unwrap();
assert!(html_xml_utf8.has_params());
let html_xml = html_xml_utf8.without_params();
assert!(!html_xml.has_params());
assert_eq!(html_xml, "text/html+xml");Trait Implementations§
Source§impl From<MediaType> for MediaRange
Any MediaType can freely be a MediaRange.
impl From<MediaType> for MediaRange
Any MediaType can freely be a MediaRange.
§Example
// If we only supported `text/plain`:
let range = mime::MediaRange::from(mime::TEXT_PLAIN);