roopes_core/patterns/state/
simple.rs

1//! Implements a basic wrapper [`Context`] which
2//! contains a generic [`State`] object.
3
4use super::{
5    Context,
6    State,
7};
8
9/// A basic implementation of [`Context`].  Stores
10/// the current generic [`State`], and possibly
11/// transitions to a new state when
12/// [`Context::handle`] is called.
13#[allow(clippy::module_name_repetitions)]
14pub struct SimpleContext<S>
15where
16    S: State,
17{
18    state: S,
19}
20
21impl<S> SimpleContext<S>
22where
23    S: State,
24{
25    /// Creates a new [`SimpleContext`] with a
26    /// given starting [`State`].
27    pub fn new(starting_state: S) -> SimpleContext<S>
28    {
29        Self {
30            state: starting_state,
31        }
32    }
33
34    /// Gets the current [`State`].
35    pub fn get_state(&self) -> &S
36    {
37        &self.state
38    }
39}
40
41impl<S> Context<S> for SimpleContext<S>
42where
43    S: State,
44{
45    fn handle(&mut self)
46    {
47        self.state = self.state.execute();
48    }
49}