Crate rulesxp

Crate rulesxp 

Source
Expand description

RulesXP - Multi-language rules expression evaluator

This crate provides a minimalistic expression evaluator that supports both Scheme syntax and JSONLogic operations with strict typing. It implements a proper subset of both languages designed for reliable rule evaluation with predictable behavior.

§Dual Language Support

The evaluator accepts expressions in Scheme or JSONLogic syntax:

;; Scheme syntax
(+ 1 2 3)           ; arithmetic
(if #t "yes" "no")  ; conditionals
(and #t #f)         ; boolean logic
(car '(1 2 3))      ; list operations

The same operations can be represented in JSONLogic:

{"+": [1, 2, 3]}
{"if": [true, "yes", "no"]}
{"and": [true, false]}
{"car": [[1, 2, 3]]}

§Strict Typing

This interpreter implements stricter semantics than standard Scheme or JSONLogic:

  • No type coercion (numbers don’t become strings, etc.)
  • Boolean operations require actual boolean values (no “truthiness”)
  • Arithmetic overflow detection and error reporting
  • Strict arity checking for all functions

Any program accepted by this interpreter will give identical results in standard Scheme R7RS-small or JSONLogic interpreters, but the converse is not true due to our additional type safety requirements.

§Modules

  • scheme: S-expression parsing from text
  • evaluator: Core expression evaluation engine
  • builtinops: Built-in operations with dual-language mapping
  • jsonlogic: JSONLogic format conversion and integration

Modules§

ast
builtinops
Built-in operations registry with dual Scheme/JSONLogic support.
evaluator
jsonlogic
scheme

Enums§

Error
Error types for the interpreter

Constants§

MAX_EVAL_DEPTH
Maximum evaluation depth to prevent stack overflow in recursive evaluation This limits deeply nested function calls and expressions during evaluation Set higher than parse depth to allow for nested function applications
MAX_PARSE_DEPTH
Maximum parsing depth to prevent stack overflow attacks This limits deeply nested structures in both S-expression and JSONLogic parsers