ping: Port to TryFrom/Into.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-04 01:31:13 +01:00
parent 6c4fd8da01
commit 033cbe777b
2 changed files with 26 additions and 19 deletions

View file

@ -20,7 +20,7 @@ use stanza_error;
use disco; use disco;
use ibb; use ibb;
use jingle::Jingle; use jingle::Jingle;
use ping; use ping::Ping;
/// Lists every known payload of a `<iq/>`. /// Lists every known payload of a `<iq/>`.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -28,7 +28,7 @@ pub enum IqPayload {
Disco(disco::Disco), Disco(disco::Disco),
IBB(ibb::IBB), IBB(ibb::IBB),
Jingle(Jingle), Jingle(Jingle),
Ping(ping::Ping), Ping(Ping),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -101,7 +101,7 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
Some(IqPayload::IBB(ibb)) Some(IqPayload::IBB(ibb))
} else if let Ok(jingle) = Jingle::try_from(elem) { } else if let Ok(jingle) = Jingle::try_from(elem) {
Some(IqPayload::Jingle(jingle)) 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)) Some(IqPayload::Ping(ping))
} else { } else {
None None
@ -155,7 +155,7 @@ pub fn serialise_payload(payload: &IqPayload) -> Element {
IqPayload::Disco(ref disco) => disco::serialise_disco(disco), IqPayload::Disco(ref disco) => disco::serialise_disco(disco),
IqPayload::IBB(ref ibb) => ibb::serialise(ibb), IqPayload::IBB(ref ibb) => ibb::serialise(ibb),
IqPayload::Jingle(ref jingle) => jingle.into(), IqPayload::Jingle(ref jingle) => jingle.into(),
IqPayload::Ping(_) => ping::serialise_ping(), IqPayload::Ping(ref ping) => ping.into(),
} }
} }

View file

@ -5,6 +5,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::convert::TryFrom;
use minidom::Element; use minidom::Element;
use error::Error; use error::Error;
@ -14,37 +16,42 @@ use ns;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Ping; pub struct Ping;
pub fn parse_ping(root: &Element) -> Result<Ping, Error> { impl<'a> TryFrom<&'a Element> for Ping {
if !root.is("ping", ns::PING) { type Error = Error;
return Err(Error::ParseError("This is not a ping element."));
}
for _ in root.children() { fn try_from(elem: &'a Element) -> Result<Ping, Error> {
return Err(Error::ParseError("Unknown child in ping element.")); 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 { impl<'a> Into<Element> for &'a Ping {
Element::builder("ping").ns(ns::PING).build() fn into(self) -> Element {
Element::builder("ping")
.ns(ns::PING)
.build()
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minidom::Element; use super::*;
use error::Error;
use ping;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap(); let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
ping::parse_ping(&elem).unwrap(); Ping::try_from(&elem).unwrap();
} }
#[test] #[test]
fn test_invalid() { fn test_invalid() {
let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap(); let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
let error = ping::parse_ping(&elem).unwrap_err(); let error = Ping::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -56,7 +63,7 @@ mod tests {
#[ignore] #[ignore]
fn test_invalid_attribute() { fn test_invalid_attribute() {
let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap(); let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
let error = ping::parse_ping(&elem).unwrap_err(); let error = Ping::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),