2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// 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/.
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
use try_from::TryFrom;
|
2017-05-06 19:42:12 +00:00
|
|
|
|
2017-04-29 05:50:57 +00:00
|
|
|
use minidom::Element;
|
2017-04-29 02:50:49 +00:00
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
2017-05-06 19:42:12 +00:00
|
|
|
use delay::Delay;
|
2017-05-06 20:21:34 +00:00
|
|
|
use message::Message;
|
2017-04-29 02:50:49 +00:00
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Forwarded {
|
2017-05-06 19:42:12 +00:00
|
|
|
pub delay: Option<Delay>,
|
2017-04-29 02:50:49 +00:00
|
|
|
// XXX: really? Option?
|
2017-05-06 20:21:34 +00:00
|
|
|
pub stanza: Option<Message>,
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
impl TryFrom<Element> for Forwarded {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-05-06 19:53:43 +00:00
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
fn try_from(elem: Element) -> Result<Forwarded, Error> {
|
2017-05-06 19:53:43 +00:00
|
|
|
if !elem.is("forwarded", ns::FORWARD) {
|
|
|
|
return Err(Error::ParseError("This is not a forwarded element."));
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
2017-05-06 19:53:43 +00:00
|
|
|
let mut delay = None;
|
|
|
|
let mut stanza = None;
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("delay", ns::DELAY) {
|
2017-05-23 22:31:33 +00:00
|
|
|
delay = Some(Delay::try_from(child.clone())?);
|
2017-05-06 19:53:43 +00:00
|
|
|
} else if child.is("message", ns::JABBER_CLIENT) {
|
2017-05-23 22:31:33 +00:00
|
|
|
stanza = Some(Message::try_from(child.clone())?);
|
2017-05-06 19:53:43 +00:00
|
|
|
// TODO: also handle the five other possibilities.
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown child in forwarded element."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Forwarded {
|
|
|
|
delay: delay,
|
|
|
|
stanza: stanza,
|
|
|
|
})
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Forwarded> for Element {
|
|
|
|
fn from(forwarded: Forwarded) -> Element {
|
2017-05-06 19:53:43 +00:00
|
|
|
Element::builder("forwarded")
|
|
|
|
.ns(ns::FORWARD)
|
2017-07-20 19:52:25 +00:00
|
|
|
.append(forwarded.delay.map(Element::from))
|
|
|
|
.append(forwarded.stanza.map(Element::from))
|
2017-05-06 19:53:43 +00:00
|
|
|
.build()
|
|
|
|
}
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 19:53:43 +00:00
|
|
|
use super::*;
|
2017-04-29 02:50:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
Forwarded::try_from(elem).unwrap();
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_child() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Forwarded::try_from(elem).unwrap_err();
|
2017-04-29 02:50:49 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown child in forwarded element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
2017-05-06 19:53:43 +00:00
|
|
|
let forwarded = Forwarded { delay: None, stanza: None };
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2 = forwarded.into();
|
2017-04-29 02:50:49 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
}
|