Crate infer

Source
Expand description

Small crate to infer file and MIME type by checking the magic number signature.

§Examples

§Get the type of a buffer

let v = vec![0xFF, 0xD8, 0xFF, 0xAA];
let info = infer::Infer::new();
assert_eq!("image/jpeg", info.get(&v).unwrap().mime);
assert_eq!("jpg", info.get(&v).unwrap().ext);

§Check path

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

§Check for specific type

Note individual matcher functions do not require an Infer struct instance.

let v = vec![0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::image::is_jpeg(&v));

§Check for specific type class

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

§Adds a custom file type matcher

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

Modules§

app
All the supported matchers categorized and exposed as functions
archive
All the supported matchers categorized and exposed as functions
audio
All the supported matchers categorized and exposed as functions
doc
All the supported matchers categorized and exposed as functions
font
All the supported matchers categorized and exposed as functions
image
All the supported matchers categorized and exposed as functions
video
All the supported matchers categorized and exposed as functions

Structs§

Infer
Infer is the main struct of the module
Type
Generic information for a type

Type Aliases§

Matcher
Matcher function