blob: a7bb5b6ac8cbc200c88480a0765890806a6f40ce (
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
30
31
|
use std::collections::HashMap;
use serde_json::Value;
use crate::{
assemblage::Assemblage,
componentwrapper::PreEntity,
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)
}
}
|