1// Copyright (c) 2018 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 try_from::TryFrom;
8
9use minidom::Element;
10use jid::Jid;
11use error::Error;
12use ns;
13
14generate_element_with_only_attributes!(Open, "open", ns::WEBSOCKET, [
15 from: Option<Jid> = "from" => optional,
16 to: Option<Jid> = "to" => optional,
17 id: Option<String> = "id" => optional,
18 version: Option<String> = "version" => optional,
19 xml_lang: Option<String> = "xml:lang" => optional,
20]);
21
22impl Open {
23 pub fn new(to: Jid) -> Open {
24 Open {
25 from: None,
26 to: Some(to),
27 id: None,
28 version: Some(String::from("1.0")),
29 xml_lang: None,
30 }
31 }
32
33 pub fn with_from(mut self, from: Jid) -> Open {
34 self.from = Some(from);
35 self
36 }
37
38 pub fn with_id(mut self, id: String) -> Open {
39 self.id = Some(id);
40 self
41 }
42
43 pub fn with_lang(mut self, xml_lang: String) -> Open {
44 self.xml_lang = Some(xml_lang);
45 self
46 }
47
48 pub fn is_version(&self, version: &str) -> bool {
49 match self.version {
50 None => false,
51 Some(ref self_version) => self_version == &String::from(version),
52 }
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_simple() {
62 let elem: Element = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>".parse().unwrap();
63 let open = Open::try_from(elem).unwrap();
64 assert_eq!(open.from, None);
65 assert_eq!(open.to, None);
66 assert_eq!(open.id, None);
67 assert_eq!(open.version, None);
68 assert_eq!(open.xml_lang, None);
69 }
70}