forwarding: Use generate_element!() instead of manually writing the parser.

This commit is contained in:
Emmanuel Gil Peyrot 2018-08-01 00:51:38 +02:00
parent 1248cac39b
commit f7c74498cc

View file

@ -4,61 +4,33 @@
// 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 try_from::TryFrom; #[deny(missing_docs)]
use minidom::Element;
use error::Error;
use delay::Delay; use delay::Delay;
use message::Message; use message::Message;
use ns; generate_element!(
/// Contains a forwarded stanza, either standalone or part of another
/// extension (such as carbons).
Forwarded, "forwarded", FORWARD,
children: [
/// When the stanza originally got sent.
delay: Option<Delay> = ("delay", DELAY) => Delay,
#[derive(Debug, Clone)] // XXX: really? Option?
pub struct Forwarded { /// The stanza being forwarded.
pub delay: Option<Delay>, stanza: Option<Message> = ("message", DEFAULT_NS) => Message
// XXX: really? Option?
pub stanza: Option<Message>,
}
impl TryFrom<Element> for Forwarded { // TODO: also handle the two other stanza possibilities.
type Err = Error; ]
);
fn try_from(elem: Element) -> Result<Forwarded, Error> {
check_self!(elem, "forwarded", FORWARD);
let mut delay = None;
let mut stanza = None;
for child in elem.children() {
if child.is("delay", ns::DELAY) {
delay = Some(Delay::try_from(child.clone())?);
} else if child.is("message", ns::DEFAULT_NS) {
stanza = Some(Message::try_from(child.clone())?);
// TODO: also handle the two other possibilities.
} else {
return Err(Error::ParseError("Unknown child in forwarded element."));
}
}
Ok(Forwarded {
delay: delay,
stanza: stanza,
})
}
}
impl From<Forwarded> for Element {
fn from(forwarded: Forwarded) -> Element {
Element::builder("forwarded")
.ns(ns::FORWARD)
.append(forwarded.delay.map(Element::from))
.append(forwarded.stanza.map(Element::from))
.build()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use try_from::TryFrom;
use minidom::Element;
use error::Error;
#[test] #[test]
fn test_simple() { fn test_simple() {