Crate infer[][src]

Expand description

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

Examples

Get the type of a buffer

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

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

Check file type by path

let kind = infer::get_from_path("testdata/sample.jpg")
    .expect("file read successfully")
    .expect("file type is known");

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

Check for specific type

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

Check for specific type class

let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::is_image(&buf));

Adds a custom file type matcher

Here we actually need to use the Infer struct to be able to declare custom matchers.

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

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

Modules

app
archive
audio
book
doc
font
image
odf
text
video

Structs

Infer

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

Type

Generic information for a type

Enums

MatcherType

Functions

get

Returns the file type of the buffer.

get_from_path

Returns the file type of the file given a path.

is

Determines whether a buffer is of given extension.

is_app

Determines whether a buffer is an application type.

is_archive

Determines whether a buffer is an archive type.

is_audio

Determines whether a buffer is an audio type.

is_book

Determines whether a buffer is a book type.

is_document

Determines whether a buffer is a document type.

is_font

Determines whether a buffer is a font type.

is_image

Determines whether a buffer is an image type.

is_mime

Determines whether a buffer is of given mime type.

is_mime_supported

Returns whether a mime type is supported.

is_supported

Returns whether an extension is supported.

is_video

Determines whether a buffer is a video type.

Type Definitions

Matcher

Matcher function