diff options
| author | troido <troido@protonmail.com> | 2020-02-07 14:08:18 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-07 14:08:18 +0100 |
| commit | 70db58f688f0dbdd69231da570cf8dbb54e5ca81 (patch) | |
| tree | ab8d61357f17ebd30c15e7206dde110da0fd1579 /src/encyclopedia.rs | |
| parent | 45ed2cd201c79fae1c61a4f6f2982a9f5cfbceca (diff) | |
named stuff properly; added encyclopedia and template
Diffstat (limited to 'src/encyclopedia.rs')
| -rw-r--r-- | src/encyclopedia.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs new file mode 100644 index 0000000..17e9857 --- /dev/null +++ b/src/encyclopedia.rs @@ -0,0 +1,41 @@ + +use std::collections::HashMap; +use serde_json::Value; +use crate::assemblage::Assemblage; +use crate::componentwrapper::ComponentWrapper; +use crate::template::Template; + +#[derive(Default)] +pub struct Encyclopedia { + items: HashMap<String, Assemblage> +} + +impl Encyclopedia { + + pub fn new() -> Encyclopedia { + Encyclopedia { + items: HashMap::new() + } + } + + 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 add_assemblage(&mut self, name: &str, assemblage: Assemblage) -> Result<(), &'static str> { + //todo: what if name exists + self.items.insert(name.to_string(), assemblage); + Ok(()) + } + + pub fn construct(&self, template: &Template) -> Result<Vec<ComponentWrapper>, &str> { + let assemblage = self.items.get(&template.name).ok_or("unknown assemblage name")?; + assemblage.instantiate(&template.args, &template.kwargs) + } + +} + |
