fsm-guards 0.1.1

Fuel-metered JSONLogic guard evaluation with per-call custom operators — forked from jsonlogic-rs
Documentation
//! Basic FSM guard evaluation.
//!
//! Run with: cargo run --example basic_guard

use fsm_guards::{evaluate, EvalCtx, GuardError};
use serde_json::json;

fn main() {
    let ctx = EvalCtx::new();

    // Simple equality check
    let approved = evaluate(
        &json!({"==": [{"var": "status"}, "approved"]}),
        &json!({"status": "approved"}),
        &ctx,
    )
    .unwrap();
    println!("approved: {approved}"); // true

    // Compound rule
    let ready = evaluate(
        &json!({"and": [
            {"==": [{"var": "status"}, "active"]},
            {">=": [{"var": "score"}, 80]},
            {"in": [{"var": "role"}, ["admin", "operator"]]}
        ]}),
        &json!({"status": "active", "score": 95, "role": "admin"}),
        &ctx,
    )
    .unwrap();
    println!("ready: {ready}"); // true

    // Missing variable — detected before evaluation
    match evaluate(
        &json!({"var": "nonexistent"}),
        &json!({"status": "active"}),
        &ctx,
    ) {
        Err(GuardError::MissingVar(path)) => println!("missing var: {path}"),
        other => println!("unexpected: {other:?}"),
    }
}