Skip to main content

Crate harn_rules

Crate harn_rules 

Source
Expand description

§harn-rules

The declarative structural rule engine for Harn — the Rust core that powers harn rules / lint / codemod surfaces. A rule describes what to match (a pattern snippet, a node kind, or a regex) and optionally how to rewrite it (a fix); the engine compiles that rule against the tree-sitter machinery in harn_hostlib::ast and produces RuleMatches with metavariable bindings.

This crate delivers the atomic matching tier (issue #2832):

  • model — the serde rule data model (id / language / severity / message / rule block / fix).
  • pattern — the snippet → tree-sitter-query compiler ($VAR metavariable lifting + unification).
  • engine — compile a Rule and run it to produce matches.
  • loader — load rules from a TOML file or a directory.

Relational/composite matching (#2833) and where / transform / fix interpolation (#2834) layer onto this surface.

use harn_rules::{Rule, CompiledRule};

let rule = Rule::from_toml_str(
    r#"
    id = "destructure-default"
    language = "typescript"
    fix = "{ $KEY: $SRC }"
    [rule]
    pattern = "$SRC?.$KEY ?? $DEFAULT"
    "#,
).unwrap();
let compiled = CompiledRule::compile(&rule).unwrap();
let matches = compiled.run("const a = cfg?.timeout ?? 30;").unwrap();
assert_eq!(matches[0].bindings["KEY"].text, "timeout");

Re-exports§

pub use engine::Binding;
pub use engine::CompiledRule;
pub use engine::RuleMatch;
pub use engine::Span;
pub use error::RulesError;
pub use loader::load_rule_dir;
pub use loader::load_rule_file;
pub use model::AtomicMatcher;
pub use model::Matcher;
pub use model::Rule;
pub use model::RuleKind;
pub use model::Severity;
pub use pattern::compile_pattern;
pub use pattern::CompiledPattern;

Modules§

engine
Compile a Rule into a runnable matcher and run it against source.
error
Error type for the rule engine.
loader
Load rules from disk: a single TOML rule file, or a directory of them.
model
The declarative rule data model.
pattern
The pattern compiler: a code snippet with metavariable holes → a tree-sitter query.