summaryrefslogtreecommitdiff
path: root/src/template.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-04 22:23:17 +0100
committertroido <troido@protonmail.com>2020-02-04 22:23:17 +0100
commit323cd679cd29a8475c3b7486ce54ecd37620dbea (patch)
tree1fc55c768a7b74644157c23580f54292e960c66a /src/template.rs
parenteb5997cbf94b1aa230cf4acb3008a7fe80ec36e0 (diff)
tried to implement deserialisation of entities
Diffstat (limited to 'src/template.rs')
-rw-r--r--src/template.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/template.rs b/src/template.rs
new file mode 100644
index 0000000..d6f5c81
--- /dev/null
+++ b/src/template.rs
@@ -0,0 +1,48 @@
+
+use std::collections::HashMap;
+use serde_json::Value;
+use super::compwrapper::CompWrapper;
+
+pub struct Template {
+ pub arguments: Vec<String>,
+ pub components: Vec<(String, HashMap<String, CompParam>)>
+}
+
+impl Template {
+ pub fn instantiate(&self, args: Vec<Value>, kwargs: HashMap<String, Value>) -> Option<Vec<CompWrapper>>{
+ let mut components: Vec<CompWrapper> = Vec::new();
+ for (compname, compparams) in &self.components {
+ let mut compargs: HashMap<&str, &Value> = HashMap::new();
+ for (name, param) in compparams {
+ match param {
+ CompParam::Constant(val) => {compargs.insert(name.as_str(), &val); Some(())},
+ CompParam::Argument(argname) => {
+ if let Some(argval) = kwargs.get(argname.as_str()) {
+ compargs.insert(name.as_str(), argval);
+ Some(())
+ } else if let Some(idx) = self.arguments.iter().position(|x| x == name){
+ if idx < args.len() {
+ compargs.insert(name.as_str(), &args[idx]);
+ Some(())
+ } else {
+ println!("positional argument out of range");
+ None
+ }
+ } else {
+ println!("can't find parameter value, compname: {}, name: {}, argname: {}", compname, name, argname);
+ None
+ }
+ }
+ }?;
+ }
+ components.push(CompWrapper::load_component(compname.as_str(), compargs)?);
+ }
+ Some(components)
+ }
+}
+
+
+pub enum CompParam {
+ Constant(Value),
+ Argument(String)
+}