From 1899b27b791734a6b72e28cfb1420536c6035ee4 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 21 May 2020 15:26:12 +0200 Subject: added exchanger as seperate component/system; refactored other interactions; parameter parsing returns result instead of option --- src/parameter.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/parameter.rs') diff --git a/src/parameter.rs b/src/parameter.rs index cee6342..e2d48dd 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -3,7 +3,9 @@ use serde_json::{Value, json}; use crate::{ Template, components::interactable::Interactable, - Pos + Pos, + PResult, + perr }; @@ -17,10 +19,10 @@ macro_rules! parameters { )* } impl Parameter { - pub fn from_typed_json(typ: ParameterType, val: &Value) -> Option{ + pub fn from_typed_json(typ: ParameterType, val: &Value) -> PResult{ match typ { $( - ParameterType::$name => Some(Self::$name({ + ParameterType::$name => Ok(Self::$name({ let $v = val; $fromjson })), @@ -63,20 +65,20 @@ 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()])); + Interaction (Interactable) interaction, _v (Interactable::from_json(_v).ok_or(perr!("{:?} not an interactable", _v))?) (panic!("interactions can't be serialized")); + Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v)); List (Vec) list, v ({ v - .as_array()? + .as_array().ok_or(perr!("{:?} not an array", v))? .iter() .map(|item| Parameter::guess_from_json(item)) - .collect::>>()? + .collect::>>()? }) (json!(["list", v.iter().map(Parameter::to_json).collect::>()])); ); @@ -88,11 +90,11 @@ impl Parameter { Self::String(string.to_string()) } - pub fn guess_from_json(val: &Value) -> Option { + pub fn guess_from_json(val: &Value) -> PResult { 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 +110,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) } -- cgit From 1e7c665728eb638cdf921824f4c22f54b81f29de Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 20 Sep 2020 23:37:28 +0200 Subject: removed interactable as Parameter type --- src/parameter.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/parameter.rs') diff --git a/src/parameter.rs b/src/parameter.rs index e2d48dd..e303f76 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -2,7 +2,6 @@ use serde_json::{Value, json}; use crate::{ Template, - components::interactable::Interactable, Pos, PResult, perr @@ -70,7 +69,6 @@ parameters!( 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()])); - Interaction (Interactable) interaction, _v (Interactable::from_json(_v).ok_or(perr!("{:?} not an interactable", _v))?) (panic!("interactions can't be serialized")); Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v)); List (Vec) list, v ({ -- cgit