Module design_pattern_examples_rust::state
source · Expand description
The State design pattern example module
The State pattern alters the behavior of an object hierarchy based on some state. This is the basis of a Finite State Machine.
In this exercise, the State struct is a filter that parses text to remove Rust-style line and block comments. It needs to be smart enough to ignore comment characters inside quotes.
The filtering process starts with creating the context that drives the state machine. Internal structs are provided for each state.
Accessed through the state_exercise() function.
Modules
- Contains the StateContext struct and the other structs used to track state in the finite state machine.
- Contains the CurrentState enum and the IStateBehavior trait, which is used by all states to ensure they are compliant with the expected behavior of the state machine.
- Contains the IStateContext trait, which is implemented on the StateContext struct. Each of the state structs holds a reference to the StateContext struct as an IStateContext trait.
Functions
- Helper function to display text from the State exercise. Text is displayed with line numbers.
- Example of using the “State” design pattern.