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
use error::*; use func::Func; use serialization::FuncMapperSerde; use std::collections::HashMap; pub struct FuncMapper { func_map: Vec<Option<HashMap<String, usize>>>, func_list: Vec<Box<Func>>, } impl FuncMapper { pub fn new( func_map: Vec<Option<HashMap<String, usize>>>, func_list: Vec<Box<Func>> ) -> Self { FuncMapper { func_map: func_map, func_list: func_list, } } pub fn get(&self, func_id: usize) -> Result<&Func> { match self.func_list.get(func_id) { Some(some) => Ok(some.as_ref()), None => Err(ErrorKind::EventNotFoundInEventList(func_id, self.func_list.len()).into()) } } pub fn get_id<TStr>(&self, module_id: usize, func_name: TStr) -> Result<usize> where TStr: AsRef<str> { let func_name = func_name.as_ref(); match self.func_map.get(module_id) { Some(&Some(ref some)) => { match some.get(func_name) { Some(some) => Ok(some.clone()), None => Err(ErrorKind::FuncNotFoundInFuncMap(module_id, func_name.to_string()).into()) } }, None | Some(&None) => Err(ErrorKind::FuncNotFoundInFuncMap(module_id, func_name.to_string()).into()) } } pub fn get_raw_list(&self) -> &Vec<Box<Func>> { &self.func_list } pub fn get_raw_map(&self) -> &Vec<Option<HashMap<String, usize>>> { &self.func_map } pub fn make_serde(&self) -> FuncMapperSerde { FuncMapperSerde::new(&self.func_map) } }