From 988b6a61601d27bf3fe4bf3b9847647be30e35d4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 23 Nov 2017 16:33:08 +0000 Subject: [PATCH] jingle_ft: Parse using the new helper. --- src/jingle_ft.rs | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/src/jingle_ft.rs b/src/jingle_ft.rs index 3339e015..53730a32 100644 --- a/src/jingle_ft.rs +++ b/src/jingle_ft.rs @@ -18,41 +18,17 @@ use minidom::{Element, IntoAttributeValue}; use error::Error; use ns; -#[derive(Debug, Clone, PartialEq)] -pub struct Range { - pub offset: u64, - pub length: Option, - pub hashes: Vec, -} - -impl TryFrom for Range { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "range", ns::JINGLE_FT); - check_no_unknown_attributes!(elem, "range", ["offset", "length"]); - let mut hashes = vec!(); - for child in elem.children() { - hashes.push(Hash::try_from(child.clone())?); - } - Ok(Range { - offset: get_attr!(elem, "offset", default), - length: get_attr!(elem, "length", optional), - hashes: hashes, - }) - } -} - -impl From for Element { - fn from(range: Range) -> Element { - Element::builder("range") - .ns(ns::JINGLE_FT) - .attr("offset", if range.offset == 0 { None } else { Some(range.offset) }) - .attr("length", range.length) - .append(range.hashes) - .build() - } -} +generate_element_with_children!( + #[derive(PartialEq)] + Range, "range", ns::JINGLE_FT, + attributes: [ + offset: u64 = "offset" => default, + length: Option = "length" => optional + ], + children: [ + hashes: Vec = ("hash", ns::HASHES) => Hash + ] +); type Lang = String;