pub struct StateMachine {
pub states: Vec<WorkflowState>,
pub transitions: Vec<WorkflowTransition>,
pub current_state: String,
}Expand description
A finite state machine composed of WorkflowStates and WorkflowTransitions.
Fields§
§states: Vec<WorkflowState>All registered states.
transitions: Vec<WorkflowTransition>All registered transitions.
current_state: StringID of the currently active state.
Implementations§
Source§impl StateMachine
impl StateMachine
Sourcepub fn new(initial_state_id: impl Into<String>) -> Self
pub fn new(initial_state_id: impl Into<String>) -> Self
Create a new FSM with initial_state_id as the starting state.
The initial state is NOT required to exist in states at construction
time; it is added implicitly when the first state is added.
Sourcepub fn add_state(&mut self, state: WorkflowState)
pub fn add_state(&mut self, state: WorkflowState)
Register a state.
Sourcepub fn add_transition(&mut self, t: WorkflowTransition)
pub fn add_transition(&mut self, t: WorkflowTransition)
Register a transition.
Sourcepub fn trigger(&mut self, event: &str) -> bool
pub fn trigger(&mut self, event: &str) -> bool
Fire event. Returns true if a transition was found and applied.
A transition is eligible when:
- Its
fromfield matchescurrent_state. - Its
triggermatchesevent. - The current state is not terminal.
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
Returns true when the current state is marked terminal.
Sourcepub fn valid_triggers(&self) -> Vec<&str>
pub fn valid_triggers(&self) -> Vec<&str>
Return the event names of all transitions originating from the current state.
Trait Implementations§
Source§impl Clone for StateMachine
impl Clone for StateMachine
Source§fn clone(&self) -> StateMachine
fn clone(&self) -> StateMachine
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for StateMachine
impl RefUnwindSafe for StateMachine
impl Send for StateMachine
impl Sync for StateMachine
impl Unpin for StateMachine
impl UnsafeUnpin for StateMachine
impl UnwindSafe for StateMachine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more