Struct xdg_mime::GuessBuilder[][src]

pub struct GuessBuilder<'a> { /* fields omitted */ }

A builder type to specify the parameters for guessing a MIME type.

Each instance of GuessBuilder is tied to the lifetime of the SharedMimeInfo instance that created it.

The GuessBuilder returned by the guess_mime_type method is empty, and will always return a mime::APPLICATION_OCTET_STREAM guess.

You can use the builder methods to specify the file name, the data, or both, to be used to guess the MIME type:

// let mime_db = ...
let mut guess_builder = mime_db.guess_mime_type();
let guess = guess_builder.file_name("foo.png").guess();
assert_eq!(guess.mime_type(), &Mime::from_str("image/png")?);

The guessed MIME type can have a degree of uncertainty; for instance, if you only set the file_name there can be multiple matching MIME types to choose from. Alternatively, if you only set the data, the content might not match any existing rule. Even in the case of setting both the file name and the data the match can be uncertain. This information is preserved by the Guess type, and can be retrieved using the uncertain method.

Implementations

impl<'a> GuessBuilder<'a>[src]

pub fn file_name(&mut self, name: &str) -> &mut Self[src]

Sets the file name to be used to guess its MIME type.

If you have a full path, you should extract the last component, for instance using the Path::file_name() method.

pub fn data(&mut self, data: &[u8]) -> &mut Self[src]

Sets the data for which you want to guess the MIME type.

pub fn metadata(&mut self, metadata: Metadata) -> &mut Self[src]

Sets the metadata of the file for which you want to get the MIME type.

The metadata can be used to match an existing file or path, for instance:

use std::fs;
use std::str::FromStr;
use mime::Mime;
// let mime_db = ...
// let metadata = fs::metadata("/path/to/lib.rs")?;
let mut guess_builder = mime_db.guess_mime_type();
let guess = guess_builder
    .file_name("lib.rs")
    .metadata(metadata)
    .guess();
assert_eq!(guess.mime_type(), &Mime::from_str("text/rust")?);

pub fn path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self[src]

Sets the path of the file for which you want to get the MIME type.

The path will be used by the guess method to extract the file name, metadata, and contents, unless you called the file_name, metadata, and data methods, respectively.

use std::fs;
use std::str::FromStr;
use mime::Mime;
// let mime_db = ...
let mut guess_builder = mime_db.guess_mime_type();
let guess = guess_builder
    .path("src")
    .guess();
assert_eq!(guess.mime_type(), &Mime::from_str("inode/directory")?);

pub fn guess(&mut self) -> Guess[src]

Guesses the MIME type using the data set on the builder. The result is a Guess instance that contains both the guessed MIME type, and whether the result of the guess is certain.

Auto Trait Implementations

impl<'a> RefUnwindSafe for GuessBuilder<'a>

impl<'a> Send for GuessBuilder<'a>

impl<'a> Sync for GuessBuilder<'a>

impl<'a> Unpin for GuessBuilder<'a>

impl<'a> UnwindSafe for GuessBuilder<'a>

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