Skip to main content

Module builtin_rules

Module builtin_rules 

Source
Expand description

Built-in magic rules compiled at build time.

This module contains magic rules that are compiled into the library binary at build time from the src/builtin_rules.magic file. The rules are parsed during the build process and converted into Rust code for efficient loading.

The BUILTIN_RULES static is lazily initialized on first access using std::sync::LazyLock, ensuring minimal overhead when not used.

§Build-Time Generation

During cargo build, the build script (build.rs):

  1. Reads and parses src/builtin_rules.magic
  2. Converts the magic rules into Rust code
  3. Generates a static LazyLock<Vec<MagicRule>> containing all rules
  4. Writes the generated code to $OUT_DIR/builtin_rules.rs

This module includes that generated file and provides a public API to access the compiled rules.

§Coverage

The built-in rules include high-confidence detection patterns for common file types:

  • Executables: ELF, PE/DOS
  • Archives: ZIP, TAR, GZIP
  • Images: JPEG, PNG, GIF, BMP
  • Documents: PDF

§Example

use libmagic_rs::builtin_rules::get_builtin_rules;

let rules = get_builtin_rules();
println!("Loaded {} built-in rules", rules.len());

Statics§

BUILTIN_RULES
Built-in magic rules compiled at build time.

Functions§

get_builtin_rules
Returns a copy of the built-in magic rules.