Crate magic[−][src]
Expand description
About
This crate provides bindings for libmagic, which recognizes the
type of data contained in a file (or buffer).
You might be similar with libmagic’s CLI file:
$ file data/tests/rust-logo-128x128-blk.png
data/tests/rust-logo-128x128-blk.png: PNG image data, 128 x 128, 8-bit/color RGBA, non-interlacedUsage example
Here’s an example of using this crate:
extern crate magic;
use magic::{Cookie, CookieFlags};
fn main() {
// Create a new default configuration
let cookie = Cookie::open(CookieFlags::default()).unwrap();
// Load one specific magic database
let databases = vec!["data/tests/db-images-png"];
assert!(cookie.load(&databases).is_ok());
// Recognize the magic of a test file
let test_file_path = "data/tests/rust-logo-128x128-blk.png";
let expected_magic = "PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced";
assert_eq!(cookie.file(&test_file_path).unwrap(), expected_magic);
}Structs
Configuration of which CookieFlags and magic databases to use
Bitmask flags that specify how Cookie functions should behave
The error type used in this crate
Functions
Returns the version of this crate in the format MAJOR.MINOR.PATCH.