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