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
/*
 * Copyright (c) 2015 Brandon Sanderson
 * Part of the genfsm library.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
use std::borrow::Borrow;

///Represents the next state an FSM will enter
pub enum NextState<S,I>{
  ///Change the state and the executor that handles input
  ChangeState(Executor<S,I>, S),
  ///Continue with the same executor
  Continue(S),
  ///FSM has reached completion.  Any further attempts
  ///to handle input will result in no changes
  End(S)
}

///An input handler that executes changes on a state
pub type Executor<S,I> = fn(S, I) -> NextState<S,I>;

///The core of the GenFSM library.  Holds the Executor
///and current state of a state machine, and handles
///the processing of NextState values from Executors.
pub struct StateMachine<S, I> {
  ///The next Executor to handle input.  Will be None
  ///if this state machine has finished.
  next_executor:Option<Executor<S,I>>,
  ///The current state.  Should only ever be None when the process
  ///method is running.
  current_state:Option<S>
}

impl<S,I> StateMachine<S,I>{
  ///Processes new input, using the current executor
  ///to update state.
  pub fn process(&mut self, input_val:I){
    if let Some(executor) = self.next_executor.take() {

      let old_state = self.current_state.take().unwrap();

      let next_state = executor(old_state, input_val);

      match next_state {
        NextState::ChangeState(exec, state) => {
          self.next_executor = Some(exec);
          self.current_state = Some(state);
        }
        NextState::Continue(state) => {
          self.current_state = Some(state);
        }
        NextState::End(state) => {
          self.next_executor = None;
          self.current_state = Some(state);
        }
      }
    }
  }

  ///Returns true if there is no executor to handle additional input.
  pub fn is_complete(&self) -> bool{
    self.next_executor.is_none()
  }
  ///Extracts the current state from the state machine.
  pub fn extract_state(self) -> S {
    self.current_state.unwrap()
  }

  ///Creates a state machine starting with the given state.
  ///The executor given will be used to process input.
  pub fn new(exec:Executor<S,I>, state:S) -> StateMachine<S,I>{
    StateMachine{next_executor:Some(exec), current_state:Some(state)}
  }
}

impl<S,I> Borrow<S> for StateMachine<S, I> {
  fn borrow(&self) -> &S {
    self.current_state.as_ref().unwrap()
  }
}

impl<S:Clone, I> StateMachine<S,I>{
  ///Returns a clone of the current state
  pub fn clone_state(&self) -> S {
    self.current_state.clone().unwrap()
  }
}

#[test]
fn simple_storage(){
  let storer:Executor<Option<isize>, isize> = simple_storage_statefn;
  let mut state_machine:StateMachine<Option<isize>, isize> = StateMachine::new(storer, None);
  assert!(state_machine.clone_state() == None);
  state_machine.process(2);
  assert!(state_machine.clone_state() == Some(2));
  state_machine.process(0);
  assert!(state_machine.is_complete());
  state_machine.process(10);
  assert!(state_machine.is_complete());
  assert!(state_machine.extract_state() == Some(2));
}

#[cfg(test)]
fn simple_storage_statefn(state:Option<isize>, input:isize) -> NextState<Option<isize>, isize> {
  if input == 0 {
    NextState::End(state)
  }else if input < 0 {
    NextState::Continue(None)
  }else{
    NextState::Continue(Some(input))
  }
}