diff options
| author | troido <troido@protonmail.com> | 2020-02-14 14:36:32 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-14 14:36:32 +0100 |
| commit | 4a4cdf7d148be0a2a756f323d27c0ee5b7976438 (patch) | |
| tree | 7add0a0d735f93cb1fce6ae4f0c476a0d550a3ee /src/template.rs | |
| parent | 7821febc8ee4c89ca1825054e0baf39eea3a0380 (diff) | |
extract the state to save
Diffstat (limited to 'src/template.rs')
| -rw-r--r-- | src/template.rs | 21 |
1 files changed, 18 insertions, 3 deletions
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<Parameter>, - pub kwargs: HashMap<String, Parameter> + pub kwargs: HashMap<String, Parameter>, + 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<Value> = 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 + }) } } |
