Struct infer::Infer[][src]

pub struct Infer { /* fields omitted */ }
Expand description

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

Most operations can be done by using the top level functions, but when custom matchers are needed every call has to go through the Infer struct to be able to see the custom matchers.

Implementations

Initialize a new instance of the infer struct.

Returns the file type of the buffer.

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

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

Returns the file type of the file given a path.

Examples

See get_from_path.

Determines whether a buffer is of given extension.

Examples

See is.

Determines whether a buffer is of given mime type.

Examples

See is_mime.

Returns whether an extension is supported.

Examples

See is_supported.

Returns whether a mime type is supported.

Examples

See is_mime_supported.

Determines whether a buffer is an application type.

Examples

See is_app.

Determines whether a buffer is an archive type.

Examples

See is_archive.

Determines whether a buffer is an audio type.

Examples

See is_audio.

Determines whether a buffer is a book type.

Examples

See is_book.

Determines whether a buffer is a document type.

Examples

See is_document.

Determines whether a buffer is a font type.

Examples

See is_font.

Determines whether a buffer is an image type.

Examples

See is_image.

Determines whether a buffer is a video type.

Examples

See is_video.

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 buf = [0x10, 0x11, 0x12, 0x13];
assert!(info.is_custom(&buf));

Adds a custom matcher.

Custom matchers are matched in order of addition and before the default set of matchers.

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 buf = [0x10, 0x11, 0x12, 0x13];
let kind =  info.get(&buf).expect("file type is known");

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

Trait Implementations

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.