From 69ac6eb6153b016c39bbe55c85f15e3478032182 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 9 Mar 2020 16:54:26 +0100 Subject: can now include variables (like health) in serialisation --- src/assemblage.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/assemblage.rs') diff --git a/src/assemblage.rs b/src/assemblage.rs index adf1ba5..8e3a349 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -18,7 +18,8 @@ type ArgumentDef = (String, ParameterType, Option); pub struct Assemblage { pub arguments: Vec, pub components: Vec<(ComponentType, HashMap)>, - pub save: bool + pub save: bool, + pub extract: Vec<(String, ComponentType, String)> } impl Assemblage { @@ -78,7 +79,26 @@ impl Assemblage { let mut assemblage = Self { arguments: Self::parse_definition_arguments(val.get("arguments").unwrap_or(&json!([])))?, components: Self::parse_definition_components(val.get("components").unwrap_or(&json!([])))?, - save: val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(aerr!("assemblage save not a bool"))? + save: val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(aerr!("assemblage save not a bool"))?, + extract: val + .get("extract") + .unwrap_or(&json!({})) + .as_object().ok_or(aerr!("assemblage extract not a bool"))? + .into_iter() + .map(|(argname, val)| { + Ok(( + argname.to_string(), + ComponentType::from_str( + val + .get(0).ok_or(aerr!("index 0 not in extract value"))? + .as_str().ok_or(aerr!("extract component name not a string"))? + ).ok_or(aerr!("extract invalid component name"))?, + val.get(1) + .ok_or(aerr!("index 1 not in extract value"))? + .as_str().ok_or(aerr!("extract member name not a string"))?.to_string() + )) + }) + .collect::>>()? }; let name = if let Some(nameval) = val.get("name") { Some(nameval.as_str().ok_or(aerr!("name not a string"))?.to_string()) @@ -161,7 +181,7 @@ impl Assemblage { components.push(ComponentWrapper::load_component(*comptype, compargs).ok_or(aerr!("failed to load component"))?); } if template.save && self.save { - components.push(ComponentWrapper::Serialise(Serialise{template: template.clone()})); + components.push(ComponentWrapper::Serialise(Serialise{template: template.clone(), extract: self.extract.clone() })); } Ok(components) } -- cgit