infer

Struct Infer

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

Infer is the main struct of the module

Implementations§

source§

impl Infer

source

pub 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 v = vec![0xFF, 0xD8, 0xFF, 0xAA];
assert_eq!("image/jpeg", info.get(&v).unwrap().mime);
assert_eq!("jpg", info.get(&v).unwrap().ext);
source

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

Returns the file type of the file given a path.

§Errors

Returns an error if we fail to read the path.

§Examples
let info = infer::Infer::new();
let res = info.get_from_path("testdata/sample.jpg");
assert!(res.is_ok());
let o = res.unwrap();
assert!(o.is_some());
let typ = o.unwrap();
assert_eq!("image/jpeg", typ.mime);
assert_eq!("jpg", typ.ext);
source

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

Determines whether a buffer is of given extension.

§Examples
let info = infer::Infer::new();
let v = vec![0xFF, 0xD8, 0xFF, 0xAA];
assert!(info.is(&v, "jpg"));
source

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

Determines whether a buffer is of given mime type.

§Examples
let info = infer::Infer::new();
let v = vec![0xFF, 0xD8, 0xFF, 0xAA];
assert!(info.is_mime(&v, "image/jpeg"));
source

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

Returns whether an extension is supported.

§Examples
let info = infer::Infer::new();
assert!(info.is_supported("jpg"));
source

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

Returns whether a mime type is supported.

§Examples
let info = infer::Infer::new();
assert!(info.is_mime_supported("image/jpeg"));
source

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

Determines whether a buffer is an application type.

§Examples
use std::fs;
let info = infer::Infer::new();
assert!(info.is_app(&fs::read("testdata/sample.wasm").unwrap()));
source

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

Determines whether a buffer is an archive type.

§Examples
use std::fs;
let info = infer::Infer::new();
assert!(info.is_archive(&fs::read("testdata/sample.pdf").unwrap()));
source

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

Determines whether a buffer is an audio type.

§Examples
// mp3
let info = infer::Infer::new();
let v = vec![0xff, 0xfb, 0x90, 0x44, 0x00];
assert!(info.is_audio(&v));
source

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

Determines whether a buffer is a document type.

§Examples
use std::fs;
let info = infer::Infer::new();
assert!(info.is_document(&fs::read("testdata/sample.docx").unwrap()));
source

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

Determines whether a buffer is a font type.

§Examples
use std::fs;
let info = infer::Infer::new();
assert!(info.is_font(&fs::read("testdata/sample.ttf").unwrap()));
source

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

Determines whether a buffer is an image type.

§Examples
let v = vec![0xFF, 0xD8, 0xFF, 0xAA];
let info = infer::Infer::new();
assert!(info.is_image(&v));
source

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

Determines whether a buffer is a video type.

§Examples
use std::fs;
let info = infer::Infer::new();
assert!(info.is_video(&fs::read("testdata/sample.mov").unwrap()));
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 v = vec![0x10, 0x11, 0x12, 0x13];
assert!(info.is_custom(&v));
source

pub fn add(&mut self, mime: &str, ext: &str, m: Matcher)

Adds a custom matcher.

§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 v = vec![0x10, 0x11, 0x12, 0x13];
let res =  info.get(&v).unwrap();
assert_eq!("custom/foo", res.mime);
assert_eq!("foo", res.ext);

Trait Implementations§

source§

impl Default for Infer

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Infer

§

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

source§

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