diff --git a/src/forwarding.rs b/src/forwarding.rs index d344145..75b7fbc 100644 --- a/src/forwarding.rs +++ b/src/forwarding.rs @@ -22,52 +22,56 @@ pub struct Forwarded { pub stanza: Option, } -pub fn parse_forwarded(root: &Element) -> Result { - if !root.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() { - if child.is("delay", ns::DELAY) { - delay = Some(Delay::try_from(child)?); - } else if child.is("message", ns::JABBER_CLIENT) { - stanza = Some(message::parse_message(child)?); - // TODO: also handle the five other possibilities. - } else { - return Err(Error::ParseError("Unknown child in forwarded element.")); +impl<'a> TryFrom<&'a Element> for Forwarded { + type Error = Error; + + fn try_from(elem: &'a Element) -> Result { + 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 elem.children() { + if child.is("delay", ns::DELAY) { + delay = Some(Delay::try_from(child)?); + } else if child.is("message", ns::JABBER_CLIENT) { + stanza = Some(message::parse_message(child)?); + // TODO: also handle the five other possibilities. + } else { + return Err(Error::ParseError("Unknown child in forwarded element.")); + } + } + Ok(Forwarded { + delay: delay, + stanza: stanza, + }) } - Ok(Forwarded { - delay: delay, - stanza: stanza, - }) } -pub fn serialise(forwarded: &Forwarded) -> 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()) - .build() +impl<'a> Into for &'a Forwarded { + fn into(self) -> Element { + Element::builder("forwarded") + .ns(ns::FORWARD) + .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 = "".parse().unwrap(); - forwarding::parse_forwarded(&elem).unwrap(); + Forwarded::try_from(&elem).unwrap(); } #[test] fn test_invalid_child() { let elem: Element = "".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 = "".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); } } diff --git a/src/mam.rs b/src/mam.rs index 308cc1c..a826dec 100644 --- a/src/mam.rs +++ b/src/mam.rs @@ -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 { 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 }