ping: Port to TryFrom/Into.
This commit is contained in:
parent
6c4fd8da01
commit
033cbe777b
2 changed files with 26 additions and 19 deletions
|
@ -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 `<iq/>`.
|
||||
#[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<Iq, Error> {
|
|||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
37
src/ping.rs
37
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<Ping, Error> {
|
||||
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<Ping, Error> {
|
||||
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<Element> 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 = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
|
||||
ping::parse_ping(&elem).unwrap();
|
||||
Ping::try_from(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid() {
|
||||
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 {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
@ -56,7 +63,7 @@ mod tests {
|
|||
#[ignore]
|
||||
fn test_invalid_attribute() {
|
||||
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 {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
Loading…
Reference in a new issue