summaryrefslogtreecommitdiff
path: root/src/template.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/template.rs')
-rw-r--r--src/template.rs71
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)]