diff --git a/src/media_element.rs b/src/media_element.rs index 0ef386a4..db91c44f 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 {