ruex/foundation/patterns/fsm/
integrations.rs

1use std::rc::Rc;
2
3use super::StateDef;
4
5// pub mod command;
6
7// pub mod injector;
8
9/// Defines finite state machine integration functionality
10pub trait FsmIntegration<T: FsmIntegration<T>>: Clone {
11    /// Makes a transition from one state to another
12    fn transition(&self, new_state: Rc<StateDef<T>>, old_state: Option<Rc<StateDef<T>>>) -> bool;
13}
14
15/// Represents callback integration
16#[derive(Default, Debug, Clone)]
17pub struct CallbackIntegration;
18
19impl CallbackIntegration {
20    /// Create new callback integration
21    pub fn new() -> Self {
22        Self
23    }
24}
25
26impl FsmIntegration<Self> for CallbackIntegration {
27    fn transition(
28        &self,
29        new_state: Rc<StateDef<Self>>,
30        old_state: Option<Rc<StateDef<Self>>>,
31    ) -> bool {
32        if let Some(ref old_state) = old_state {
33            old_state.state.exit(self)
34        }
35
36        new_state.state.enter(self);
37
38        true
39    }
40}