diff options
| author | troido <troido@protonmail.com> | 2020-09-24 13:10:54 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-24 13:10:54 +0200 |
| commit | e6476e7afff0234d67ebce7c74e4c91c31d87755 (patch) | |
| tree | 638133c0f7c9253e863e01806150f8bec860d12e /src/template.rs | |
| parent | 523a635dc88bc7890a0e1d3c062a8165259761d6 (diff) | |
actually use the serde serialisation more
Diffstat (limited to 'src/template.rs')
| -rw-r--r-- | src/template.rs | 71 |
1 files changed, 1 insertions, 70 deletions
diff --git a/src/template.rs b/src/template.rs index da758b6..66d58b9 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,45 +1,22 @@ use std::collections::HashMap; -use serde_json::{json, Value}; use serde::{Serialize, Deserialize}; use crate::{ parameter::Parameter, - PResult, - perr }; #[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)] pub struct EntityType(pub String); -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] -#[serde(from="Option<bool>", into="Option<bool>")] +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub enum SaveOption { Default, False, Always } -impl From<Option<bool>> for SaveOption { - fn from(b: Option<bool>) -> Self { - match b { - Some(true) => Self::Always, - Some(false) => Self::False, - None => Self::Default - } - } -} -impl Into<Option<bool>> for SaveOption { - fn into(self) -> Option<bool> { - match self { - Self::Always => Some(true), - Self::False => Some(false), - Self::Default => None - } - } -} - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(untagged)] enum TemplateSave { @@ -143,52 +120,6 @@ impl Template { } self } - - pub fn from_json(v: &Value) -> PResult<Template> { - let val = match v { - Value::String(s) => json!({"type": s}), - Value::Array(_) => json!({ - "type": v.get(0).ok_or(perr!("index 0 not in template array {:?}", v))?, - "kwargs": v.get(1).ok_or(perr!("index 1 not in template array {:?}", v))? - }), - Value::Object(_) => v.clone(), - _ => return Err(perr!("invalid template {:?}", v)) - }; - - let name = EntityType(val.get("type").ok_or(perr!("template doesn't have 'type'"))?.as_str().ok_or(perr!("template type not a string"))?.to_string()); - let mut args = Vec::new(); - for arg in val.get("args").unwrap_or(&json!([])).as_array().ok_or(perr!("template args not an array"))? { - args.push(Parameter::guess_from_json(arg)?); - } - let mut kwargs = HashMap::new(); - for (key, arg) in val.get("kwargs").unwrap_or(&json!({})).as_object().ok_or(perr!("template kwargs not a json object"))? { - kwargs.insert(key.to_string(), Parameter::guess_from_json(arg)?); - } - let save = - if let Some(saveval) = val.get("save") { - if saveval.as_bool().ok_or(perr!("save not a bool"))? { - SaveOption::Always - } else { - SaveOption::False - } - } else { - SaveOption::Default - }; - Ok(Template {name, args, kwargs, save}) - } - - pub fn to_json(&self) -> Value { - if self.args.is_empty() && self.kwargs.is_empty() { - return json!(self.name.0); - } - 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.0, - "args": jsonargs, - "kwargs": jsonkwargs - }) - } } #[cfg(test)] |
