# Finitomata :: FSM with rich lifecycle callbacks, persistence, and supervision

**URL:** <https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863>\
**Category:** announcements\
**Created:** [June 21, 2026, 5:41am UTC](https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863 "2026-06-21T05:41:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/aleix/32/53019_2.png) [@Aleix](https://users.rust-lang.org/u/Aleix)\
**Post date:** [June 21, 2026, 5:41am UTC](https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863/1 "2026-06-21T05:41:25Z")

</div>

A half of year ago I ported most of Erlang supervision tree model to Rust as [`joerl`](https://crates.io/crates/joerl). It has been tested in the field since then and it came to port of [`finitomata`](https://hexdocs.pm/finitomata) — a Finite Automata implementation done right.

Welcome [`finitomata`](https://crates.io/crates/finitomata) in Rust on top of `joerl`.

Self-explanatory quick-start from README:

```rust
use async_trait::async_trait;
use finitomata::{finitomata, Finitomata, FinitomataSupervisor, TransitionResult};
use std::time::Duration;

#[finitomata(
    fsm = r#"
        [*] --> idle
        idle --> |start| running
        running --> |stop| idle
        idle --> |shutdown| off
        off --> |confirm| [*]
    "#,
    syntax = "mermaid",
    auto_terminate = true
)]
#[derive(Debug, Clone, Default)]
struct MyFsm;

#[derive(Debug, Clone)]
struct Payload { counter: u32 }

#[async_trait]
impl Finitomata for MyFsm {
    type State = MyFsmState;
    type Event = MyFsmEvent;
    type Payload = Payload;

    async fn on_transition(
        &mut self,
        _from: &MyFsmState,
        event: &MyFsmEvent,
        _event_payload: &Payload,
        state_payload: &mut Payload,
    ) -> TransitionResult<MyFsmState, Payload> {
        match event {
            MyFsmEvent::Start => {
                state_payload.counter += 1;
                TransitionResult::Ok(MyFsmState::Running)
            }
            MyFsmEvent::Stop => TransitionResult::Ok(MyFsmState::Idle),
            MyFsmEvent::Shutdown => TransitionResult::Ok(MyFsmState::Off),
            MyFsmEvent::Confirm => TransitionResult::Ok(MyFsmState::Off),
        }
    }
}

#[tokio::main]
async fn main() {
    let graph = MyFsm::build_graph();
    let supervisor = FinitomataSupervisor::<MyFsm>::new("my_sup", graph)
        .with_auto_terminate(true);

    supervisor.start_fsm("instance_1", MyFsm, Payload { counter: 0 }).await.unwrap();

    // Send events
    supervisor.transition("instance_1", MyFsmEvent::Start, Payload { counter: 0 }).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Query state (from cache, no actor round-trip)
    let state = supervisor.state("instance_1").unwrap();
    println!("Current: {:?}, Counter: {}", state.current, state.payload.counter);
}

```

Enjoy!

---

<div class="post-metadata">

**Author:** ![shishkin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/shishkin/32/44454_2.png) [@shishkin](https://users.rust-lang.org/u/shishkin)\
**Post date:** [June 22, 2026, 5:43am UTC](https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863/2 "2026-06-22T05:43:02Z")

</div>

I'm super interested in actor model implementations in Rust and joerl looks really cool. Though I'm confused what finitomata adds on top of it (apart from a different API) and when would I use one or the other.

Small suggestion on the example: It is somewhat confusing when the `Payload` type is used for both the actor internal state and the event. Maybe a richer example could demonstrate how different event payloads could be checked at compile time?

---

<div class="post-metadata">

**Author:** ![Aleix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/aleix/32/53019_2.png) [@Aleix](https://users.rust-lang.org/u/Aleix)\
**Post date:** [June 22, 2026, 5:57am UTC](https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863/3 "2026-06-22T05:57:47Z")

</div>

**TL;DR:** if you are confused what’s the difference, you don’t need `finitomata` yet, you are good to go with `joerl`.

> [@shishkin](#):
>
> I'm confused what finitomata adds on top of it

Yes, `joerl` comes up with [`GenStatem`](https://docs.rs/joerl/latest/joerl/gen_statem/trait.GenStatem.html) trait mirroring the erlang’s [`gen_statem`](https://www.erlang.org/doc/apps/stdlib/gen_statem.html) behaviour. If you are interested in _actor model_ per se, you should be good with `joerl`.

On the other hand, FSM implementation has nothing to do with the actor model, except it’s drastically easy to build the one on top of it. `finitomata` is not about the different API by any means, it’s about the complete, fault tolerant, easy-to-use finite automata implementation.

> [@shishkin](#):
>
> when would I use one or the other

It’s like asking when would I eat apples or oranges. FSM might be implemented on top of actor model, or a factory in OOP, or a typeclass in Haskell, or even on top of jump instructions in assembly language. Basically, if you have bare states, you would most likely want to use an FSM to manage them (I even wrote [a long rant on that topic](https://rocket-science.ru/hacking/2026/06/14/status-driven-orchestrator-in-denial).)

> [@shishkin](#):
>
> Maybe a richer example could demonstrate

I am not doing Rust for a living, that was just a request from the neighbour team, so I doubt I am to put any additional effort into this in the meanwhile. The Elixir examples might shed a light, andalso I gladly accept PRs.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 20, 2026, 5:58am UTC](https://users.rust-lang.org/t/finitomata-fsm-with-rich-lifecycle-callbacks-persistence-and-supervision/140863/4 "2026-09-20T05:58:09Z")

</div>

Closed on September 20, 2026, 5:58am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
