magic-embed: Compile-time Magic Database Embedding
A 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.
Features
- Compile-time Embedding: Magic rule files are compiled and embedded during build
- Zero Runtime Dependencies: No need to distribute separate rule files
- Flexible Configuration: Include/exclude specific rule files or directories
- Seamless Integration: Works with the
pure_magic
Installation
Add magic-embed to your Cargo.toml:
[]
= "0.1" # Replace with the latest version
= "0.1" # Required peer dependency
Usage
Apply the #[magic_embed] attribute to a struct to embed a compiled magic database:
use magic_embed;
use MagicDb;
;
Attributes
| 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 |
Complete Example
use magic_embed;
use MagicDb;
use File;
use current_exe;
;
Build Configuration
To ensure your database is rebuilt when rule files change, create a build.rs file:
// build.rs
Replace magic/rules/ with the path to your actual rule files.
How It Works
- Compile Time: The macro compiles all specified magic rule files into a binary database
- Embedding: The compiled database is embedded in your binary as a byte array
- Runtime: The
open()method deserializes the embedded database
The compiled database is stored in target/magic-db/db.bin and will be automatically
rebuilt when any included rule file is modified (considering you've added a build.rs script).
Performance Considerations
- The database is compiled only when source files change
- Embedded databases increase binary size but eliminate runtime file I/O
- Database deserialization happens once at runtime when
open()is called
License
This project is licensed under the GPL-3.0 License.