Struct neo_mime::MediaRange[][src]

pub struct MediaRange { /* fields omitted */ }

A parsed media range used to match media types.

Commonly found in the HTTP Accept header, these are used for agents to indicate general classes of content that they can understand.

Example

use neo_mime::MediaRange;

// Suppose this was parsed from an `Accept` header
let range = MediaRange::parse("text/*").unwrap();

// The formats of the resource that we have:
let formats = vec![
    neo_mime::APPLICATION_JSON,
    neo_mime::TEXT_PLAIN_UTF_8,
    neo_mime::TEXT_HTML,
];

for format in &formats {
    if range.matches(format) {
        // This should print for the plain and HTML text...
        println!("We could send in {:?} format!", format);
    }
}

Implementations

impl MediaRange[src]

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

Parse a string as a MediaRange.

Example

let range = neo_mime::MediaRange::parse("*/*").unwrap();
assert_eq!(range, neo_mime::STAR_STAR);

Errors

Returns an error if the source is not a valid media range.

pub fn type_(&self) -> &str[src]

Get the top level media type for this MediaRange.

Example

let range = neo_mime::TEXT_STAR;
assert_eq!(range.type_(), "text");
assert_eq!(range.type_(), neo_mime::TEXT);

pub fn subtype(&self) -> &str[src]

Get the subtype of this MediaRange.

Example

let range = neo_mime::TEXT_STAR;

assert_eq!(range.subtype(), "*");
assert_eq!(range.subtype(), neo_mime::STAR);

let exact = neo_mime::MediaRange::from(neo_mime::TEXT_PLAIN);
assert_eq!(exact.subtype(), neo_mime::PLAIN);
assert_eq!(exact.subtype(), "plain");

pub fn suffix(&self) -> Option<&str>[src]

Get an optional +suffix for this MediaRange.

Example

let svg = neo_mime::MediaRange::from(neo_mime::IMAGE_SVG);

assert_eq!(svg.suffix(), Some(neo_mime::XML));
assert_eq!(svg.suffix().unwrap(), "xml");


let any = neo_mime::STAR_STAR;

assert_eq!(any.suffix(), None);

pub fn matches(&self, mt: &MediaType) -> bool[src]

Checks if this MediaRange matches a specific MediaType.

Example

let images = neo_mime::IMAGE_STAR;

assert!(images.matches(&neo_mime::IMAGE_JPEG));
assert!(images.matches(&neo_mime::IMAGE_PNG));

assert!(!images.matches(&neo_mime::TEXT_PLAIN));

pub fn param<'a>(&'a self, attr: &str) -> Option<Value<'a>>[src]

Look up a parameter by name.

Example

let range = neo_mime::MediaRange::from(neo_mime::TEXT_PLAIN_UTF_8);

assert_eq!(range.param(neo_mime::CHARSET), Some(neo_mime::UTF_8));
assert_eq!(range.param("charset").unwrap(), "utf-8");
assert_eq!(range.param("boundary"), None);

pub fn params(&self) -> impl Iterator<Item = (&str, Value<'_>)>[src]

Returns an iterator over the parameters.

Example

let pkcs7 = neo_mime::MediaRange::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: neo_mime::MediaType = "text/plain".parse().unwrap();
assert_eq!(plain_text.has_params(), false);

let plain_text_utf8: neo_mime::MediaType = "text/plain; charset=utf-8".parse().unwrap();
assert_eq!(plain_text_utf8.has_params(), true);

Trait Implementations

impl AsRef<str> for MediaRange[src]

impl Clone for MediaRange[src]

impl Debug for MediaRange[src]

impl Display for MediaRange[src]

impl From<MediaType> for MediaRange[src]

Any MediaType can freely be a MediaRange.

Example

// If we only supported `text/plain`:
let range = neo_mime::MediaRange::from(neo_mime::TEXT_PLAIN);

impl FromStr for MediaRange[src]

type Err = InvalidMime

The associated error which can be returned from parsing.

impl<'a> PartialEq<&'a str> for MediaRange[src]

impl PartialEq<MediaRange> for MediaRange[src]

impl PartialEq<str> for MediaRange[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.