1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{AsXml, FromXml};
use jid::BareJid;
use crate::ns;
/// The stream opening for client-server communications.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::STREAM, name = "stream")]
pub struct Stream {
/// The JID of the entity opening this stream.
#[xml(attribute(default))]
pub from: Option<BareJid>,
/// The JID of the entity receiving this stream opening.
#[xml(attribute(default))]
to: Option<BareJid>,
/// The id of the stream, used for authentication challenges.
#[xml(attribute(default))]
id: Option<String>,
/// The XMPP version used during this stream.
#[xml(attribute(default))]
version: Option<String>,
/// The default human language for all subsequent stanzas, which will
/// be transmitted to other entities for better localisation.
#[xml(lang(default))]
xml_lang: Option<String>,
}
impl Stream {
/// Creates a simple client→server `<stream:stream>` element.
pub fn new(to: BareJid) -> Stream {
Stream {
from: None,
to: Some(to),
id: None,
version: Some(String::from("1.0")),
xml_lang: None,
}
}
/// Sets the [@from](#structfield.from) attribute on this `<stream:stream>`
/// element.
pub fn with_from(mut self, from: BareJid) -> Stream {
self.from = Some(from);
self
}
/// Sets the [@id](#structfield.id) attribute on this `<stream:stream>`
/// element.
pub fn with_id(mut self, id: String) -> Stream {
self.id = Some(id);
self
}
/// Sets the [@xml:lang](#structfield.xml_lang) attribute on this
/// `<stream:stream>` element.
pub fn with_lang(mut self, xml_lang: String) -> Stream {
self.xml_lang = Some(xml_lang);
self
}
/// Checks whether the version matches the expected one.
pub fn is_version(&self, version: &str) -> bool {
match self.version {
None => false,
Some(ref self_version) => self_version == &String::from(version),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Stream, 68);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Stream, 136);
}
#[test]
fn test_simple() {
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();
let stream = Stream::try_from(elem).unwrap();
assert_eq!(
stream.from,
Some(BareJid::new("some-server.example").unwrap())
);
assert_eq!(stream.to, None);
assert_eq!(stream.id, Some(String::from("abc")));
assert_eq!(stream.version, Some(String::from("1.0")));
assert_eq!(stream.xml_lang, Some(String::from("en")));
}
}