Skip to main content

MediaType

Struct MediaType 

Source
pub struct MediaType<'a> { /* private fields */ }
Expand description

A validated media type.

§Examples

let ld = parse_net::parse_media_type(b"application/ld+json")?;
assert_eq!(ld.suffix(), Some(&b"json"[..]));
assert!(ld.is_json());

Implementations§

Source§

impl<'a> MediaType<'a>

Source

pub const fn type_(&self) -> &'a [u8] ⓘ

The type, as sent.

§Examples
let mt = parse_net::parse_media_type(b"Text/HTML")?;
assert_eq!(mt.type_(), b"Text");
Source

pub const fn subtype(&self) -> &'a [u8] ⓘ

The subtype, as sent.

§Examples
let mt = parse_net::parse_media_type(b"Text/HTML; charset=utf-8")?;
assert_eq!(mt.subtype(), b"HTML");
Source

pub fn suffix(&self) -> Option<&'a [u8]>

The structured syntax suffix: the part of the subtype after its last + (json of ld+json), when there is one.

§Examples
use parse_net::parse_media_type;

assert_eq!(parse_media_type(b"image/svg+xml")?.suffix(), Some(&b"xml"[..]));
assert_eq!(parse_media_type(b"text/plain")?.suffix(), None);
Source

pub fn essence_is(&self, essence: &str) -> bool

Whether type/subtype equals essence, ignoring ASCII case.

§Examples
let mt = parse_net::parse_media_type(b"Application/JSON; charset=utf-8")?;
assert!(mt.essence_is("application/json"));
assert!(!mt.essence_is("application"));
Source

pub fn is(&self, type_: &str, subtype: &str) -> bool

Whether the type and subtype equal type_ and subtype, ignoring ASCII case.

§Examples
let mt = parse_net::parse_media_type(b"TEXT/html")?;
assert!(mt.is("text", "HTML") && !mt.is("text", "plain"));
Source

pub const fn params(&self) -> Params<'a>

The parameters.

§Examples
let mt = parse_net::parse_media_type(b"text/plain; charset=utf-8; format=flowed")?;
assert_eq!(mt.params().len(), 2);
Source

pub fn param(&self, name: &str) -> Option<Word<'a>>

The value of the parameter name (ASCII case-insensitive).

§Examples
let mt = parse_net::parse_media_type(b"text/plain; format=flowed")?;
assert_eq!(mt.param("Format").and_then(|w| w.as_str()), Some("flowed"));
assert!(mt.param("delsp").is_none());
Source

pub fn charset(&self) -> Option<Word<'a>>

The charset parameter. Compare it case-insensitively.

§Examples
let mt = parse_net::parse_media_type(b"text/html; charset=\"UTF-8\"")?;
assert!(mt.charset().is_some_and(|c| c.eq_ignore_ascii_case(b"utf-8")));
Source

pub fn boundary(&self) -> Result<Option<Boundary<'a>>, ParseError>

The boundary parameter, validated by RFC 2046 section 5.1.1: 1 to 70 bchars, the last not a space. Ok(None) when absent.

§Errors

ErrorKind::InvalidBoundary, with the offset inside the boundary value.

§Examples
use parse_net::parse_media_type;

let mt = parse_media_type(b"multipart/form-data; boundary=\"a b\"")?;
assert_eq!(mt.boundary()?.map(|b| b.as_bytes()), Some(&b"a b"[..]));
assert_eq!(parse_media_type(b"text/plain")?.boundary()?, None);
assert!(parse_media_type(b"multipart/mixed; boundary=\"ab \"")?.boundary().is_err());
Source

pub fn is_json(&self) -> bool

Whether this is application/json or has the +json suffix.

§Examples
use parse_net::parse_media_type;

assert!(parse_media_type(b"application/json")?.is_json());
assert!(parse_media_type(b"application/problem+json")?.is_json());
assert!(!parse_media_type(b"text/json")?.is_json());
Source

pub fn is_form_urlencoded(&self) -> bool

Whether this is application/x-www-form-urlencoded.

§Examples
let mt = parse_net::parse_media_type(b"application/x-www-form-urlencoded; charset=utf-8")?;
assert!(mt.is_form_urlencoded());
Source

pub fn is_multipart_form_data(&self) -> bool

Whether this is multipart/form-data.

§Examples
use parse_net::parse_media_type;

assert!(parse_media_type(b"multipart/form-data; boundary=x")?.is_multipart_form_data());
assert!(!parse_media_type(b"multipart/mixed; boundary=x")?.is_multipart_form_data());
Source

pub fn write_canonical(&self, out: &mut impl Sink) -> Result<(), WriteError>

Writes the media type with the type, subtype, and parameter names in lowercase and the values as sent, separated by ; .

§Errors

WriteError::Full; nothing is written when the sink reports its room.

§Examples
use parse_net::{parse_media_type, SliceSink};

let mt = parse_media_type(b"Text/HTML;Charset=\"UTF-8\"")?;
let mut buf = [0u8; 64];
let mut out = SliceSink::new(&mut buf);
mt.write_canonical(&mut out).expect("room");
assert_eq!(out.written(), b"text/html; charset=\"UTF-8\"");
Source§

impl MediaType<'_>

Source

pub fn to_owned(&self) -> OwnedMediaType

Available on crate feature alloc only.

Copies the media type into an OwnedMediaType (one allocation).

§Examples
let input = b"application/json; charset=utf-8".to_vec();
let owned = parse_net::parse_media_type(&input)?.to_owned();
drop(input);
assert!(owned.as_view().is("application", "json"));

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for MediaType<'a>

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> Eq for MediaType<'a>

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<'a> PartialEq for MediaType<'a>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for MediaType<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for MediaType<'a>

§

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> UnsafeUnpin for MediaType<'a>

§

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

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.