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

/// built-in executor
use nature_common::{NatureError, Result};

use crate::task::ExecutorTrait;

lazy_static! {
    static ref CACHE: HashMap<String,&'static dyn ExecutorTrait> = {
        info!("BuiltIn executor initialized");
        let mut map: HashMap<String,&'static dyn ExecutorTrait> = HashMap::new();
        let cnt : &dyn ExecutorTrait = &simple_counter::SimpleCounter{};
        map.insert("simpleCounter".to_string(), cnt);
        map
    };
}

pub struct BuiltIn;

impl BuiltIn {
    pub fn get(name: &str) -> Result<&'static dyn ExecutorTrait> {
        match CACHE.get(name) {
            Some(x) => Ok(*x),
            None => Err(NatureError::VerifyError(format!("not exists built-in executor for name : {}", name))),
        }
    }
}

mod simple_counter;

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn get_test() {
        assert_eq!(BuiltIn::get("hello").is_err(), true);
        let rtn = BuiltIn::get("simpleCounter");
        assert_eq!(rtn.is_ok(), true);
    }
}