summaryrefslogtreecommitdiff
path: root/src/assemblage.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-14 14:36:32 +0100
committertroido <troido@protonmail.com>2020-02-14 14:36:32 +0100
commit4a4cdf7d148be0a2a756f323d27c0ee5b7976438 (patch)
tree7add0a0d735f93cb1fce6ae4f0c476a0d550a3ee /src/assemblage.rs
parent7821febc8ee4c89ca1825054e0baf39eea3a0380 (diff)
extract the state to save
Diffstat (limited to 'src/assemblage.rs')
-rw-r--r--src/assemblage.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/assemblage.rs b/src/assemblage.rs
index f84037c..dd84837 100644
--- a/src/assemblage.rs
+++ b/src/assemblage.rs
@@ -4,7 +4,9 @@ use serde_json::{Value, json};
use super::componentparameter::ComponentParameter;
use super::parameter::{Parameter, ParameterType};
use super::componentwrapper::{ComponentWrapper, ComponentType};
+use super::components::Serialise;
use crate::hashmap;
+use crate::template::Template;
type ArgumentDef = (String, ParameterType, Option<Parameter>);
@@ -36,16 +38,20 @@ impl Assemblage {
fn parse_definition_components(comps: &Value) -> Result<Vec<(ComponentType, HashMap<String, ComponentParameter>)>, &'static str> {
let mut components = Vec::new();
for tup in comps.as_array().ok_or("components is not a json array")? {
- let comptype = ComponentType::from_str(tup
- .get(0).ok_or("index 0 not in component")?
- .as_str().ok_or("component name not a string")?
- ).ok_or("not a valid componenttype")?;
- let mut parameters: HashMap<String, ComponentParameter> = HashMap::new();
- for (key, value) in tup.get(1).ok_or("index 1 not in component")?.as_object().ok_or("component parameters not a json object")? {
- let param = ComponentParameter::from_json(value)?;
- parameters.insert(key.clone(), param);
+ if let Some(name) = tup.as_str() {
+ components.push((ComponentType::from_str(name).ok_or("not a valid componenttype")?, HashMap::new()));
+ } else {
+ let comptype = ComponentType::from_str(tup
+ .get(0).ok_or("index 0 not in component")?
+ .as_str().ok_or("component name not a string")?
+ ).ok_or("not a valid componenttype")?;
+ let mut parameters: HashMap<String, ComponentParameter> = HashMap::new();
+ for (key, value) in tup.get(1).ok_or("index 1 not in component")?.as_object().ok_or("component parameters not a json object")? {
+ let param = ComponentParameter::from_json(value)?;
+ parameters.insert(key.clone(), param);
+ }
+ components.push((comptype, parameters));
}
- components.push((comptype, parameters));
}
Ok(components)
}
@@ -109,7 +115,9 @@ impl Assemblage {
Ok(arguments)
}
- pub fn instantiate(&self, args: &[Parameter], kwargs: &HashMap<String, Parameter>) -> Result<Vec<ComponentWrapper>, &'static str>{
+ pub fn instantiate(&self, template: &Template) -> Result<Vec<ComponentWrapper>, &'static str>{
+ let args = &template.args;
+ let kwargs = &template.kwargs;
let mut components: Vec<ComponentWrapper> = Vec::new();
let arguments = self.prepare_arguments(args, kwargs)?;
for (comptype, compparams) in &self.components {
@@ -119,6 +127,9 @@ impl Assemblage {
}
components.push(ComponentWrapper::load_component(*comptype, compargs).ok_or("failed to load component")?);
}
+ if template.save {
+ components.push(ComponentWrapper::Serialise(Serialise{template: template.clone()}));
+ }
Ok(components)
}
}