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

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Util {
    pub is_hidden: bool,
    pub name: &'static str,
    pub category: &'static str,
    pub content: &'static str,
}

mod get_all_utils {
    include!(concat!(env!("OUT_DIR"), "/get_all_utils.rs"));
}

lazy_static::lazy_static! {
    static ref HIDDEN_SET: HashSet<&'static str> = {
        let mut set = HashSet::<&str>::new();
        set.insert("util/common");
        set.insert("util/hs_env");
        set
    };
}

pub fn get_all() -> Vec<Util> {
    get_all_utils::get_all()
        .into_iter()
        .map(|(name, category, content)| Util {
            is_hidden: HIDDEN_SET.contains(name),
            name,
            category,
            content,
        })
        .collect()
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_get_all() {
        let utils = get_all();
        let comm = utils
            .iter()
            .find(|u| u.name == "util/common")
            .expect("找不到應該存在的工具");
        assert!(comm.is_hidden, "{:?} 有問題", comm);
        assert_eq!(comm.category, "rb", "{:?} 有問題", comm);
        assert_eq!(include_str!("../util/common.rb"), comm.content);

        let import = utils
            .iter()
            .find(|u| u.name == "util/import")
            .expect("找不到應該存在的工具");
        assert!(!import.is_hidden, "{:?} 有問題", import);
        assert_eq!(import.category, "rb", "{:?} 有問題", import);
        assert_eq!(include_str!("../util/import.rb"), import.content);

        assert_eq!(
            None,
            utils.iter().find(|u| u.name == "not-exist"),
            "找到了不存在的工具"
        );
    }
}