mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
media_element: Switch to Into/TryFrom.
This commit is contained in:
parent
1ec3806629
commit
0dd0b444b3
2 changed files with 39 additions and 34 deletions
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
@ -11,7 +12,7 @@ use minidom::Element;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use media_element::{MediaElement, parse_media_element};
|
use media_element::MediaElement;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Field {
|
pub struct Field {
|
||||||
|
@ -74,7 +75,7 @@ pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> {
|
||||||
if element.is("value", ns::DATA_FORMS) {
|
if element.is("value", ns::DATA_FORMS) {
|
||||||
values.push(element.text());
|
values.push(element.text());
|
||||||
} else if element.is("media", ns::MEDIA_ELEMENT) {
|
} else if element.is("media", ns::MEDIA_ELEMENT) {
|
||||||
match parse_media_element(element) {
|
match MediaElement::try_from(element) {
|
||||||
Ok(media_element) => media.push(media_element),
|
Ok(media_element) => media.push(media_element),
|
||||||
Err(_) => (), // TODO: is it really nice to swallow this error?
|
Err(_) => (), // TODO: is it really nice to swallow this error?
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
@ -23,15 +25,18 @@ pub struct MediaElement {
|
||||||
pub uris: Vec<URI>,
|
pub uris: Vec<URI>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
|
impl<'a> TryFrom<&'a Element> for MediaElement {
|
||||||
if !root.is("media", ns::MEDIA_ELEMENT) {
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<MediaElement, Error> {
|
||||||
|
if !elem.is("media", ns::MEDIA_ELEMENT) {
|
||||||
return Err(Error::ParseError("This is not a media element."));
|
return Err(Error::ParseError("This is not a media element."));
|
||||||
}
|
}
|
||||||
|
|
||||||
let width = root.attr("width").and_then(|width| width.parse().ok());
|
let width = elem.attr("width").and_then(|width| width.parse().ok());
|
||||||
let height = root.attr("height").and_then(|height| height.parse().ok());
|
let height = elem.attr("height").and_then(|height| height.parse().ok());
|
||||||
let mut uris = vec!();
|
let mut uris = vec!();
|
||||||
for uri in root.children() {
|
for uri in elem.children() {
|
||||||
if uri.is("uri", ns::MEDIA_ELEMENT) {
|
if uri.is("uri", ns::MEDIA_ELEMENT) {
|
||||||
let type_ = uri.attr("type").ok_or(Error::ParseError("Attribute type on uri is mandatory."))?;
|
let type_ = uri.attr("type").ok_or(Error::ParseError("Attribute type on uri is mandatory."))?;
|
||||||
let text = uri.text().trim().to_owned();
|
let text = uri.text().trim().to_owned();
|
||||||
|
@ -45,18 +50,17 @@ pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
|
||||||
}
|
}
|
||||||
Ok(MediaElement { width: width, height: height, uris: uris })
|
Ok(MediaElement { width: width, height: height, uris: uris })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use media_element;
|
|
||||||
use data_forms;
|
use data_forms;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element'/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element'/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.width.is_none());
|
assert!(media.width.is_none());
|
||||||
assert!(media.height.is_none());
|
assert!(media.height.is_none());
|
||||||
assert!(media.uris.is_empty());
|
assert!(media.uris.is_empty());
|
||||||
|
@ -65,7 +69,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_width_height() {
|
fn test_width_height() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element' width='32' height='32'/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element' width='32' height='32'/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert_eq!(media.width.unwrap(), 32);
|
assert_eq!(media.width.unwrap(), 32);
|
||||||
assert_eq!(media.height.unwrap(), 32);
|
assert_eq!(media.height.unwrap(), 32);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +77,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uri() {
|
fn test_uri() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri type='text/html'>https://example.org/</uri></media>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri type='text/html'>https://example.org/</uri></media>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert_eq!(media.uris.len(), 1);
|
assert_eq!(media.uris.len(), 1);
|
||||||
assert_eq!(media.uris[0].type_, "text/html");
|
assert_eq!(media.uris[0].type_, "text/html");
|
||||||
assert_eq!(media.uris[0].uri, "https://example.org/");
|
assert_eq!(media.uris[0].uri, "https://example.org/");
|
||||||
|
@ -82,26 +86,26 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_width_height() {
|
fn test_invalid_width_height() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element' width=''/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element' width=''/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.width.is_none());
|
assert!(media.width.is_none());
|
||||||
|
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element' width='coucou'/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element' width='coucou'/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.width.is_none());
|
assert!(media.width.is_none());
|
||||||
|
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element' height=''/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element' height=''/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.height.is_none());
|
assert!(media.height.is_none());
|
||||||
|
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element' height='-10'/>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element' height='-10'/>".parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.height.is_none());
|
assert!(media.height.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unknown_child() {
|
fn test_unknown_child() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element'><coucou/></media>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element'><coucou/></media>".parse().unwrap();
|
||||||
let error = media_element::parse_media_element(&elem).unwrap_err();
|
let error = MediaElement::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -112,7 +116,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_uri() {
|
fn test_bad_uri() {
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri>https://example.org/</uri></media>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri>https://example.org/</uri></media>".parse().unwrap();
|
||||||
let error = media_element::parse_media_element(&elem).unwrap_err();
|
let error = MediaElement::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -120,7 +124,7 @@ mod tests {
|
||||||
assert_eq!(message, "Attribute type on uri is mandatory.");
|
assert_eq!(message, "Attribute type on uri is mandatory.");
|
||||||
|
|
||||||
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri type='text/html'/></media>".parse().unwrap();
|
let elem: Element = "<media xmlns='urn:xmpp:media-element'><uri type='text/html'/></media>".parse().unwrap();
|
||||||
let error = media_element::parse_media_element(&elem).unwrap_err();
|
let error = MediaElement::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -142,7 +146,7 @@ mod tests {
|
||||||
http://victim.example.com/challenges/speech.mp3?F3A6292C
|
http://victim.example.com/challenges/speech.mp3?F3A6292C
|
||||||
</uri>
|
</uri>
|
||||||
</media>"#.parse().unwrap();
|
</media>"#.parse().unwrap();
|
||||||
let media = media_element::parse_media_element(&elem).unwrap();
|
let media = MediaElement::try_from(&elem).unwrap();
|
||||||
assert!(media.width.is_none());
|
assert!(media.width.is_none());
|
||||||
assert!(media.height.is_none());
|
assert!(media.height.is_none());
|
||||||
assert_eq!(media.uris.len(), 3);
|
assert_eq!(media.uris.len(), 3);
|
||||||
|
|
Loading…
Reference in a new issue