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
use error::*;
use serialization::ActionMapperSerde;
use std::collections::HashMap;

#[derive(Debug)]
pub struct ActionMapper {
    action_map: Vec<Option<HashMap<String, usize>>>,
}

impl ActionMapper {
    pub fn new(
        action_map: Vec<Option<HashMap<String, usize>>>,
    ) -> Self {
        ActionMapper {
            action_map: action_map,
        }
    }

    pub fn get<TStr>(&self, module_id: usize, action_name: TStr) -> Result<usize>
        where TStr: AsRef<str> {
        let action_name = action_name.as_ref();
        match self.action_map.get(module_id) {
            Some(&Some(ref some)) => {
                match some.get(action_name) {
                    Some(some) => Ok(some.clone()),
                    None => Err(ErrorKind::ActionNotFoundInMap(module_id, action_name.to_string()).into())
                }
            },
            None | Some(&None) => Err(ErrorKind::ActionNotFoundInMap(module_id, action_name.to_string()).into()),
        }
    }

    pub fn get_raw_map(&self) -> &Vec<Option<HashMap<String, usize>>> {
        &self.action_map
    }

    pub fn make_serde(&self) -> ActionMapperSerde {
        ActionMapperSerde::new(&self.action_map)
    }
}