pub struct Infer { /* private fields */ }
Expand description
Infer is the main struct of the module
Implementations§
source§impl Infer
impl Infer
sourcepub fn get(&self, buf: &[u8]) -> Option<Type>
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);
sourcepub fn get_from_path<P: AsRef<Path>>(
&self,
path: P,
) -> Result<Option<Type>, Error>
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);
sourcepub fn is(&self, buf: &[u8], ext: &str) -> bool
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"));
sourcepub fn is_mime(&self, buf: &[u8], mime: &str) -> bool
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"));
sourcepub fn is_supported(&self, ext: &str) -> bool
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"));
sourcepub fn is_mime_supported(&self, mime: &str) -> bool
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"));
sourcepub fn is_app(&self, buf: &[u8]) -> bool
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()));
sourcepub fn is_archive(&self, buf: &[u8]) -> bool
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()));
sourcepub fn is_audio(&self, buf: &[u8]) -> bool
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));
sourcepub fn is_document(&self, buf: &[u8]) -> bool
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()));
sourcepub fn is_font(&self, buf: &[u8]) -> bool
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()));
sourcepub fn is_image(&self, buf: &[u8]) -> bool
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));
sourcepub fn is_video(&self, buf: &[u8]) -> bool
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()));
sourcepub fn is_custom(&self, buf: &[u8]) -> bool
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));
sourcepub fn add(&mut self, mime: &str, ext: &str, m: Matcher)
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);