| Crates.io | magic-embed |
| lib.rs | magic-embed |
| version | 0.3.1 |
| created_at | 2025-11-14 14:06:55.462331+00 |
| updated_at | 2026-01-19 16:56:53.804332+00 |
| description | Proc-macro to embed a compiled magic database |
| homepage | |
| repository | https://github.com/qjerome/magic-rs/ |
| max_upload_size | |
| id | 1932922 |
| size | 41,403 |
magic-embed: Compile-time Magic Database EmbeddingA procedural macro crate for embedding compiled pure_magic databases directly into your Rust binary.
This crate provides a convenient way to bundle file type detection rules with your application,
eliminating the need for external rule files at runtime.
pure_magicAdd magic-embed to your Cargo.toml:
[dependencies]
magic-embed = "0.1" # Replace with the latest version
pure-magic = "0.1" # Required peer dependency
Apply the #[magic_embed] attribute to a struct to embed a compiled magic database:
use magic_embed::magic_embed;
use pure_magic::MagicDb;
#[magic_embed(include=["../../magic-db/src/magdir"], exclude=["../../magic-db/src/magdir/der"])]
struct MyMagicDb;
fn main() -> Result<(), pure_magic::Error> {
let db = MyMagicDb::open()?;
// Use the database as you would with pure_magic
Ok(())
}
| Attribute | Type | Required | Description |
|---|---|---|---|
include |
String[] | Yes | Paths to include in the database (files or directories) |
exclude |
String[] | No | Paths to exclude from the database |
use magic_embed::magic_embed;
use pure_magic::MagicDb;
use std::fs::File;
use std::env::current_exe;
#[magic_embed(
include=["../../magic-db/src/magdir"],
exclude=["../../magic-db/src/magdir/der"]
)]
struct AppMagicDb;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open the embedded database
let db = AppMagicDb::open()?;
// Use it to detect file types
let mut file = File::open(current_exe()?)?;
let magic = db.first_magic(&mut file, None)?;
println!("Detected: {} (MIME: {})", magic.message(), magic.mime_type());
Ok(())
}
To ensure your database is rebuilt when rule files change, create a build.rs file:
// build.rs
fn main() {
println!("cargo:rerun-if-changed=magic/rules/");
}
Replace magic/rules/ with the path to your actual rule files.
open() method deserializes the embedded databaseopen() is calledThis project is licensed under the GPL-3.0 License.