install
source · Clone the upstream repo
git clone https://github.com/EvilBit-Labs/libmagic-rs
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/EvilBit-Labs/libmagic-rs "$T" && mkdir -p ~/.claude/skills && cp -r "$T/.claude/skills" ~/.claude/skills/evilbit-labs-libmagic-rs-libmagic-rs-patterns && rm -rf "$T"
manifest:
.claude/skills/SKILL.mdsource content
libmagic-rs Development Patterns
Commit Conventions
This project uses conventional commits (77% of commits follow the pattern):
- New features (most common, ~30%)feat:
/chore:
/chore(deps):
- Maintenance taskschore(ci):
- Bug fixesfix:
- Code restructuringrefactor:
- Documentation updatesdocs:
- Test additions/changestest:
PR references use
(#N) suffix format.
Architecture
Module Structure
src/ lib.rs # Public API (MagicDatabase), re-exports main.rs # CLI binary (rmagic), clap-based error.rs # Error types (LibmagicError, ParseError, EvaluationError) mime.rs # MIME type detection tags.rs # Tag-based classification build_helpers.rs # Testable build script logic builtin_rules.rs # Built-in magic rules (compiled at build time) builtin_rules.magic # Magic rule definitions parser/ mod.rs # Parser public interface ast.rs # AST node definitions (OffsetSpec, TypeKind, Operator, etc.) grammar.rs # nom-based parser combinators evaluator/ mod.rs # Main evaluation engine offset.rs # Offset resolution (absolute, indirect, relative) operators.rs # Comparison and bitwise operations types.rs # Type interpretation with endianness strength.rs # Rule strength calculation io/ mod.rs # Memory-mapped file I/O output/ mod.rs # Output formatting interface json.rs # JSON output format text.rs # Text output format
Pipeline Pattern
Magic File -> Parser -> AST -> Evaluator -> Match Results -> Output Formatter ^ Target File -> Memory Mapper -> File Buffer
Core Types
- Main entry point for loading rules and evaluating filesMagicDatabase
- A single magic rule with offset, type, operator, value, descriptionMagicRule
- Absolute, Indirect, Relative, or FromEnd offsetsOffsetSpec
- Byte, Short, Long, String, Regex with endiannessTypeKind
- Equal, NotEqual, Greater, Less, BitwiseAnd, XorOperator
/LibmagicError
/ParseError
- Error hierarchyEvaluationError
Co-Change Patterns (Files That Change Together)
| File Group | Frequency | Reason |
|---|---|---|
+ | 8x | API changes require CLI updates |
+ | 8x | Evaluator changes exposed through lib API |
+ | 6x | Dependency changes affect library code |
+ | 5x | AST changes re-exported through lib |
+ | 4x | CLI changes require test updates |
Clippy Configuration
Extremely strict clippy setup in
Cargo.toml:
- No unsafe code allowedunsafe_code = "forbid"
- Zero warnings policywarnings = "deny"
- Never useunwrap_used = "deny"
in production.unwrap()
- Never panic in productionpanic = "deny"
,pedantic
,nursery
lint groups enabledcargo- Security-focused lints:
,indexing_slicing
,arithmetic_side_effectsstring_slice
Error Handling Pattern
Use
thiserror with constructor methods:
#[derive(Debug, thiserror::Error)] pub enum ParseError { #[error("Invalid syntax at line {line}: {message}")] InvalidSyntax { line: usize, message: String }, } impl ParseError { #[must_use] pub fn invalid_syntax(line: usize, message: impl Into<String>) -> Self { Self::InvalidSyntax { line, message: message.into() } } }
- Every error variant has a named constructor method
- Constructors use
for ergonomic creationimpl Into<String> - All constructors marked
#[must_use] - Error messages include contextual info (line numbers, offsets)
Testing Patterns
Test Organization
- Unit tests:
in each source file#[cfg(test)] mod tests - Integration tests:
directorytests/*.rs - Compatibility tests:
against real libmagic test suitetests/compatibility_tests.rs - Property tests:
usingtests/property_tests.rsproptest - Benchmarks:
usingbenches/*.rscriterion - Snapshots:
for CLI output snapshotsinsta
Test File Naming
- CLI tests:
tests/cli_integration.rs - JSON output:
tests/json_integration_test.rs - Parser:
tests/parser_integration_tests.rs - Properties:
tests/property_tests.rs
Commands
cargo nextest run --workspace --no-capture # Standard test run just ci-check # Full CI parity check just coverage # Generate coverage report just test-compatibility # Test against original libmagic
Build Script Pattern
Build logic extracted into testable library module:
// src/build_helpers.rs - testable parsing and code generation #[cfg(any(test, doc))] pub mod build_helpers { ... } // build.rs - minimal, delegates to build_helpers fn main() { ... }
Tooling
- mise: Tool version manager (replaces asdf/rtx)
- just: Task runner (justfile)
- cargo-nextest: Fast test runner
- cargo-llvm-cov: Code coverage
- insta: Snapshot testing
- criterion: Benchmarking
- pre-commit: Git hooks
- actionlint: GitHub Actions validation
- mdbook: Documentation site
- prettier: JSON/YAML formatting
- mdformat: Markdown formatting
- cargo-dist: Binary distribution
Rust Edition & Style
- Edition: 2024
- MSRV: 1.89
- rustfmt:
style_edition = "2024" - All derive macros:
Debug, Clone, Serialize, Deserialize, PartialEq, Eq - Public API types derive
+SerializeDeserialize - Doc comments on all public items with examples
on public enums#[non_exhaustive]
Safety Rules
- Use
for buffer access, never direct indexing.get() - Use
/strip_prefix()
instead ofstrip_suffix()&str[n..] - No
orunwrap()
in library codepanic!() - Bounds checking on all byte reads
- Configurable timeouts for evaluation
- No non-ASCII literals in code/comments