Struct mime_guess::MimeGuess

source ·
pub struct MimeGuess(/* private fields */);
Expand description

A “guess” of the MIME/Media Type(s) of an extension or path as one or more Mime instances.

Note: Ordering

A given file format may have one or more applicable Media Types; in this case the first Media Type returned is whatever is declared in the latest IETF RFC for the presumed file format or the one that explicitly supercedes all others. Ordering of additional Media Types is arbitrary.

Note: Values Not Stable

The exact Media Types returned in any given guess are not considered to be stable and are often updated in patch releases in order to reflect the most up-to-date information possible.

Implementations§

source§

impl MimeGuess

source

pub fn from_ext(ext: &str) -> MimeGuess

Guess the MIME type of a file (real or otherwise) with the given extension.

The search is case-insensitive.

If ext is empty or has no (currently) known MIME type mapping, then an empty guess is returned.

source

pub fn from_path<P: AsRef<Path>>(path: P) -> MimeGuess

Guess the MIME type of path by its extension (as defined by Path::extension()). No disk access is performed.

If path has no extension, the extension cannot be converted to str, or has no known MIME type mapping, then an empty guess is returned.

The search is case-insensitive.

Note

Guess is the operative word here, as there are no guarantees that the contents of the file that path points to match the MIME type associated with the path’s extension.

Take care when processing files with assumptions based on the return value of this function.

source

pub fn is_empty(&self) -> bool

true if the guess did not return any known mappings for the given path or extension.

source

pub fn count(&self) -> usize

Get the number of MIME types in the current guess.

source

pub fn first(&self) -> Option<Mime>

Get the first guessed Mime, if applicable.

See Note: Ordering above.

source

pub fn first_raw(&self) -> Option<&'static str>

Get the first guessed Media Type as a string, if applicable.

See Note: Ordering above.

source

pub fn first_or_octet_stream(&self) -> Mime

Get the first guessed Mime, or if the guess is empty, return application/octet-stream instead.

See Note: Ordering above.

Note: HTTP Applications

For HTTP request and response bodies if a value for the Content-Type header cannot be determined it might be preferable to not send one at all instead of defaulting to application/octet-stream as the recipient will expect to infer the format directly from the content instead. (RFC 7231, Section 3.1.1.5)

On the contrary, for multipart/form-data bodies, the Content-Type of a form-data part is assumed to be text/plain unless specified so a default of application/octet-stream for non-text parts is safer. (RFC 7578, Section 4.4)

source

pub fn first_or_text_plain(&self) -> Mime

Get the first guessed Mime, or if the guess is empty, return text/plain instead.

See Note: Ordering above.

source

pub fn first_or(&self, default: Mime) -> Mime

Get the first guessed Mime, or if the guess is empty, return the given Mime instead.

See Note: Ordering above.

source

pub fn first_or_else<F>(&self, default_fn: F) -> Mimewhere F: FnOnce() -> Mime,

Get the first guessed Mime, or if the guess is empty, execute the closure and return its result.

See Note: Ordering above.

source

pub fn iter(&self) -> Iter

Get an iterator over the Mime values contained in this guess.

See Note: Ordering above.

source

pub fn iter_raw(&self) -> IterRaw

Get an iterator over the raw media-type strings in this guess.

See Note: Ordering above.

Trait Implementations§

source§

impl Clone for MimeGuess

source§

fn clone(&self) -> MimeGuess

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 Debug for MimeGuess

source§

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

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

impl<'a> IntoIterator for &'a MimeGuess

§

type Item = Mime

The type of the elements being iterated over.
§

type IntoIter = Iter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for MimeGuess

§

type Item = Mime

The type of the elements being iterated over.
§

type IntoIter = Iter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq<MimeGuess> for MimeGuess

source§

fn eq(&self, other: &MimeGuess) -> 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 Copy for MimeGuess

source§

impl Eq for MimeGuess

source§

impl StructuralEq for MimeGuess

source§

impl StructuralPartialEq for MimeGuess

Auto Trait Implementations§

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, 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.