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
use std::collections::HashMap;

#[test]
fn state_change()
{
    use std::rc::Rc;
    use std::cell::RefCell;

    struct States;
    impl States
    {
        fn a() -> &'static str { "a" }
        fn b() -> &'static str { "b" }
        fn c() -> &'static str { "c" }
    }

    struct Log { log: String }
    impl Log
    {
        fn append(&mut self, string: &str)
        {
            self.log = self.log.clone() + string;
        }
    }

    let log = Rc::new(RefCell::new(Log { log: String::new() }));

    let mut state = Stator::new(States::a());

    state.add_handler(States::a(), States::b(), |from: &String|
    {
        println!("a -> b");
        log.borrow_mut().append("1");
    });

    state.add_handler(Stator::any_state(), States::b(), |from: &String|
    {
        println!("? -> b");
        log.borrow_mut().append("2");
    });

    state.add_handler(States::a(), Stator::any_state(), |from: &String|
    {
        println!("a -> ?");
        log.borrow_mut().append("3");
    });

    state.add_handler(Stator::any_state(), States::a(), |from: &String|
    {
        println!("? -> a");
        log.borrow_mut().append("4");
    });

    state.enter(States::b());

    assert!(log.borrow_mut().log == "123", "Unexpected log sequence");

    state.enter(States::c());

    assert!(log.borrow_mut().log == "123", "Unexpected log sequence");

    state.enter(States::b());

    assert!(log.borrow_mut().log == "1232", "Unexpected log sequence");

    state.enter(States::a());

    assert!(log.borrow_mut().log == "12324", "Unexpected log sequence");
}

#[derive(Hash, Eq, PartialEq)]
struct StatorHashKey
{
    from: String,
    to: String
}

pub struct Stator<'a>
{
    current_state: String,
    state_handlers: HashMap<StatorHashKey, Vec<Box<FnMut(&String) + 'a>>>
}

impl<'a> Stator<'a>
{
    pub fn any_state() -> &'static str { "_Stator_Internal__Any_State_" }

    pub fn new(state: &str) -> Stator<'a>
    {
        Stator { current_state: state.to_string(), state_handlers: HashMap::new() }
    }

    pub fn add_handler<F: FnMut(&String) + 'a>(&mut self, from: &str, to: &str, f: F)
    {
        let key = StatorHashKey { from: from.to_string(), to: to.to_string() };

        if self.state_handlers.contains_key(&key)
        {
            self.state_handlers.get_mut(&key).unwrap().push(Box::new(f));
        }else
        {
            self.state_handlers.insert(key, vec![Box::new(f)]);
        }
    }

    pub fn enter(&mut self, state: &str)
    {
        {
            let key = StatorHashKey { from: self.current_state.clone(), to: state.to_string() };

            if self.state_handlers.contains_key(&key)
            {
                let handlers = self.state_handlers.get_mut(&key).unwrap();

                for handler in handlers
                {
                    (*handler)(&self.current_state);
                }
            }
        }

        {
            let key = StatorHashKey { from: Stator::any_state().to_string(), to: state.to_string() };

            if self.state_handlers.contains_key(&key)
            {
                let handlers = self.state_handlers.get_mut(&key).unwrap();

                for handler in handlers
                {
                    (*handler)(&self.current_state);
                }
            }
        }

        {
            let key = StatorHashKey { from: self.current_state.clone(), to: Stator::any_state().to_string() };

            if self.state_handlers.contains_key(&key)
            {
                let handlers = self.state_handlers.get_mut(&key).unwrap();

                for handler in handlers
                {
                    (*handler)(&self.current_state);
                }
            }
        }

        {
            let key = StatorHashKey { from: Stator::any_state().to_string(), to: Stator::any_state().to_string() };

            if self.state_handlers.contains_key(&key)
            {
                let handlers = self.state_handlers.get_mut(&key).unwrap();

                for handler in handlers
                {
                    (*handler)(&self.current_state);
                }
            }
        }

        self.current_state = state.to_string();
    }
}