From a9b9e5876a93bd2ba701ab7c766746856df83820 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 6 Feb 2020 14:04:34 +0100 Subject: even more refactoring --- src/compwrapper.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/compwrapper.rs b/src/compwrapper.rs index b027c10..b38b626 100644 --- a/src/compwrapper.rs +++ b/src/compwrapper.rs @@ -164,9 +164,9 @@ pub struct Template { impl Template { - fn parse_definition_arguments(args: &Vec) -> Result)>, &'static str> { + fn parse_definition_arguments(args: &Value) -> Result)>, &'static str> { let mut arguments: Vec<(String, ParamType, Option)> = Vec::new(); - for arg in args { + for arg in args.as_array().ok_or("arguments is not an array")? { let tup = arg.as_array().ok_or("argument is not an array")?; let key = tup.get(0).ok_or("argument has no name")?.as_str().ok_or("argument name is not a string")?.to_string(); let typ = ParamType::from_str(tup.get(1).ok_or("argument has no type")?.as_str().ok_or("argument type not a string")?).ok_or("failed to parse argument type")?; @@ -180,10 +180,9 @@ impl Template { Ok(arguments) } - pub fn from_json(val: Value) -> Result{ - let arguments = Self::parse_definition_arguments(val.get("arguments").ok_or("property 'arguments' not found")?.as_array().ok_or("arguments is not an array")?)?; + fn parse_definition_components(comps: &Value, arguments: &Vec<(String, ParamType, Option)>) -> Result)>, &'static str> { let mut components = Vec::new(); - for tup in val.get("components").ok_or("property 'arguments' not found")?.as_array().ok_or("arguments is not a json object")? { + for tup in comps.as_array().ok_or("components is not a json array")? { let comptype = ComponentType::from_str(tup .get(0).ok_or("index 0 not in component")? .as_str().ok_or("component name not a string")? @@ -196,6 +195,12 @@ impl Template { } components.push((comptype, parameters)); } + Ok(components) + } + + pub fn from_json(val: Value) -> Result{ + let arguments = Self::parse_definition_arguments(val.get("arguments").ok_or("property 'arguments' not found")?)?; + let components = Self::parse_definition_components(val.get("components").ok_or("property 'components' not found")?, &arguments)?; Ok(Template { arguments, components -- cgit