From 5ece20a029f27bac74eab2e954c8f692f0bf9595 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 29 Jul 2017 04:25:55 +0100 Subject: [PATCH] disco: Create a mutable DiscoInfoResult at the beginning of its parsing. --- src/disco.rs | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/disco.rs b/src/disco.rs index aa816de..1649682 100644 --- a/src/disco.rs +++ b/src/disco.rs @@ -99,16 +99,17 @@ impl TryFrom for DiscoInfoResult { return Err(Error::ParseError("This is not a disco#info element.")); } - let mut identities: Vec = vec!(); - let mut features: Vec = vec!(); - let mut extensions: Vec = vec!(); - - let node = get_attr!(elem, "node", optional); + let mut result = DiscoInfoResult { + node: get_attr!(elem, "node", optional), + identities: vec!(), + features: vec!(), + extensions: vec!(), + }; for child in elem.children() { if child.is("feature", ns::DISCO_INFO) { let feature = get_attr!(child, "var", required); - features.push(Feature { + result.features.push(Feature { var: feature, }); } else if child.is("identity", ns::DISCO_INFO) { @@ -124,7 +125,7 @@ impl TryFrom for DiscoInfoResult { let lang = get_attr!(child, "xml:lang", optional); let name = get_attr!(child, "name", optional); - identities.push(Identity { + result.identities.push(Identity { category: category, type_: type_, lang: lang, @@ -135,31 +136,26 @@ impl TryFrom for DiscoInfoResult { if data_form.type_ != DataFormType::Result_ { return Err(Error::ParseError("Data form must have a 'result' type in disco#info.")); } - match data_form.form_type { - Some(_) => extensions.push(data_form), - None => return Err(Error::ParseError("Data form found without a FORM_TYPE.")), + if data_form.form_type.is_none() { + return Err(Error::ParseError("Data form found without a FORM_TYPE.")); } + result.extensions.push(data_form); } else { return Err(Error::ParseError("Unknown element in disco#info.")); } } - if identities.is_empty() { + if result.identities.is_empty() { return Err(Error::ParseError("There must be at least one identity in disco#info.")); } - if features.is_empty() { + if result.features.is_empty() { return Err(Error::ParseError("There must be at least one feature in disco#info.")); } - if !features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) { + if !result.features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) { return Err(Error::ParseError("disco#info feature not present in disco#info.")); } - Ok(DiscoInfoResult { - node: node, - identities: identities, - features: features, - extensions: extensions - }) + Ok(result) } }