fennel_engine/
registry.rs1use ron::Value;
2use specs::{Entity, World};
3use std::collections::HashMap;
4
5pub trait ComponentFactory: Send + Sync {
8 fn insert(&self, world: &mut World, entity: Entity, value: &Value);
9}
10
11pub struct ComponentRegistry {
13 map: HashMap<String, Box<dyn ComponentFactory>>,
14}
15
16impl ComponentRegistry {
17 pub fn new() -> Self {
19 Self {
20 map: HashMap::new(),
21 }
22 }
23
24 pub fn register(&mut self, name: &str, f: Box<dyn ComponentFactory>) {
26 self.map.insert(name.to_string(), f);
27 }
28
29 pub fn get(&self, name: &str) -> Option<&dyn ComponentFactory> {
31 self.map.get(name).map(|v| &**v)
32 }
33}
34
35impl Default for ComponentRegistry {
36 fn default() -> Self {
37 Self::new()
38 }
39}