1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
use std::{cell::RefCell, marker::PhantomData}; pub mod builder; pub mod error; /// The trait is representing the basic operation for the state machine. /// It includes getting its current state, transition to the next state, /// resetting its current state to initial state and setting particular state forcibly. /// [`BasicStateMachine`] is a good example to implement it. /// Of course, you can build your own state machine by using this trait. pub trait StateMachine<State, Input> { /// Returns the current state of the state machine. /// /// # Example /// ``` /// use statemachine_rs::machine::{builder::StateMachineBuilder, StateMachine}; /// /// #[derive(Clone, Debug, PartialEq)] /// enum ButtonState { /// On, /// Off, /// } /// /// #[allow(dead_code)] /// enum Input { /// Press, /// } /// /// let sm = StateMachineBuilder::start() /// .initial_state(ButtonState::Off) /// .transition(|state, input| match (state, input) { /// (ButtonState::On, Input::Press) => ButtonState::Off, /// (ButtonState::Off, Input::Press) => ButtonState::On, /// }) /// .build() /// .unwrap(); /// /// assert_eq!(ButtonState::Off, sm.current_state()); /// ``` fn current_state(&self) -> State; /// Returns the result of state transition according to `input` and /// the definition of transition function. /// /// # Example /// ``` /// use statemachine_rs::machine::{builder::StateMachineBuilder, StateMachine}; /// /// #[derive(Clone, Debug, PartialEq)] /// enum ButtonState { /// On, /// Off, /// } /// /// enum Input { /// Press, /// } /// /// let sm = StateMachineBuilder::start() /// .initial_state(ButtonState::Off) /// .transition(|state, input| match (state, input) { /// (ButtonState::On, Input::Press) => ButtonState::Off, /// (ButtonState::Off, Input::Press) => ButtonState::On, /// }) /// .build() /// .unwrap(); /// /// assert_eq!(ButtonState::Off, sm.current_state()); /// assert_eq!(ButtonState::On, sm.consume(Input::Press)); /// ``` fn consume(&self, input: Input) -> State; /// Returns the next state from the current state but the state machine /// retains in its current state. /// /// # Example /// ``` /// use statemachine_rs::machine::{builder::StateMachineBuilder, StateMachine}; /// /// #[derive(Clone, Debug, PartialEq)] /// enum ButtonState { /// On, /// Off, /// } /// /// enum Input { /// Press, /// } /// /// let sm = StateMachineBuilder::start() /// .initial_state(ButtonState::Off) /// .transition(|state, input| match (state, input) { /// (ButtonState::On, Input::Press) => ButtonState::Off, /// (ButtonState::Off, Input::Press) => ButtonState::On, /// }) /// .build() /// .unwrap(); /// /// assert_eq!(ButtonState::Off, sm.current_state()); /// assert_eq!(ButtonState::On, sm.peek(Input::Press)); /// assert_eq!(ButtonState::Off, sm.current_state()); /// ``` fn peek(&self, input: Input) -> State; /// Resets the current state to the initial state. /// /// # Example /// ``` /// use statemachine_rs::machine::{builder::StateMachineBuilder, StateMachine}; /// /// #[derive(Clone, Debug, PartialEq)] /// enum ButtonState { /// On, /// Off, /// } /// /// enum Input { /// Press, /// } /// /// let sm = StateMachineBuilder::start() /// .initial_state(ButtonState::Off) /// .transition(|state, input| match (state, input) { /// (ButtonState::On, Input::Press) => ButtonState::Off, /// (ButtonState::Off, Input::Press) => ButtonState::On, /// }) /// .build() /// .unwrap(); /// /// assert_eq!(ButtonState::Off, sm.current_state()); /// assert_eq!(ButtonState::On, sm.consume(Input::Press)); /// assert_eq!(ButtonState::Off, sm.reset()); /// ``` fn reset(&self) -> State; /// Set a new state forcibly to the current state. /// /// # Example /// ``` /// use statemachine_rs::machine::{builder::StateMachineBuilder, StateMachine}; /// /// #[derive(Clone, Debug, PartialEq)] /// enum ButtonState { /// On, /// Off, /// Disable, /// } /// /// enum Input { /// Press, /// } /// /// let sm = StateMachineBuilder::start() /// .initial_state(ButtonState::Off) /// .transition(|state, input| match (state, input) { /// (ButtonState::On, Input::Press) => ButtonState::Off, /// (ButtonState::Off, Input::Press) => ButtonState::On, /// (ButtonState::Disable, Input::Press) => ButtonState::Disable, /// }) /// .build() /// .unwrap(); /// /// assert_eq!(ButtonState::Off, sm.current_state()); /// sm.set(ButtonState::Disable); /// assert_eq!(ButtonState::Disable, sm.consume(Input::Press)); /// ``` fn set(&self, new_state: State); } /// [`StateWrapper`] is a struct for interior mutability. /// It enables to acquire the control of switching mutable/imutable /// with [`std::cell::RefCell`]. pub(crate) struct StateWrapper<State: Clone>(State); impl<State> StateWrapper<State> where State: Clone, { pub fn new(state: State) -> Self { StateWrapper(state) } pub fn get(&self) -> State { self.0.clone() } pub fn set(&mut self, state: State) { self.0 = state; } } /// The basic state machine implementation. /// It holds `initial_state`, `current_state`, `transition` function. pub struct BasicStateMachine<State, Input, Transition> where Transition: Fn(&State, Input) -> State, State: Clone, { /// `initial_state` is literally an initial state of the state machine. /// The field isn't updated the whole life of its state machine. /// That is, it always returns its initial state of its machine. initial_state: State, /// `current_state` is the current state of the state machine. /// It transit to the next state via `transition`. current_state: RefCell<StateWrapper<State>>, /// `transition` is the definition of state transition. /// See an example of [`StateMachine::consume()`], you can grasp how /// to define the transition. transition: Transition, _maker: PhantomData<Input>, } impl<State, Input, Transition> StateMachine<State, Input> for BasicStateMachine<State, Input, Transition> where Transition: Fn(&State, Input) -> State, State: Clone, { fn current_state(&self) -> State { self.current_state.borrow().get() } fn consume(&self, input: Input) -> State { let new_state = (self.transition)(&self.current_state.borrow().0, input); self.current_state.borrow_mut().set(new_state); self.current_state() } fn peek(&self, input: Input) -> State { (self.transition)(&self.current_state.borrow().0, input) } fn reset(&self) -> State { self.current_state .borrow_mut() .set(self.initial_state.clone()); self.current_state() } fn set(&self, new_state: State) { self.current_state.borrow_mut().set(new_state) } } #[cfg(test)] mod test { use std::{cell::RefCell, marker::PhantomData}; use super::StateMachine; use super::{BasicStateMachine, StateWrapper}; #[derive(Copy, Clone, Debug, PartialEq)] enum Stations { Shibuya, IkejiriOhashi, Sangendyaya, KomazawaDaigaku, Sakurashinmachi, Yoga, FutakoTamagawa, } enum Train { Local, Express, } #[test] fn test_current_state() { let sm = BasicStateMachine { initial_state: Stations::Shibuya, current_state: RefCell::new(StateWrapper::new(Stations::Shibuya)), transition: |station, train| match (station, train) { (Stations::Shibuya, Train::Local) => Stations::IkejiriOhashi, _ => unreachable!(), }, _maker: PhantomData::<Train>::default(), }; assert_eq!(Stations::Shibuya, sm.current_state()); } #[test] fn test_consume() { let sm = BasicStateMachine { initial_state: Stations::Shibuya, current_state: RefCell::new(StateWrapper::new(Stations::Shibuya)), transition: |station, train| match (station, train) { (Stations::Shibuya, Train::Local) => Stations::IkejiriOhashi, (Stations::Shibuya, Train::Express) => Stations::Sangendyaya, (Stations::IkejiriOhashi, Train::Local) => Stations::Sangendyaya, (Stations::Sangendyaya, Train::Local) => Stations::KomazawaDaigaku, (Stations::Sangendyaya, Train::Express) => Stations::FutakoTamagawa, (Stations::KomazawaDaigaku, Train::Local) => Stations::Sakurashinmachi, (Stations::Sakurashinmachi, Train::Local) => Stations::Yoga, _ => unreachable!(), }, _maker: PhantomData::<Train>::default(), }; assert_eq!(Stations::IkejiriOhashi, sm.consume(Train::Local)); } #[test] fn test_peek() { let sm = BasicStateMachine { initial_state: Stations::Sangendyaya, current_state: RefCell::new(StateWrapper::new(Stations::Sangendyaya)), transition: |station, train| match (station, train) { (Stations::Shibuya, Train::Local) => Stations::IkejiriOhashi, (Stations::Shibuya, Train::Express) => Stations::Sangendyaya, (Stations::IkejiriOhashi, Train::Local) => Stations::Sangendyaya, (Stations::Sangendyaya, Train::Local) => Stations::KomazawaDaigaku, (Stations::Sangendyaya, Train::Express) => Stations::FutakoTamagawa, (Stations::KomazawaDaigaku, Train::Local) => Stations::Sakurashinmachi, (Stations::Sakurashinmachi, Train::Local) => Stations::Yoga, _ => unreachable!(), }, _maker: PhantomData::<Train>::default(), }; assert_eq!(Stations::FutakoTamagawa, sm.peek(Train::Express)); assert_eq!(Stations::Sangendyaya, sm.current_state()); } #[test] fn test_reset() { let sm = BasicStateMachine { initial_state: Stations::Shibuya, current_state: RefCell::new(StateWrapper::new(Stations::Sangendyaya)), transition: |station, train| match (station, train) { (Stations::Shibuya, Train::Local) => Stations::IkejiriOhashi, (Stations::Shibuya, Train::Express) => Stations::Sangendyaya, (Stations::IkejiriOhashi, Train::Local) => Stations::Sangendyaya, (Stations::Sangendyaya, Train::Local) => Stations::KomazawaDaigaku, (Stations::Sangendyaya, Train::Express) => Stations::FutakoTamagawa, (Stations::KomazawaDaigaku, Train::Local) => Stations::Sakurashinmachi, (Stations::Sakurashinmachi, Train::Local) => Stations::Yoga, _ => unreachable!(), }, _maker: PhantomData::<Train>::default(), }; assert_eq!(Stations::FutakoTamagawa, sm.consume(Train::Express)); assert_eq!(Stations::Shibuya, sm.reset()); } #[test] fn test_set() { let sm = BasicStateMachine { initial_state: Stations::Shibuya, current_state: RefCell::new(StateWrapper::new(Stations::Shibuya)), transition: |station, train| match (station, train) { (Stations::Shibuya, Train::Local) => Stations::IkejiriOhashi, _ => unreachable!(), }, _maker: PhantomData::<Train>::default(), }; assert_eq!(Stations::Shibuya, sm.current_state()); sm.set(Stations::Yoga); assert_eq!(Stations::Yoga, sm.current_state()) } }