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);
10}
11
12pub struct ComponentRegistry {
14 map: HashMap<String, Box<dyn ComponentFactory>>,
15}
16
17impl ComponentRegistry {
18 pub fn new() -> Self {
20 Self {
21 map: HashMap::new(),
22 }
23 }
24
25 pub fn register(&mut self, name: &str, f: Box<dyn ComponentFactory>) {
27 self.map.insert(name.to_string(), f);
28 }
29
30 pub fn get(&self, name: &str) -> Option<&dyn ComponentFactory> {
32 self.map.get(name).map(|v| &**v)
33 }
34}
35
36impl Default for ComponentRegistry {
37 fn default() -> Self {
38 Self::new()
39 }
40}