MediaType

Struct MediaType 

Source
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;
// 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 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

Source

pub fn parse(source: impl Parse) -> Result<Self, InvalidMime>

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.

Source

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);
Source

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");
Source

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());
Source

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");
Source

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());
Source

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);
Source

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 AsRef<str> for MediaType

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for MediaType

Source§

fn clone(&self) -> MediaType

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MediaType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for MediaType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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);
Source§

fn from(mt: MediaType) -> MediaRange

Converts to this type from the input type.
Source§

impl FromStr for MediaType

Source§

type Err = InvalidMime

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<MediaType, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<'a> PartialEq<&'a str> for MediaType

Source§

fn eq(&self, s: &&'a str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq<MediaType> for &'a str

Source§

fn eq(&self, mt: &MediaType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<MediaType> for str

Source§

fn eq(&self, mt: &MediaType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for MediaType

Source§

fn eq(&self, s: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for MediaType

Source§

fn eq(&self, other: &MediaType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.