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:
- Create a
Config, which is one of:DefaultConfig: uses the system magic database.BufferConfig: uses magic database(s) provided from&[u8]buffers.FileConfig: uses magic database(s) on the filesystem.
- Build either a single
Handle(which isSend, but notSync), or aPoolof handles (that is bothSendandSync), which can then be used to acquire handles viaPool::handle. - Call methods on
Handleto 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§
- Buffer
Config - A configuration using one or more magic databases provided as
[u8]buffers. - Default
Config - A configuration using the default magic database installed on the system.
- File
Config - 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
Handleinstances.
Enums§
Traits§
- Config
- A configuration that sets libmagic flags on any created
Handleinstances. - Result
Type - A raw result from the libmagic C API, which can be either a
c_intor a*const c_char.
Functions§
- version
- Returns the libmagic version.