diff options
| author | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
| commit | 5ce9b012a7987f4085057f4f0b0af35e76810a7a (patch) | |
| tree | a99418b5aec06d6be28e22150bce85d3c40b8ee7 /src/parameter.rs | |
| parent | b6a4c7b2d383755402e5e2c6f60d9a75a899b809 (diff) | |
| parent | 455867294cc849bff2c0829a7464e71e79a0dcae (diff) | |
Merge branch 'interact' into master
Diffstat (limited to 'src/parameter.rs')
| -rw-r--r-- | src/parameter.rs | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/parameter.rs b/src/parameter.rs index cee6342..e303f76 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -2,8 +2,9 @@ use serde_json::{Value, json}; use crate::{ Template, - components::interactable::Interactable, - Pos + Pos, + PResult, + perr }; @@ -17,10 +18,10 @@ macro_rules! parameters { )* } impl Parameter { - pub fn from_typed_json(typ: ParameterType, val: &Value) -> Option<Parameter>{ + pub fn from_typed_json(typ: ParameterType, val: &Value) -> PResult<Parameter>{ match typ { $( - ParameterType::$name => Some(Self::$name({ + ParameterType::$name => Ok(Self::$name({ let $v = val; $fromjson })), @@ -63,20 +64,19 @@ macro_rules! parameters { } parameters!( - String (String) string, v (v.as_str()?.to_string()) (json!(v)); - Int (i64) int, v (v.as_i64()?) (json!(v)); - Pos (Pos) pos, v (Pos::from_json(v)?) (json!(v)); - Float (f64) float, v (v.as_f64()?) (json!(v)); - Template (Template) template, v (Template::from_json(v).ok()?) (json!(["template", v.to_json()])); - Interaction (Interactable) interaction, _v (Interactable::from_json(_v)?) (panic!("interactions can't be serialized")); - Bool (bool) bool, v (v.as_bool()?) (json!(v)); + String (String) string, v (v.as_str().ok_or(perr!("{:?} not a string", v))?.to_string()) (json!(v)); + Int (i64) int, v (v.as_i64().ok_or(perr!("{:?} not an int", v))?) (json!(v)); + Pos (Pos) pos, v (Pos::from_json(v).ok_or(perr!("{:?} not a pos", v))?) (json!(v)); + Float (f64) float, v (v.as_f64().ok_or(perr!("{:?} not an float", v))?) (json!(v)); + Template (Template) template, v (Template::from_json(v)?) (json!(["template", v.to_json()])); + Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v)); List (Vec<Parameter>) list, v ({ v - .as_array()? + .as_array().ok_or(perr!("{:?} not an array", v))? .iter() .map(|item| Parameter::guess_from_json(item)) - .collect::<Option<Vec<Parameter>>>()? + .collect::<PResult<Vec<Parameter>>>()? }) (json!(["list", v.iter().map(Parameter::to_json).collect::<Vec<Value>>()])); ); @@ -88,11 +88,11 @@ impl Parameter { Self::String(string.to_string()) } - pub fn guess_from_json(val: &Value) -> Option<Parameter> { + pub fn guess_from_json(val: &Value) -> PResult<Parameter> { if let Some(arr) = val.as_array() { if arr.len() == 2 && arr[0].is_string() { let typestr = arr[0].as_str().unwrap(); - let typ = ParameterType::from_str(typestr)?; + let typ = ParameterType::from_str(typestr).ok_or(perr!("invalid parameter type {}", typestr))?; return Self::from_typed_json(typ, &arr[1]); } } @@ -108,7 +108,7 @@ impl Parameter { } else if val.is_object(){ ParameterType::Template } else { - return None + return Err(perr!("can't guess the type of parameter {:?}", val)); }; Self::from_typed_json(typ, val) } |
