[−][src]Struct mime_4::MediaType
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; // etc
A 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 MediaType
s
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
impl MediaType
[src]
pub fn parse(source: impl Parse) -> Result<Self, InvalidMime>
[src]
Parse a string as a MediaType
.
Example
let mt = mime::MediaType::parse("text/plain").unwrap(); assert_eq!(mt, mime::TEXT_PLAIN);
Errors
Returns an error if the source is not a valid media type.
pub fn type_(&self) -> &str
[src]
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);
pub fn subtype(&self) -> &str
[src]
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");
pub fn suffix(&self) -> Option<&str>
[src]
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());
pub fn param<'a>(&'a self, attr: &str) -> Option<Value<'a>>
[src]
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");
pub fn params(&self) -> impl Iterator<Item = (&str, Value<'_>)>
[src]
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());
pub fn has_params(&self) -> bool
[src]
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);
pub fn without_params(mut self: Self) -> Self
[src]
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
impl AsRef<str> for MediaType
[src]
impl Clone for MediaType
[src]
impl Debug for MediaType
[src]
impl Display for MediaType
[src]
impl From<MediaType> for MediaRange
[src]
Any MediaType
can freely be a MediaRange
.
Example
// If we only supported `text/plain`: let range = mime::MediaRange::from(mime::TEXT_PLAIN);
fn from(mt: MediaType) -> MediaRange
[src]
impl FromStr for MediaType
[src]
type Err = InvalidMime
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<MediaType, Self::Err>
[src]
impl<'a> PartialEq<&'a str> for MediaType
[src]
impl PartialEq<MediaType> for MediaType
[src]
fn eq(&self, other: &MediaType) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl PartialEq<str> for MediaType
[src]
Auto Trait Implementations
impl RefUnwindSafe for MediaType
[src]
impl Send for MediaType
[src]
impl Sync for MediaType
[src]
impl Unpin for MediaType
[src]
impl UnwindSafe for MediaType
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,