diff --git a/src/helpers.rs b/src/helpers.rs index a457081..4db2d2c 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -25,6 +25,22 @@ impl PlainText { } } +/// Codec for trimmed plain text content. +pub struct TrimmedPlainText; + +impl TrimmedPlainText { + pub fn decode(s: &str) -> Result { + Ok(match s.trim() { + "" => return Err(Error::ParseError("URI missing in uri.")), + text => text.to_owned(), + }) + } + + pub fn encode(string: &String) -> String { + string.to_owned() + } +} + /// Codec wrapping base64 encode/decode pub struct Base64; diff --git a/src/media_element.rs b/src/media_element.rs index b700eec..0ef386a 100644 --- a/src/media_element.rs +++ b/src/media_element.rs @@ -11,40 +11,14 @@ use minidom::Element; use error::Error; use ns; +use helpers::TrimmedPlainText; -#[derive(Debug, Clone)] -pub struct URI { - pub type_: String, - 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") - .ns(ns::MEDIA_ELEMENT) - .attr("type", uri.type_) - .append(uri.uri) - .build() - } -} +generate_element_with_text!(URI, "uri", ns::MEDIA_ELEMENT, + [ + type_: String = "type" => required + ], + uri: TrimmedPlainText +); #[derive(Debug, Clone)] pub struct MediaElement {