Crate mojique

Source
Expand description

mojique provides a simple interface into libmagic (using magic_sys underneath), along with an optional Send, Sync pool that makes it easier to use libmagic in multi-threaded and async environments.

To use mojique, you need to:

  1. Create a Config, which is one of:
    1. DefaultConfig: uses the system magic database.
    2. BufferConfig: uses magic database(s) provided from &[u8] buffers.
    3. FileConfig: uses magic database(s) on the filesystem.
  2. Build either a single Handle (which is Send, but not Sync), or a Pool of handles (that is both Send and Sync), which can then be used to acquire handles via Pool::handle.
  3. Call methods on Handle to detect file types based on content.

§Simple example

A simple example that uses the system magic database to get a MIME type:

use mojique::{Config, DefaultConfig, Flag};

let mut handle = DefaultConfig::default().set_flag(Flag::Mime).build_handle()?;
let mime_type = handle.buffer(b"#include <stdio.h>")?;

println!("MIME type of something looking like C is: {mime_type}");

§Pools

Building a pool uses the same configuration:

use mojique::{Config, DefaultConfig, Flag};

let pool = DefaultConfig::default().set_flag(Flag::Mime).build_pool()?;

Once you have a pool, you can Clone it as much as needed and use Pool::handle to acquire handles to specific tasks or threads.

Re-exports§

pub use magic_sys;

Structs§

BufferConfig
A configuration using one or more magic databases provided as [u8] buffers.
DefaultConfig
A configuration using the default magic database installed on the system.
FileConfig
A configuration using one or more magic databases on the filesystem, but not including the default database.
Handle
A handle to a single libmagic “cookie”, which is better thought of as an instance of the libmagic database.
Pool
A thread-safe pool of Handle instances.

Enums§

Error
Errors that can be returned from mojique.
Flag
libmagic flags.

Traits§

Config
A configuration that sets libmagic flags on any created Handle instances.
ResultType
A raw result from the libmagic C API, which can be either a c_int or a *const c_char.

Functions§

version
Returns the libmagic version.