[][src]Struct infer::Infer

pub struct Infer { /* fields omitted */ }

Infer is the main struct of the module

Methods

impl Infer[src]

pub fn new() -> Infer[src]

Initialize a new instance of the infer struct.

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

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);

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

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);

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

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"));

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

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"));

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

Returns whether an extension is supported.

Examples

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

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

Returns whether a mime type is supported.

Examples

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

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

Determines whether a buffer is an image type.

Examples

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

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

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()));

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

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));

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

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()));

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

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()));

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

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));

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

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()));

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

Determines whether a buffer isone 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));

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

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);

Auto Trait Implementations

impl Send for Infer

impl Sync for Infer

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.