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
use actiondispatch::dispatchable::Dispatchable;
use std::collections::HashMap;
use xfstruct::XFlowNode;
use xfstate::XFState;

type Dispatchers<'a> = HashMap<String, Box<Dispatchable + 'a>>;

pub struct Dispatcher<'a> {
    dispatchers: Dispatchers<'a>,
}

impl<'a> Dispatcher<'a> {
    pub fn new() -> Dispatcher<'a> {
        let dispatchers: Dispatchers = HashMap::new();

        Dispatcher { dispatchers: dispatchers }
    }

    pub fn register_dispatcher<T: Dispatchable + 'a>(&mut self, name: &str, dispatcher: T) -> () {
        let disp_box = Box::new(dispatcher);
        self.dispatchers.insert(name.to_owned(), disp_box);
    }

    pub fn dispatch(&self, xfnode: &XFlowNode, xfstate: &mut XFState) -> bool {
        println!("Dispatch {}/{}!", xfnode.nodetype, xfnode.action);

        if let Some(dispatch) = self.dispatchers.get(&xfnode.nodetype) {
            dispatch.dispatch(xfnode, xfstate);
            true
        } else {
            false
        }

    }
}