media_element: Simplify parsing of URI.

This commit is contained in:
Emmanuel Gil Peyrot 2017-11-24 04:46:08 +00:00
parent ef04d2e524
commit 6533395ec3
2 changed files with 23 additions and 33 deletions

View file

@ -25,6 +25,22 @@ impl PlainText {
} }
} }
/// Codec for trimmed plain text content.
pub struct TrimmedPlainText;
impl TrimmedPlainText {
pub fn decode(s: &str) -> Result<String, Error> {
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 /// Codec wrapping base64 encode/decode
pub struct Base64; pub struct Base64;

View file

@ -11,40 +11,14 @@ use minidom::Element;
use error::Error; use error::Error;
use ns; use ns;
use helpers::TrimmedPlainText;
#[derive(Debug, Clone)] generate_element_with_text!(URI, "uri", ns::MEDIA_ELEMENT,
pub struct URI { [
pub type_: String, type_: String = "type" => required
pub uri: String, ],
} uri: TrimmedPlainText<String>
);
impl TryFrom<Element> for URI {
type Err = Error;
fn try_from(elem: Element) -> Result<URI, Error> {
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<URI> for Element {
fn from(uri: URI) -> Element {
Element::builder("uri")
.ns(ns::MEDIA_ELEMENT)
.attr("type", uri.type_)
.append(uri.uri)
.build()
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MediaElement { pub struct MediaElement {