1use xso::{AsXml, FromXml};
8
9use jid::BareJid;
10
11use crate::ns;
12
13#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
15#[xml(namespace = ns::STREAM, name = "stream")]
16pub struct Stream {
17 #[xml(attribute(default))]
19 pub from: Option<BareJid>,
20
21 #[xml(attribute(default))]
23 to: Option<BareJid>,
24
25 #[xml(attribute(default))]
27 id: Option<String>,
28
29 #[xml(attribute(default))]
31 version: Option<String>,
32
33 #[xml(attribute(default, name = "xml:lang"))]
36 xml_lang: Option<String>,
37}
38
39impl Stream {
40 pub fn new(to: BareJid) -> Stream {
42 Stream {
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) -> Stream {
54 self.from = Some(from);
55 self
56 }
57
58 pub fn with_id(mut self, id: String) -> Stream {
61 self.id = Some(id);
62 self
63 }
64
65 pub fn with_lang(mut self, xml_lang: String) -> Stream {
68 self.xml_lang = Some(xml_lang);
69 self
70 }
71
72 pub fn is_version(&self, version: &str) -> bool {
74 match self.version {
75 None => false,
76 Some(ref self_version) => self_version == &String::from(version),
77 }
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use minidom::Element;
85
86 #[cfg(target_pointer_width = "32")]
87 #[test]
88 fn test_size() {
89 assert_size!(Stream, 68);
90 }
91
92 #[cfg(target_pointer_width = "64")]
93 #[test]
94 fn test_size() {
95 assert_size!(Stream, 136);
96 }
97
98 #[test]
99 fn test_simple() {
100 let elem: Element = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' version='1.0' id='abc' from='some-server.example'/>".parse().unwrap();
101 let stream = Stream::try_from(elem).unwrap();
102 assert_eq!(
103 stream.from,
104 Some(BareJid::new("some-server.example").unwrap())
105 );
106 assert_eq!(stream.to, None);
107 assert_eq!(stream.id, Some(String::from("abc")));
108 assert_eq!(stream.version, Some(String::from("1.0")));
109 assert_eq!(stream.xml_lang, Some(String::from("en")));
110 }
111}