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