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

View file

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