From 4875f15201f687d0a0e22e735b35507fb49d5108 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 31 Oct 2017 22:01:52 +0000 Subject: [PATCH] media_element: Parse URI separately from MediaElement. --- src/media_element.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/media_element.rs b/src/media_element.rs index b2e5857d..478dbb31 100644 --- a/src/media_element.rs +++ b/src/media_element.rs @@ -18,6 +18,24 @@ pub struct URI { pub uri: String, } +impl TryFrom for URI { + type Err = Error; + + fn try_from(elem: Element) -> Result { + check_self!(elem, "uri", ns::MEDIA_ELEMENT); + check_no_unknown_attributes!(elem, "uri", ["type"]); + check_no_children!(elem, "uri"); + let uri = elem.text().trim().to_owned(); + if uri == "" { + return Err(Error::ParseError("URI missing in uri.")); + } + Ok(URI { + type_: get_attr!(elem, "type", required), + uri: uri, + }) + } +} + impl From for Element { fn from(uri: URI) -> Element { Element::builder("uri") @@ -48,14 +66,9 @@ impl TryFrom for MediaElement { height: get_attr!(elem, "height", optional), uris: vec!(), }; - for uri in elem.children() { - if uri.is("uri", ns::MEDIA_ELEMENT) { - let type_ = get_attr!(uri, "type", required); - let text = uri.text().trim().to_owned(); - if text == "" { - return Err(Error::ParseError("URI missing in uri.")); - } - media.uris.push(URI { type_: type_, uri: text }); + 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.")); }