diff options
| author | troido <troido@protonmail.com> | 2020-04-06 16:01:00 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-06 16:01:00 +0200 |
| commit | 66a3d3131f32e7bae2f0f7c4fd0b0c876eb3e8a0 (patch) | |
| tree | b6e7fe873be9c17b49596946543ee24f3771745d /src/template.rs | |
| parent | e8d3e3c4f69fc5bab2b32b16b7c8c2c4a8a89a4b (diff) | |
shortcut or defining some entities like crops
Diffstat (limited to 'src/template.rs')
| -rw-r--r-- | src/template.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/template.rs b/src/template.rs index c2f905f..850a323 100644 --- a/src/template.rs +++ b/src/template.rs @@ -49,10 +49,17 @@ impl Template { self } - pub fn from_json(val: &Value) -> PResult<Template> { - if val.is_string(){ - return Ok(Self::empty(val.as_str().unwrap())); - } + 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(), + _ => 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"))? { |
