forwarding: Switch to Into/TryFrom.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-06 20:53:43 +01:00
parent 8673b8f90e
commit 2f05d02d23
2 changed files with 37 additions and 34 deletions

View file

@ -22,13 +22,16 @@ pub struct Forwarded {
pub stanza: Option<message::Message>,
}
pub fn parse_forwarded(root: &Element) -> Result<Forwarded, Error> {
if !root.is("forwarded", ns::FORWARD) {
impl<'a> TryFrom<&'a Element> for Forwarded {
type Error = Error;
fn try_from(elem: &'a Element) -> Result<Forwarded, Error> {
if !elem.is("forwarded", ns::FORWARD) {
return Err(Error::ParseError("This is not a forwarded element."));
}
let mut delay = None;
let mut stanza = None;
for child in root.children() {
for child in elem.children() {
if child.is("delay", ns::DELAY) {
delay = Some(Delay::try_from(child)?);
} else if child.is("message", ns::JABBER_CLIENT) {
@ -43,31 +46,32 @@ pub fn parse_forwarded(root: &Element) -> Result<Forwarded, Error> {
stanza: stanza,
})
}
}
pub fn serialise(forwarded: &Forwarded) -> Element {
impl<'a> Into<Element> for &'a Forwarded {
fn into(self) -> Element {
Element::builder("forwarded")
.ns(ns::FORWARD)
.append(match forwarded.delay { Some(ref delay) => { let elem: Element = delay.into(); Some(elem) }, None => None })
.append(forwarded.stanza.clone())
.append(match self.delay { Some(ref delay) => { let elem: Element = delay.into(); Some(elem) }, None => None })
.append(self.stanza.clone())
.build()
}
}
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
use forwarding;
use super::*;
#[test]
fn test_simple() {
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
forwarding::parse_forwarded(&elem).unwrap();
Forwarded::try_from(&elem).unwrap();
}
#[test]
fn test_invalid_child() {
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>".parse().unwrap();
let error = forwarding::parse_forwarded(&elem).unwrap_err();
let error = Forwarded::try_from(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
@ -78,8 +82,8 @@ mod tests {
#[test]
fn test_serialise() {
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
let forwarded = forwarding::Forwarded { delay: None, stanza: None };
let elem2 = forwarding::serialise(&forwarded);
let forwarded = Forwarded { delay: None, stanza: None };
let elem2 = (&forwarded).into();
assert_eq!(elem, elem2);
}
}

View file

@ -13,7 +13,6 @@ use error::Error;
use data_forms::DataForm;
use rsm::Set;
use forwarding;
use forwarding::Forwarded;
use ns;
@ -86,7 +85,7 @@ pub fn parse_result(root: &Element) -> Result<Result_, Error> {
let mut forwarded = None;
for child in root.children() {
if child.is("forwarded", ns::FORWARD) {
forwarded = Some(forwarding::parse_forwarded(child)?);
forwarded = Some(Forwarded::try_from(child)?);
} else {
return Err(Error::ParseError("Unknown child in result element."));
}
@ -190,7 +189,7 @@ pub fn serialise_result(result: &Result_) -> Element {
.attr("queryid", result.queryid.clone())
.attr("id", result.id.clone())
.build();
elem.append_child(forwarding::serialise(&result.forwarded));
elem.append_child((&result.forwarded).into());
elem
}