1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8use std::convert::TryFrom;
9
10use minidom::Element;
11
12use error::Error;
13
14use ns;
15
16#[derive(Debug, Clone)]
17pub struct Ping;
18
19impl<'a> TryFrom<&'a Element> for Ping {
20 type Error = Error;
21
22 fn try_from(elem: &'a Element) -> Result<Ping, Error> {
23 if !elem.is("ping", ns::PING) {
24 return Err(Error::ParseError("This is not a ping element."));
25 }
26 for _ in elem.children() {
27 return Err(Error::ParseError("Unknown child in ping element."));
28 }
29 Ok(Ping)
30 }
31}
32
33impl<'a> Into<Element> for &'a Ping {
34 fn into(self) -> Element {
35 Element::builder("ping")
36 .ns(ns::PING)
37 .build()
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_simple() {
47 let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
48 Ping::try_from(&elem).unwrap();
49 }
50
51 #[test]
52 fn test_invalid() {
53 let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
54 let error = Ping::try_from(&elem).unwrap_err();
55 let message = match error {
56 Error::ParseError(string) => string,
57 _ => panic!(),
58 };
59 assert_eq!(message, "Unknown child in ping element.");
60 }
61
62 #[test]
63 #[ignore]
64 fn test_invalid_attribute() {
65 let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
66 let error = Ping::try_from(&elem).unwrap_err();
67 let message = match error {
68 Error::ParseError(string) => string,
69 _ => panic!(),
70 };
71 assert_eq!(message, "Unknown attribute in ping element.");
72 }
73}