6 releases (3 breaking)

new 0.11.0 Apr 11, 2025
0.10.1 Feb 19, 2025
0.9.0 Jan 4, 2025
0.8.1 Dec 5, 2024
0.8.0 Nov 27, 2024

#5 in #edfsm

Download history 179/week @ 2025-01-04 11/week @ 2025-01-11 2/week @ 2025-01-18 149/week @ 2025-02-01 25/week @ 2025-02-08 131/week @ 2025-02-15 36/week @ 2025-02-22 8/week @ 2025-03-01 3/week @ 2025-03-08 5/week @ 2025-03-15 2/week @ 2025-03-22 79/week @ 2025-04-05

90 downloads per month
Used in 3 crates (via edfsm)

Apache-2.0

29KB
626 lines

Macros for EDFSM

Provides a DSL that conveniently implements the FSM trait. States, Commands and Events are all required to be implemented both as structs and enums.

An example:

#[impl_fsm]
impl Fsm<State, Command, Event, EffectHandlers> for MyFsm {
    state!(Running / entry);

    command!(Idle    => Start => Started => Running);
    command!(Running => Stop  => Stopped => Idle);

    ignore_command!(Idle    => Stop);
    ignore_command!(Running => Start);
}

The state! macro declares state-related attributes. At this time, only entry handlers can be declared. In our example, the macro will ensure that an on_entry_running method will be called for MyFsm. The developer is then required to implement a method e.g.:

fn on_entry_running(_s: &Running, _se: &mut EffectHandlers) {
    // Do something
}

The command! macro declares an entire transition using the form:

<from-state> => <given-command> [=> <yields-event> []=> <to-state>]]

In our example, for the first transition, multiple methods will be called that the developer must provide e.g.:

fn for_idle_start(_s: &Idle, _c: Start, _se: &mut EffectHandlers) -> Option<Started> {
    // Perform some effect here if required. Effects are performed via the EffectHandler
    Some(Started)
}

fn on_idle_started(_s: &Idle, _e: &Started) -> Option<Running> {
    Some(Running)
}

The ignore_command! macro describes those states and commands that should be ignored given:

<from-state> => <given-command>

It is possible to use a wildcard i.e. _ in place of <from-state> and <to-state>.

There are similar macros for events e.g. event! and ignore_event. For event!, the declaration becomes:

<from-state> => <given-event> [=> <to-state> [ / action]]

The / action is optional and is used to declare that a side-effect is to be performed.

Dependencies

~1.5MB
~38K SLoC