pub struct StateContext {
current_state: CurrentState,
behaviors: Vec<(CurrentState, Box<dyn IStateBehavior>)>,
}
Expand description
Implementation of the state machine. This maintains the context in which the state machine runs.
Fields§
§current_state: CurrentState
The current state of the machine.
behaviors: Vec<(CurrentState, Box<dyn IStateBehavior>)>
Maps values from the CurrentState enumeration to instances of the IStateBehavior representing the behavior for that state. This vector owns the StateXXX struct instances.
Implementations§
source§impl StateContext
impl StateContext
sourcepub fn new() -> StateContext
pub fn new() -> StateContext
sourcepub fn remove_comments(&mut self, text: &str) -> String
pub fn remove_comments(&mut self, text: &str) -> String
Entry point for callers to filter text. Removes Rust-style line and block comments from the text.
Parameters
-
text
The text from which to remove comments.
Returns
Returns the text as a new string, without the comments.
sourcefn set_next_state(&mut self, new_state: &CurrentState)
fn set_next_state(&mut self, new_state: &CurrentState)
Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state.
Parameters
-
new_state
A value from the CurrentState enumeration indicating the state to which to transition.
sourcefn get_behavior(&mut self, state: &CurrentState) -> &mut Box<dyn IStateBehavior>
fn get_behavior(&mut self, state: &CurrentState) -> &mut Box<dyn IStateBehavior>
Helper method to retrieve the behavior corresponding to the given state. A behavior is an implementation of the IStateBehavior trait that can be called to get a new state. There is a different behavior for each state.
The behaviors are created as needed and cached in the StateContext struct.
Parameters
-
state
A value from the CurrentState enumeration indicating which behavior to return.
Returns
Returns a mutable reference to a IStateBehavior instance representing the specified state.