From 80a2f425e2fbab76d97619e9d89ec18777f0571c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 24 Nov 2017 04:48:31 +0000 Subject: [PATCH] media_element: Simplify parsing of MediaElement. --- src/media_element.rs | 49 ++++++++------------------------------------ 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/src/media_element.rs b/src/media_element.rs index 0ef386a..db91c44 100644 --- a/src/media_element.rs +++ b/src/media_element.rs @@ -20,46 +20,15 @@ generate_element_with_text!(URI, "uri", ns::MEDIA_ELEMENT, uri: TrimmedPlainText ); -#[derive(Debug, Clone)] -pub struct MediaElement { - pub width: Option, - pub height: Option, - pub uris: Vec, -} - -impl TryFrom for MediaElement { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "media", ns::MEDIA_ELEMENT); - check_no_unknown_attributes!(elem, "media", ["width", "height"]); - - let mut media = MediaElement { - width: get_attr!(elem, "width", optional), - height: get_attr!(elem, "height", optional), - uris: vec!(), - }; - for child in elem.children() { - if child.is("uri", ns::MEDIA_ELEMENT) { - media.uris.push(URI::try_from(child.clone())?); - } else { - return Err(Error::ParseError("Unknown child in media element.")); - } - } - Ok(media) - } -} - -impl From for Element { - fn from(media: MediaElement) -> Element { - Element::builder("media") - .ns(ns::MEDIA_ELEMENT) - .attr("width", media.width) - .attr("height", media.height) - .append(media.uris) - .build() - } -} +generate_element_with_children!(MediaElement, "media", ns::MEDIA_ELEMENT, + attributes: [ + width: Option = "width" => optional, + height: Option = "height" => optional + ], + children: [ + uris: Vec = ("uri", ns::MEDIA_ELEMENT) => URI + ] +); #[cfg(test)] mod tests {