| Crates.io | pure-magic |
| lib.rs | pure-magic |
| version | 0.2.1 |
| created_at | 2025-11-18 14:26:12.334884+00 |
| updated_at | 2026-01-19 16:56:51.345654+00 |
| description | Safe Rust re-implementation of libmagic |
| homepage | |
| repository | https://github.com/qjerome/magic-rs/ |
| max_upload_size | |
| id | 1938501 |
| size | 275,255 |
pure-magic: A pure and safe Rust Reimplementation of libmagicUnlike many file identification crates, pure-magic is highly compatible with the standard
magic rule format, allowing seamless reuse of existing
rules. This makes it an ideal
drop-in replacement for crates relying on libmagic C bindings, where memory safety is critical.
Key Features:
Add pure-magic to your Cargo.toml:
[dependencies]
pure-magic = "0.1" # Replace with the latest version
Or add the latest version with cargo:
cargo add pure-magic
use pure_magic::{MagicDb, MagicSource};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = MagicDb::new();
// Create a MagicSource from a file
let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
db.load(rust_magic);
// Verification is not mandatory
db.verify()?;
// Open a file and detect its type
let mut file = File::open("src/lib.rs")?;
let magic = db.first_magic(&mut file, None)?;
println!(
"File type: {} (MIME: {}, strength: {})",
magic.message(),
magic.mime_type(),
magic.strength()
);
Ok(())
}
use pure_magic::{MagicDb, MagicSource};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = MagicDb::new();
// Create a MagicSource from a file
let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
db.load(rust_magic);
// Open a file and detect its type
let mut file = File::open("src/lib.rs")?;
// Get all matching rules, sorted by strength
let magics = db.all_magics(&mut file)?;
// Must contain rust file magic and default text magic
assert!(magics.len() > 1);
for magic in magics {
println!(
"Match: {} (strength: {}, source: {})",
magic.message(),
magic.strength(),
magic.source().unwrap_or("unknown")
);
}
Ok(())
}
use pure_magic::{MagicDb, MagicSource};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = MagicDb::new();
// Create a MagicSource from a file
let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
db.load(rust_magic);
// Serialize the database to a file
let mut output = File::create("/tmp/compiled.db")?;
db.serialize(&mut output)?;
println!("Database saved to file");
Ok(())
}
use pure_magic::{MagicDb, MagicSource};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = MagicDb::new();
// Create a MagicSource from a file
let rust_magic = MagicSource::open("../magic-db/src/magdir/rust")?;
db.load(rust_magic);
// Serialize the database in a vector
let mut ser = vec![];
db.serialize(&mut ser)?;
println!("Database saved to vector");
// We deserialize from slice
let db = MagicDb::deserialize(&mut ser.as_slice())?;
assert!(!db.rules().is_empty());
Ok(())
}
This project is licensed under the GPL-3.0 License.
Contributions are welcome! Open an issue or submit a pull request.
libmagic (part of the file command).