summaryrefslogtreecommitdiff
path: root/src/encyclopedia.rs
blob: 8de5424997d78c70cbf63ac00d67c83ca2ec9dce (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
32
33
34

use std::collections::HashMap;
use serde_json::Value;
use crate::{
	assemblage::Assemblage,
	componentwrapper::PreEntity,
	Template,
	template::EntityType,
	Result,
	aerr
};

#[derive(Default, Clone)]
pub struct Encyclopedia {
	items: HashMap<EntityType, Assemblage>
}

impl Encyclopedia {
	
	pub fn from_json(val: Value) -> Result<Encyclopedia> {
		let mut items = HashMap::new();
		for (k, v) in val.as_object().ok_or(aerr!("encyclopedia not a json object"))?.into_iter() {
			items.insert(EntityType(k.clone()), Assemblage::from_json(v)?);
		}
		Ok(Encyclopedia{items})
	}
	
	pub fn construct(&self, template: &Template) -> Result<PreEntity> {
		let assemblage = self.items.get(&template.name).ok_or(aerr!("unknown assemblage name"))?;
		assemblage.instantiate(template)
	}
	
}