1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use minidom::Element;
8
9use error::Error;
10
11use ns;
12
13#[derive(Debug, Clone)]
14pub enum ChatState {
15 Active,
16 Composing,
17 Gone,
18 Inactive,
19 Paused,
20}
21
22pub fn parse_chatstate(root: &Element) -> Result<ChatState, Error> {
23 for _ in root.children() {
24 return Err(Error::ParseError("Unknown child in chatstate element."));
25 }
26 if root.is("active", ns::CHATSTATES) {
27 Ok(ChatState::Active)
28 } else if root.is("composing", ns::CHATSTATES) {
29 Ok(ChatState::Composing)
30 } else if root.is("gone", ns::CHATSTATES) {
31 Ok(ChatState::Gone)
32 } else if root.is("inactive", ns::CHATSTATES) {
33 Ok(ChatState::Inactive)
34 } else if root.is("paused", ns::CHATSTATES) {
35 Ok(ChatState::Paused)
36 } else {
37 Err(Error::ParseError("This is not a chatstate element."))
38 }
39}
40
41pub fn serialise(chatstate: &ChatState) -> Element {
42 Element::builder(match *chatstate {
43 ChatState::Active => "active",
44 ChatState::Composing => "composing",
45 ChatState::Gone => "gone",
46 ChatState::Inactive => "inactive",
47 ChatState::Paused => "paused",
48 }).ns(ns::CHATSTATES)
49 .build()
50}
51
52#[cfg(test)]
53mod tests {
54 use minidom::Element;
55 use error::Error;
56 use chatstates;
57 use ns;
58
59 #[test]
60 fn test_simple() {
61 let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
62 chatstates::parse_chatstate(&elem).unwrap();
63 }
64
65 #[test]
66 fn test_invalid() {
67 let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
68 let error = chatstates::parse_chatstate(&elem).unwrap_err();
69 let message = match error {
70 Error::ParseError(string) => string,
71 _ => panic!(),
72 };
73 assert_eq!(message, "This is not a chatstate element.");
74 }
75
76 #[test]
77 fn test_invalid_child() {
78 let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
79 let error = chatstates::parse_chatstate(&elem).unwrap_err();
80 let message = match error {
81 Error::ParseError(string) => string,
82 _ => panic!(),
83 };
84 assert_eq!(message, "Unknown child in chatstate element.");
85 }
86
87 #[test]
88 #[ignore]
89 fn test_invalid_attribute() {
90 let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
91 let error = chatstates::parse_chatstate(&elem).unwrap_err();
92 let message = match error {
93 Error::ParseError(string) => string,
94 _ => panic!(),
95 };
96 assert_eq!(message, "Unknown attribute in chatstate element.");
97 }
98
99 #[test]
100 fn test_serialise() {
101 let chatstate = chatstates::ChatState::Active;
102 let elem = chatstates::serialise(&chatstate);
103 assert!(elem.is("active", ns::CHATSTATES));
104 }
105}