websocket.rs

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