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 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");
assert_eq!(kind.matcher_type(), infer::MatcherType::Image);

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

  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions
  • All the supported matchers categorized and exposed as functions

Structs

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

Enums

Functions

  • Returns the file type of the buffer.
  • Returns the file type of the file given a path.
  • Determines whether a buffer is of given extension.
  • Determines whether a buffer is an application type.
  • Determines whether a buffer is an archive type.
  • Determines whether a buffer is an audio type.
  • Determines whether a buffer is a book type.
  • Determines whether a buffer is a document type.
  • Determines whether a buffer is a font type.
  • Determines whether a buffer is an image type.
  • Determines whether a buffer is of given mime type.
  • Returns whether a mime type is supported.
  • Returns whether an extension is supported.
  • Determines whether a buffer is a video type.

Type Definitions