#statemachine

microstate

Finite state machine, inspired by micromachine

2 releases (1 stable)

Uses old Rust 2015

1.0.0 Jan 15, 2016
0.1.0 Jun 13, 2015

#658 in Algorithms

Download history 2/week @ 2021-06-29 6/week @ 2021-07-06 2/week @ 2021-07-13 6/week @ 2021-07-20 10/week @ 2021-07-27 6/week @ 2021-08-03 4/week @ 2021-08-10 5/week @ 2021-08-17 26/week @ 2021-08-24 1/week @ 2021-08-31 34/week @ 2021-09-07 4/week @ 2021-09-14 8/week @ 2021-09-21 14/week @ 2021-09-28 13/week @ 2021-10-05 20/week @ 2021-10-12

54 downloads per month

MIT license

5KB
72 lines

MicroState

Build Status crates.io

Minimal Finite State Machine.

Description

A finite state machine is in only one state at a time. From there it can change from one state to another when initiated by an triggering event: the transition. A finite state machine is fully defined by a list of states and the transitions triggering a change from one state to another.

And that's all this crate does: it let's you define the states and transitions. The rest is up to you.

Inspired by @soveran's micromachine in Ruby.

Documentation

Online documentation

Usage

First you need to import the macro:

#[macro_use] extern crate microstate;

You can then create a new state machine and call transitions.

microstate!{
    MicroMachine { New };
    states { New, Confirmed, Ignored };

    confirm {
        New => Confirmed
    }

    ignore {
        New => Ignored
    }

    reset {
        Confirmed => New
        Ignored   => New
    }
}

let mut machine = MicroMachine::new();

machine.confirm(); // => Some(Confirmed)
machine.state();   // => Confirmed

machine.ignore();  // => None
machine.state();   // => Confirmed

machine.reset();   // => Some(New)
machine.state();   // => New

machine.ignore();  // => Some(Ignored)
machine.state();   // => Ignored

Contribute

If you find bugs or want to help otherwise, please open an issue.

License

MIT. See LICENSE.

No runtime deps