finny/decl/
event.rs

1use crate::lib::*;
2
3use crate::{FsmBackend, fsm::EventContext};
4use super::{FsmQueueMock, FsmStateBuilder};
5
6pub struct FsmEventBuilderState<'a, TFsm, TContext, TEvent, TState> {
7    pub (crate) _state_builder: &'a FsmStateBuilder<TFsm, TContext, TState>,
8    pub (crate) _event: PhantomData<TEvent>
9}
10
11impl<'a, TFsm, TContext, TEvent, TState> FsmEventBuilderState<'a, TFsm, TContext, TEvent, TState> {
12    /// An internal transition doesn't trigger the state's entry and exit actions, as opposed to self-transitions.
13    pub fn internal_transition<'b>(&'b self) -> FsmEventBuilderTransition<'b, TFsm, TContext, TEvent, TState> {
14        FsmEventBuilderTransition {
15            _state_event_builder: self
16        }
17    }
18
19    /// A self transition triggers this state's entry and exit actions, while an internal transition does not.
20    pub fn self_transition<'b>(&'b self) -> FsmEventBuilderTransition<'b, TFsm, TContext, TEvent, TState> {
21        FsmEventBuilderTransition {
22            _state_event_builder: self
23        }
24    }
25
26    /// Transition into this state. The transition can have a guard and an action.
27    pub fn transition_to<'b, TStateTo>(&'b self) -> FsmEventBuilderTransitionFull<'b, TFsm, TContext, TEvent, TState, TStateTo> {
28        FsmEventBuilderTransitionFull {
29            _transition_from: self,
30            _state_to: PhantomData::default()
31        }
32    }
33}
34
35
36pub struct FsmEventBuilderTransition<'a, TFsm, TContext, TEvent, TState> {
37    _state_event_builder: &'a FsmEventBuilderState<'a, TFsm, TContext, TEvent, TState>
38}
39
40impl<'a, TFsm, TContext, TEvent, TState> FsmEventBuilderTransition<'a, TFsm, TContext, TEvent, TState>
41    where TFsm: FsmBackend
42{
43    /// An action that happens when the currently active state receives this event. No transitions.
44    pub fn action<TAction: Fn(&TEvent, &mut EventContext<'a, TFsm, FsmQueueMock<TFsm>>, &mut TState)>(&mut self, _action: TAction) -> &mut Self {
45        self
46    }
47    
48    /// A guard for executing this action.
49    pub fn guard<TGuard: Fn(&TEvent, &EventContext<'a, TFsm, FsmQueueMock<TFsm>>, &<TFsm as FsmBackend>::States) -> bool>(&mut self, _guard: TGuard) -> &mut Self {
50        self
51    }
52
53    /// A type for this transition. The struct for the transition will be generated.
54    pub fn with_transition_ty<TTransition>(&mut self) -> &mut Self {
55        self
56    }
57}
58
59
60pub struct FsmEventBuilderTransitionFull<'a, TFsm, TContext, TEvent, TStateFrom, TStateTo> {
61    _transition_from: &'a FsmEventBuilderState<'a, TFsm, TContext, TEvent, TStateFrom>,
62    _state_to: PhantomData<TStateTo>
63}
64
65impl<'a, TFsm, TContext, TEvent, TStateFrom, TStateTo> FsmEventBuilderTransitionFull<'a, TFsm, TContext, TEvent, TStateFrom, TStateTo> 
66    where TFsm: FsmBackend
67{
68    /// An action that happens between the transitions from the two states.
69    pub fn action<TAction: Fn(&TEvent, &mut EventContext<'a, TFsm, FsmQueueMock<TFsm>>, &mut TStateFrom, &mut TStateTo)>(&mut self, _action: TAction) -> &mut Self {
70        self
71    }
72
73    /// A guard for starting this transition from one state to another, including executing the action.
74    pub fn guard<TGuard: Fn(&TEvent, &EventContext<'a, TFsm, FsmQueueMock<TFsm>>, &<TFsm as FsmBackend>::States) -> bool>(&mut self, _guard: TGuard) -> &mut Self {
75        self
76    }
77
78    /// A type for this transition. The struct for the transition will be generated.
79    pub fn with_transition_ty<TTransition>(&mut self) -> &mut Self {
80        self
81    }    
82}