Crates.io | edfsm-macros |
lib.rs | edfsm-macros |
version | |
source | src |
created_at | 2024-11-27 08:06:48.791761 |
updated_at | 2024-12-05 04:03:04.014864 |
description | Macros for the Event Driven Finite State Machine library |
homepage | |
repository | https://github.com/titanclass/edfsm.git |
max_upload_size | |
id | 1462762 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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.