2018-03-01 09:08:30 +00:00
|
|
|
// 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/.
|
|
|
|
|
2019-06-10 21:17:09 +00:00
|
|
|
use jid::BareJid;
|
2018-03-01 09:08:30 +00:00
|
|
|
|
2018-08-08 17:47:06 +00:00
|
|
|
generate_element!(
|
|
|
|
/// The stream opening for client-server communications.
|
|
|
|
Stream, "stream", STREAM,
|
|
|
|
attributes: [
|
|
|
|
/// The JID of the entity opening this stream.
|
2019-06-10 21:17:09 +00:00
|
|
|
from: Option<BareJid> = "from",
|
2018-08-08 17:47:06 +00:00
|
|
|
|
|
|
|
/// The JID of the entity receiving this stream opening.
|
2019-06-10 21:17:09 +00:00
|
|
|
to: Option<BareJid> = "to",
|
2018-08-08 17:47:06 +00:00
|
|
|
|
|
|
|
/// The id of the stream, used for authentication challenges.
|
2019-02-24 19:26:40 +00:00
|
|
|
id: Option<String> = "id",
|
2018-08-08 17:47:06 +00:00
|
|
|
|
|
|
|
/// The XMPP version used during this stream.
|
2019-02-24 19:26:40 +00:00
|
|
|
version: Option<String> = "version",
|
2018-08-08 17:47:06 +00:00
|
|
|
|
|
|
|
/// The default human language for all subsequent stanzas, which will
|
|
|
|
/// be transmitted to other entities for better localisation.
|
2019-02-24 19:26:40 +00:00
|
|
|
xml_lang: Option<String> = "xml:lang",
|
2018-08-08 17:47:06 +00:00
|
|
|
]
|
|
|
|
);
|
2018-03-01 09:08:30 +00:00
|
|
|
|
|
|
|
impl Stream {
|
2018-08-08 17:47:06 +00:00
|
|
|
/// Creates a simple client→server `<stream:stream>` element.
|
2019-06-10 21:17:09 +00:00
|
|
|
pub fn new(to: BareJid) -> Stream {
|
2018-03-01 09:08:30 +00:00
|
|
|
Stream {
|
|
|
|
from: None,
|
|
|
|
to: Some(to),
|
|
|
|
id: None,
|
|
|
|
version: Some(String::from("1.0")),
|
|
|
|
xml_lang: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:47:06 +00:00
|
|
|
/// Sets the [@from](#structfield.from) attribute on this `<stream:stream>`
|
|
|
|
/// element.
|
2019-06-10 21:17:09 +00:00
|
|
|
pub fn with_from(mut self, from: BareJid) -> Stream {
|
2018-03-01 09:08:30 +00:00
|
|
|
self.from = Some(from);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:47:06 +00:00
|
|
|
/// Sets the [@id](#structfield.id) attribute on this `<stream:stream>`
|
|
|
|
/// element.
|
2018-03-01 09:08:30 +00:00
|
|
|
pub fn with_id(mut self, id: String) -> Stream {
|
|
|
|
self.id = Some(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:47:06 +00:00
|
|
|
/// Sets the [@xml:lang](#structfield.xml_lang) attribute on this
|
|
|
|
/// `<stream:stream>` element.
|
2018-03-01 09:08:30 +00:00
|
|
|
pub fn with_lang(mut self, xml_lang: String) -> Stream {
|
|
|
|
self.xml_lang = Some(xml_lang);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:47:06 +00:00
|
|
|
/// Checks whether the version matches the expected one.
|
2018-03-01 09:08:30 +00:00
|
|
|
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::*;
|
2019-09-25 08:28:44 +00:00
|
|
|
use crate::Element;
|
2019-04-12 08:58:42 +00:00
|
|
|
use std::convert::TryFrom;
|
2018-03-01 09:08:30 +00:00
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2019-04-22 12:34:25 +00:00
|
|
|
assert_size!(Stream, 84);
|
2018-10-28 12:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2023-06-20 12:08:58 +00:00
|
|
|
assert_size!(Stream, 136);
|
2018-10-26 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 09:08:30 +00:00
|
|
|
#[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();
|
2023-06-20 12:07:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
stream.from,
|
|
|
|
Some(BareJid::new("some-server.example").unwrap())
|
|
|
|
);
|
2018-03-01 09:08:30 +00:00
|
|
|
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")));
|
|
|
|
}
|
|
|
|
}
|