Banish: a declarative framework for rule-based state machines

โš“ Rust    ๐Ÿ“… 2026-03-23    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 3      

surdeus

Most state machines in Rust end up as a loop wrapping a match wrapping a pile of if chains. Banish gets that boilerplate out of your way. Declare states and rules, and the framework handles scheduling, fixed-point evaluation, and transitions at compile time.

use banish::banish;

// Will print all light colors twice
fn main() {
    banish! {
        let mut ticks: i32 = 0;

        // State attribute that triggers a return on the third entry
        #[max_entry = 2]
        @red // State declaration
            // Conditionless rule that runs once per state entry. Ignores iterations
            announce? {
                ticks = 0;
                println!("\nRed light");
            }

            // Causes @red to loop till false
            timer ? ticks < 3 {
                ticks += 1;
            }
        // @red finishes and transitions to @green

        @green
            announce? { println!("Green light"); }

            timer ? ticks < 6 {
                ticks += 1;
            }

        @yellow
            announce? { println!("Yellow light"); }

            timer ? ticks < 10 {
                ticks += 1;
            } !? { => @red; } // Explicit transition to @red as a fallback
    }
}

It also supports async blocks, runtime dispatch via any enum variant, isolated states reachable only by explicit transition, max_iter and max_entry caps with optional redirect targets, and trace diagnostics through the log crate.

I'm happy to answer any questions and would love your feedback!

Repo: GitHub - LoganFlaherty/banish: A declarative framework for rule-based state machines in Rust. ยท GitHub

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed