Struct mediatype::MediaType

source ·
pub struct MediaType<'a> {
    pub ty: Name<'a>,
    pub subty: Name<'a>,
    pub suffix: Option<Name<'a>>,
    pub params: Cow<'a, [(Name<'a>, Value<'a>)]>,
}
Expand description

A borrowed media type.

use mediatype::{names::*, MediaType, Value, WriteParams};

let mut multipart = MediaType::new(MULTIPART, FORM_DATA);

let boundary = Value::new("dyEV84n7XNJ").unwrap();
multipart.set_param(BOUNDARY, boundary);
assert_eq!(
    multipart.to_string(),
    "multipart/form-data; boundary=dyEV84n7XNJ"
);

multipart.subty = RELATED;
assert_eq!(
    multipart.to_string(),
    "multipart/related; boundary=dyEV84n7XNJ"
);

const IMAGE_SVG: MediaType = MediaType::from_parts(IMAGE, SVG, Some(XML), &[]);
let svg = MediaType::parse("IMAGE/SVG+XML").unwrap();
assert_eq!(svg, IMAGE_SVG);

Fields§

§ty: Name<'a>

Top-level type.

§subty: Name<'a>

Subtype.

§suffix: Option<Name<'a>>

Optional suffix.

§params: Cow<'a, [(Name<'a>, Value<'a>)]>

Parameters.

Implementations§

source§

impl<'a> MediaType<'a>

source

pub const fn new(ty: Name<'a>, subty: Name<'a>) -> Self

Constructs a MediaType from a top-level type and a subtype.

const IMAGE_PNG: MediaType = MediaType::new(IMAGE, PNG);
assert_eq!(IMAGE_PNG, MediaType::parse("image/png").unwrap());
source

pub const fn from_parts( ty: Name<'a>, subty: Name<'a>, suffix: Option<Name<'a>>, params: &'a [(Name<'a>, Value<'a>)] ) -> Self

Constructs a MediaType with an optional suffix and parameters.

const IMAGE_SVG: MediaType = MediaType::from_parts(IMAGE, SVG, Some(XML), &[(CHARSET, UTF_8)]);
assert_eq!(
    IMAGE_SVG,
    MediaType::parse("image/svg+xml; charset=UTF-8").unwrap()
);
source

pub fn parse<'s: 'a>(s: &'s str) -> Result<Self, MediaTypeError>

Constructs a MediaType from str without copying the string.

Errors

Returns an error if the string fails to be parsed.

source

pub const fn essence(&self) -> MediaType<'_>

Returns a MediaType without parameters.

const IMAGE_SVG: MediaType = MediaType::from_parts(IMAGE, SVG, Some(XML), &[(CHARSET, UTF_8)]);
assert_eq!(
    IMAGE_SVG.essence(),
    MediaType::parse("image/svg+xml").unwrap()
);

Trait Implementations§

source§

impl<'a> Clone for MediaType<'a>

source§

fn clone(&self) -> MediaType<'a>

Returns a copy 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<'a> Debug for MediaType<'a>

source§

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

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

impl<'a> Display for MediaType<'a>

source§

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

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

impl<'a> From<&'a MediaTypeBuf> for MediaType<'a>

source§

fn from(t: &'a MediaTypeBuf) -> Self

Converts to this type from the input type.
source§

impl From<&MediaType<'_>> for MediaTypeBuf

source§

fn from(t: &MediaType<'_>) -> Self

Converts to this type from the input type.
source§

impl From<MediaType<'_>> for MediaTypeBuf

source§

fn from(t: MediaType<'_>) -> Self

Converts to this type from the input type.
source§

impl<'a> Hash for MediaType<'a>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<&MediaType<'_>> for MediaTypeBuf

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<MediaType<'_>> for &MediaTypeBuf

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<MediaType<'_>> for MediaTypeBuf

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, 'b> PartialEq<MediaType<'b>> for MediaType<'a>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<MediaTypeBuf> for MediaType<'a>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> ReadParams for MediaType<'a>

source§

fn params(&self) -> Params<'_>

Returns the parameters.
source§

fn get_param(&self, name: Name<'_>) -> Option<Value<'_>>

Gets the parameter value by its name. Read more
source§

impl<'a> WriteParams<'a> for MediaType<'a>

source§

fn set_param<'n: 'a, 'v: 'a>(&mut self, name: Name<'n>, value: Value<'v>)

Sets a parameter value. Read more
source§

fn remove_params(&mut self, name: Name<'_>)

Removes all parameters with the name.
source§

fn clear_params(&mut self)

Removes all parameters.
source§

impl<'a> Eq for MediaType<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for MediaType<'a>

§

impl<'a> Send for MediaType<'a>

§

impl<'a> Sync for MediaType<'a>

§

impl<'a> Unpin for MediaType<'a>

§

impl<'a> UnwindSafe for MediaType<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.