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