diff --git a/parsers/src/media_element.rs b/parsers/src/media_element.rs index 99dbc016a037c2ff79cff9229b8c96620be4dae6..973d5f49385e0f69acb13c1432ba5301884a2b71 100644 --- a/parsers/src/media_element.rs +++ b/parsers/src/media_element.rs @@ -4,26 +4,28 @@ // 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/. -use crate::util::text_node_codecs::{Codec, Text, Trimmed}; +use xso::{text::EmptyAsError, AsXml, FromXml}; -generate_element!( - /// Represents an URI used in a media element. - URI, "uri", MEDIA_ELEMENT, - attributes: [ - /// The MIME type of the URI referenced. - /// - /// See the [IANA MIME Media Types Registry][1] for a list of - /// registered types, but unregistered or yet-to-be-registered are - /// accepted too. - /// - /// [1]: - type_: Required = "type" - ], - text: ( - /// The actual URI contained. - uri: Trimmed - ) -); +use crate::ns; + +/// Represents an URI used in a media element. +#[derive(FromXml, AsXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::MEDIA_ELEMENT, name = "uri")] +pub struct Uri { + /// The MIME type of the URI referenced. + /// + /// See the [IANA MIME Media Types Registry][1] for a list of + /// registered types, but unregistered or yet-to-be-registered are + /// accepted too. + /// + /// [1]: + #[xml(attribute(name = "type"))] + pub type_: String, + + /// The actual URI contained. + #[xml(text(codec = EmptyAsError))] + pub uri: String, +} generate_element!( /// References a media element, to be used in [data @@ -38,7 +40,7 @@ generate_element!( ], children: [ /// A list of URIs referencing this media. - uris: Vec = ("uri", MEDIA_ELEMENT) => URI + uris: Vec = ("uri", MEDIA_ELEMENT) => Uri ] ); @@ -52,14 +54,14 @@ mod tests { #[cfg(target_pointer_width = "32")] #[test] fn test_size() { - assert_size!(URI, 24); + assert_size!(Uri, 24); assert_size!(MediaElement, 28); } #[cfg(target_pointer_width = "64")] #[test] fn test_size() { - assert_size!(URI, 48); + assert_size!(Uri, 48); assert_size!(MediaElement, 56); } @@ -174,7 +176,10 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Required attribute 'type' missing."); + assert_eq!( + message, + "Required attribute field 'type_' on Uri element missing." + ); let elem: Element = "" .parse() @@ -184,24 +189,15 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!( - message, - "The text in the element's text node was empty after trimming." - ); + assert_eq!(message, "Empty text node."); } #[test] fn test_xep_ex1() { let elem: Element = r#" - - http://victim.example.com/challenges/speech.wav?F3A6292C - - - cid:sha1+a15a505e360702b79c75a5f67773072ed392f52a@bob.xmpp.org - - - http://victim.example.com/challenges/speech.mp3?F3A6292C - + http://victim.example.com/challenges/speech.wav?F3A6292C + cid:sha1+a15a505e360702b79c75a5f67773072ed392f52a@bob.xmpp.org + http://victim.example.com/challenges/speech.mp3?F3A6292C "# .parse() .unwrap(); @@ -234,12 +230,8 @@ mod tests { - - http://www.victim.com/challenges/ocr.jpeg?F3A6292C - - - cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org - + http://www.victim.com/challenges/ocr.jpeg?F3A6292C + cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org [ ... ]