Skip to content

Commit

Permalink
Add missing on_transition and superstate
Browse files Browse the repository at this point in the history
  • Loading branch information
nprbst authored and mdeloof committed Sep 11, 2023
1 parent 4cb00ce commit 449b7bb
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions examples/macro/history/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![allow(unused)]

use statig::prelude::*;
use std::io::Write;

pub enum Event {
StartProgram,
Expand All @@ -10,56 +7,67 @@ pub enum Event {
TimerElapsed,
}

#[derive(Debug)]
pub struct Dishwasher {
previous_state: State,
}

#[state_machine(initial = "State::idle()", state(derive(Debug, Clone)))]
#[state_machine(
initial = "State::idle()",
on_transition = "Self::on_transition",
state(derive(Debug, Clone))
)]
impl Dishwasher {
#[state]
// On every transition we update the previous state, so we can transition back to it.
fn on_transition(&mut self, source: &State, _target: &State) {
// println!("transitioned from `{:?}` to `{:?}`", source, _target);
self.previous_state = source.clone();
}

#[superstate]
fn door_closed(event: &Event) -> Response<State> {
match event {
// When the door is opened the program needs to be paused until
// the door is closed again.
Event::DoorOpened => Transition(State::door_opened()),
_ => Super,
}
}

#[state(superstate = "door_closed")]
fn idle(event: &Event) -> Response<State> {
match event {
Event::StartProgram => Transition(State::soap()),
_ => Super,
}
}

#[state]
#[state(superstate = "door_closed")]
fn soap(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::rinse()),
_ => Super,
}
}

#[state]
#[state(superstate = "door_closed")]
fn rinse(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::dry()),
_ => Super,
}
}

#[state]
#[state(superstate = "door_closed")]
fn dry(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::idle()),
_ => Super,
}
}

#[superstate]
fn door_closed(event: &Event) -> Response<State> {
match event {
// When the door is opened the program needs to be paused until
// the door is closed again.
Event::DoorOpened => Transition(State::door_opened()),
_ => Super,
}
}

#[state]
fn door_opened(&mut self, event: &Event) -> Response<State> {
fn door_opened(&self, event: &Event) -> Response<State> {
match event {
// When the door is closed again, the program is resumed from
// the previous state.
Expand All @@ -76,6 +84,8 @@ fn main() {
.uninitialized_state_machine()
.init();

println!("State: {:?}", state_machine.state()); // State: Idle

state_machine.handle(&Event::StartProgram);

println!("State: {:?}", state_machine.state()); // State: Soap
Expand All @@ -92,6 +102,10 @@ fn main() {

println!("State: {:?}", state_machine.state()); // State: DoorOpened

state_machine.handle(&Event::TimerElapsed);

println!("State: {:?}", state_machine.state()); // State: DoorOpened

state_machine.handle(&Event::DoorClosed);

println!("State: {:?}", state_machine.state()); // State: Dry
Expand Down

0 comments on commit 449b7bb

Please sign in to comment.