1 unstable release
Uses new Rust 2024
| 0.1.1 | Sep 10, 2025 |
|---|---|
| 0.1.0 |
|
#14 in #rules-engine
35 downloads per month
110KB
2K
SLoC
Rusty Rules Lua Bindings
This crate provides Lua bindings for the rusty_rules crate, allowing you to create and
evaluate rule-based conditions from within Lua scripts.
Overview
The library exposes two main types:
Engine- The main rules engine for registering fetchers and compiling rulesRule- A compiled rule that can be evaluated against context data
Basic Usage
use mlua::prelude::*;
// Register the Engine type
let lua = Lua::new();
let engine = lua.create_proxy::<rusty_rules_lua::Engine>()?;
lua.globals().set("Engine", engine)?;
-- Create a new engine instance
local engine = Engine.new()
-- Register a simple fetcher that extracts values from context
engine:register_fetcher("user_attr", function(ctx, attr)
return ctx.user[attr]
end)
-- Compile and evaluate a rule
local rule = engine:compile({["user_attr(role)"] = "admin"})
local result = rule:evaluate({user = {role = "admin"}})
-- `result` will be true
Advanced Features
Custom Matchers
You can specify custom matchers for different data types:
-- Register an IP address fetcher with IP matcher
engine:register_fetcher("client_ip", { matcher = "ip" }, function(ctx)
return ctx.ip_address
end)
-- Use CIDR notation in rules
local rule = engine:compile({ client_ip = "192.168.1.0/24" })
Available matchers:
"bool"- Boolean matching"ip"- IP address and CIDR range matching"number"- Numeric comparison"regex"or"re"- Regular expression matching"string"- String comparison
The default matcher (if unspecified) is a universal matcher for all types.
Raw Arguments
By default, fetcher arguments are split by whitespace and trimmed. You can disable this:
engine:register_fetcher("raw_fetcher", { raw_args = true }, function(ctx, arg)
-- Single argument is passed as a string without modification
return arg
end)
Rule Validation
When the validation feature is enabled, you can validate rules without compiling them:
local is_valid, error = engine:validate({
["get_value(key)"] = "expected_value"
})
-- `error` is: "Additional properties are not allowed ('get_value(key)' was unexpected)"
Validation check the rule structure against the json schema.
JSON Schema
Get the JSON schema for valid rule structures:
-- Returns JSON schema as a string
local schema = engine:json_schema()
Dependencies
~5–8.5MB
~161K SLoC