blob: 6851a066841bf9bcd0242d60607f063c129aff82 (
plain)
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
|
use std::collections::HashMap;
use serde_json::Value;
use crate::assemblage::Assemblage;
use crate::componentwrapper::PreEntity;
use crate::template::Template;
#[derive(Default, Clone)]
pub struct Encyclopedia {
items: HashMap<String, Assemblage>
}
impl Encyclopedia {
pub fn from_json(val: Value) -> Result<Encyclopedia, &'static str> {
let mut items = HashMap::new();
for (k, v) in val.as_object().ok_or("encyclopedia not a json object")?.into_iter() {
items.insert(k.clone(), Assemblage::from_json(v)?);
}
Ok(Encyclopedia{items})
}
pub fn construct(&self, template: &Template) -> Result<PreEntity, &'static str> {
let assemblage = self.items.get(&template.name).ok_or("unknown assemblage name")?;
assemblage.instantiate(&template.args, &template.kwargs)
}
}
|