From 4a4cdf7d148be0a2a756f323d27c0ee5b7976438 Mon Sep 17 00:00:00 2001 From: troido Date: Fri, 14 Feb 2020 14:36:32 +0100 Subject: extract the state to save --- src/template.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/template.rs') diff --git a/src/template.rs b/src/template.rs index 0f846fd..5347e51 100644 --- a/src/template.rs +++ b/src/template.rs @@ -8,7 +8,8 @@ use crate::parameter::Parameter; pub struct Template { pub name: String, pub args: Vec, - pub kwargs: HashMap + pub kwargs: HashMap, + pub save: bool } @@ -18,7 +19,8 @@ impl Template { Self { name: name.to_string(), args: Vec::new(), - kwargs + kwargs, + save: true } } @@ -39,6 +41,19 @@ impl Template { for (key, arg) in val.get("kwargs").unwrap_or(&json!({})).as_object()? { kwargs.insert(key.to_string(), Parameter::guess_from_json(arg)?); } - Some(Template {name, args, kwargs}) + Some(Template {name, args, kwargs, save: true}) + } + + pub fn to_json(&self) -> Value { + if self.args.is_empty() && self.kwargs.is_empty() { + return json!(self.name); + } + let jsonargs: Vec = self.args.iter().map(|a| a.to_json()).collect(); + let jsonkwargs: HashMap<&String, Value> = self.kwargs.iter().map(|(k, a)| (k, a.to_json())).collect(); + json!({ + "type": self.name, + "args": jsonargs, + "kwargs": jsonkwargs + }) } } -- cgit