diff --git a/src/iq.rs b/src/iq.rs
index 8a4e70e..d1e3eea 100644
--- a/src/iq.rs
+++ b/src/iq.rs
@@ -20,7 +20,7 @@ use stanza_error;
use disco;
use ibb;
use jingle::Jingle;
-use ping;
+use ping::Ping;
/// Lists every known payload of a ``.
#[derive(Debug, Clone)]
@@ -28,7 +28,7 @@ pub enum IqPayload {
Disco(disco::Disco),
IBB(ibb::IBB),
Jingle(Jingle),
- Ping(ping::Ping),
+ Ping(Ping),
}
#[derive(Debug, Clone)]
@@ -101,7 +101,7 @@ pub fn parse_iq(root: &Element) -> Result {
Some(IqPayload::IBB(ibb))
} else if let Ok(jingle) = Jingle::try_from(elem) {
Some(IqPayload::Jingle(jingle))
- } else if let Ok(ping) = ping::parse_ping(elem) {
+ } else if let Ok(ping) = Ping::try_from(elem) {
Some(IqPayload::Ping(ping))
} else {
None
@@ -155,7 +155,7 @@ pub fn serialise_payload(payload: &IqPayload) -> Element {
IqPayload::Disco(ref disco) => disco::serialise_disco(disco),
IqPayload::IBB(ref ibb) => ibb::serialise(ibb),
IqPayload::Jingle(ref jingle) => jingle.into(),
- IqPayload::Ping(_) => ping::serialise_ping(),
+ IqPayload::Ping(ref ping) => ping.into(),
}
}
diff --git a/src/ping.rs b/src/ping.rs
index b6b6f61..4fe2fd8 100644
--- a/src/ping.rs
+++ b/src/ping.rs
@@ -5,6 +5,8 @@
// 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/.
+use std::convert::TryFrom;
+
use minidom::Element;
use error::Error;
@@ -14,37 +16,42 @@ use ns;
#[derive(Debug, Clone)]
pub struct Ping;
-pub fn parse_ping(root: &Element) -> Result {
- if !root.is("ping", ns::PING) {
- return Err(Error::ParseError("This is not a ping element."));
- }
+impl<'a> TryFrom<&'a Element> for Ping {
+ type Error = Error;
- for _ in root.children() {
- return Err(Error::ParseError("Unknown child in ping element."));
+ fn try_from(elem: &'a Element) -> Result {
+ if !elem.is("ping", ns::PING) {
+ return Err(Error::ParseError("This is not a ping element."));
+ }
+ for _ in elem.children() {
+ return Err(Error::ParseError("Unknown child in ping element."));
+ }
+ Ok(Ping)
}
- Ok(Ping { })
}
-pub fn serialise_ping() -> Element {
- Element::builder("ping").ns(ns::PING).build()
+impl<'a> Into for &'a Ping {
+ fn into(self) -> Element {
+ Element::builder("ping")
+ .ns(ns::PING)
+ .build()
+ }
}
#[cfg(test)]
mod tests {
- use minidom::Element;
- use error::Error;
- use ping;
+ use super::*;
#[test]
fn test_simple() {
let elem: Element = "".parse().unwrap();
- ping::parse_ping(&elem).unwrap();
+ Ping::try_from(&elem).unwrap();
}
#[test]
fn test_invalid() {
let elem: Element = "".parse().unwrap();
- let error = ping::parse_ping(&elem).unwrap_err();
+ let error = Ping::try_from(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
@@ -56,7 +63,7 @@ mod tests {
#[ignore]
fn test_invalid_attribute() {
let elem: Element = "".parse().unwrap();
- let error = ping::parse_ping(&elem).unwrap_err();
+ let error = Ping::try_from(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),