Struct infer::Infer

source ·
pub struct Infer { /* private fields */ }
Expand description

Infer allows to use a custom set of Matchers for infering a MIME type.

Most operations can be done by using the top level functions, but when custom matchers are needed every call has to go through the Infer struct to be able to see the custom matchers.

Implementations§

source§

impl Infer

source

pub const fn new() -> Infer

Initialize a new instance of the infer struct.

source

pub fn get(&self, buf: &[u8]) -> Option<Type>

Returns the file type of the buffer.

Examples
let info = infer::Infer::new();
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = info.get(&buf).expect("file type is known");

assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
source

pub fn get_from_path<P: AsRef<Path>>(&self, path: P) -> Result<Option<Type>>

Returns the file type of the file given a path.

Examples

See get_from_path.

source

pub fn is(&self, buf: &[u8], extension: &str) -> bool

Determines whether a buffer is of given extension.

Examples

See is.

source

pub fn is_mime(&self, buf: &[u8], mime_type: &str) -> bool

Determines whether a buffer is of given mime type.

Examples

See is_mime.

source

pub fn is_supported(&self, extension: &str) -> bool

Returns whether an extension is supported.

Examples

See is_supported.

source

pub fn is_mime_supported(&self, mime_type: &str) -> bool

Returns whether a mime type is supported.

Examples

See is_mime_supported.

source

pub fn is_app(&self, buf: &[u8]) -> bool

Determines whether a buffer is an application type.

Examples

See is_app.

source

pub fn is_archive(&self, buf: &[u8]) -> bool

Determines whether a buffer is an archive type.

Examples

See is_archive.

source

pub fn is_audio(&self, buf: &[u8]) -> bool

Determines whether a buffer is an audio type.

Examples

See is_audio.

source

pub fn is_book(&self, buf: &[u8]) -> bool

Determines whether a buffer is a book type.

Examples

See is_book.

source

pub fn is_document(&self, buf: &[u8]) -> bool

Determines whether a buffer is a document type.

Examples

See is_document.

source

pub fn is_font(&self, buf: &[u8]) -> bool

Determines whether a buffer is a font type.

Examples

See is_font.

source

pub fn is_image(&self, buf: &[u8]) -> bool

Determines whether a buffer is an image type.

Examples

See is_image.

source

pub fn is_video(&self, buf: &[u8]) -> bool

Determines whether a buffer is a video type.

Examples

See is_video.

source

pub fn is_custom(&self, buf: &[u8]) -> bool

Determines whether a buffer is one of the custom types added.

Examples
fn custom_matcher(buf: &[u8]) -> bool {
    return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}

let mut info = infer::Infer::new();
info.add("custom/foo", "foo", custom_matcher);
let buf = [0x10, 0x11, 0x12, 0x13];
assert!(info.is_custom(&buf));
source

pub fn add( &mut self, mime_type: &'static str, extension: &'static str, m: Matcher )

Adds a custom matcher.

Custom matchers are matched in order of addition and before the default set of matchers.

Examples
fn custom_matcher(buf: &[u8]) -> bool {
    return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}

let mut info = infer::Infer::new();
info.add("custom/foo", "foo", custom_matcher);
let buf = [0x10, 0x11, 0x12, 0x13];
let kind =  info.get(&buf).expect("file type is known");

assert_eq!(kind.mime_type(), "custom/foo");
assert_eq!(kind.extension(), "foo");

Trait Implementations§

source§

impl Default for Infer

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Infer

§

impl Send for Infer

§

impl Sync for Infer

§

impl Unpin for Infer

§

impl UnwindSafe for Infer

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