about summary refs log tree commit diff
path: root/src/entities/raws.rs
blob: 061e29a840371ffecb0b2e8893285c01dc235e88 (plain) (blame)
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
pub use crate::entities::raw_types::{CreatureType, EntityRaw, ItemType};
use std::collections::HashMap;

static_cfg! {
    static ref RAWS: Vec<EntityRaw<'static>> = cfg_dir("src/entities/raws");
}

lazy_static! {
    static ref RAWS_BY_NAME: HashMap<&'static str, &'static EntityRaw<'static>> = {
        let mut hm = HashMap::new();
        for er in RAWS.iter() {
            if hm.contains_key(er.name()) {
                panic!("Duplicate entity: {}", er.name())
            }

            hm.insert(er.name(), er);
        }
        hm
    };
}

pub fn raw(name: &'static str) -> &'static EntityRaw<'static> {
    RAWS_BY_NAME
        .get(name)
        .copied()
        .unwrap_or_else(|| panic!("Raw not found: {}", name))
}

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

    #[test]
    fn test_raws() {
        RAWS_BY_NAME.keys();
        assert_eq!(raw("noodles").name(), "noodles");
    }
}