xmpp_parsers/
websocket.rs1use xso::{AsXml, FromXml};
8
9use jid::BareJid;
10
11use crate::ns;
12
13#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
15#[xml(namespace = ns::WEBSOCKET, name = "open")]
16pub struct Open {
17 #[xml(attribute(default))]
19 pub from: Option<BareJid>,
20
21 #[xml(attribute(default))]
23 pub to: Option<BareJid>,
24
25 #[xml(attribute(default))]
27 pub id: Option<String>,
28
29 #[xml(attribute(default))]
31 pub version: Option<String>,
32
33 #[xml(attribute(default, name = "xml:lang"))]
36 pub xml_lang: Option<String>,
37}
38
39impl Open {
40 pub fn new(to: BareJid) -> Open {
42 Open {
43 from: None,
44 to: Some(to),
45 id: None,
46 version: Some(String::from("1.0")),
47 xml_lang: None,
48 }
49 }
50
51 pub fn with_from(mut self, from: BareJid) -> Open {
54 self.from = Some(from);
55 self
56 }
57
58 pub fn with_id(mut self, id: String) -> Open {
60 self.id = Some(id);
61 self
62 }
63
64 pub fn with_lang(mut self, xml_lang: String) -> Open {
67 self.xml_lang = Some(xml_lang);
68 self
69 }
70
71 pub fn is_version(&self, version: &str) -> bool {
73 match self.version {
74 None => false,
75 Some(ref self_version) => self_version == &String::from(version),
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use minidom::Element;
84
85 #[cfg(target_pointer_width = "32")]
86 #[test]
87 fn test_size() {
88 assert_size!(Open, 68);
89 }
90
91 #[cfg(target_pointer_width = "64")]
92 #[test]
93 fn test_size() {
94 assert_size!(Open, 136);
95 }
96
97 #[test]
98 fn test_simple() {
99 let elem: Element = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>"
100 .parse()
101 .unwrap();
102 let open = Open::try_from(elem).unwrap();
103 assert_eq!(open.from, None);
104 assert_eq!(open.to, None);
105 assert_eq!(open.id, None);
106 assert_eq!(open.version, None);
107 assert_eq!(open.xml_lang, None);
108 }
109}