xmpp-rs/parsers/src/websocket.rs

110 lines
3 KiB
Rust
Raw Normal View History

2018-02-20 15:20:45 +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/.
2024-07-09 15:01:42 +00:00
use xso::{AsXml, FromXml};
use jid::BareJid;
2018-02-20 15:20:45 +00:00
use crate::ns;
/// The stream opening for WebSocket.
2024-07-09 15:01:42 +00:00
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::WEBSOCKET, name = "open")]
pub struct Open {
/// The JID of the entity opening this stream.
#[xml(attribute(default))]
pub from: Option<BareJid>,
2018-08-08 17:41:03 +00:00
/// The JID of the entity receiving this stream opening.
#[xml(attribute(default))]
pub to: Option<BareJid>,
2018-08-08 17:41:03 +00:00
/// The id of the stream, used for authentication challenges.
#[xml(attribute(default))]
pub id: Option<String>,
2018-08-08 17:41:03 +00:00
/// The XMPP version used during this stream.
#[xml(attribute(default))]
pub version: Option<String>,
2018-08-08 17:41:03 +00:00
/// The default human language for all subsequent stanzas, which will
/// be transmitted to other entities for better localisation.
#[xml(attribute(default, name = "xml:lang"))]
pub xml_lang: Option<String>,
}
2018-02-20 15:20:45 +00:00
impl Open {
2018-08-08 17:41:03 +00:00
/// Creates a simple client→server `<open/>` element.
pub fn new(to: BareJid) -> Open {
2018-02-20 15:20:45 +00:00
Open {
from: None,
to: Some(to),
id: None,
version: Some(String::from("1.0")),
xml_lang: None,
}
}
2018-08-08 17:41:03 +00:00
/// Sets the [@from](#structfield.from) attribute on this `<open/>`
/// element.
pub fn with_from(mut self, from: BareJid) -> Open {
2018-02-20 15:20:45 +00:00
self.from = Some(from);
self
}
2018-08-08 17:41:03 +00:00
/// Sets the [@id](#structfield.id) attribute on this `<open/>` element.
2018-02-20 15:20:45 +00:00
pub fn with_id(mut self, id: String) -> Open {
self.id = Some(id);
self
}
2018-08-08 17:41:03 +00:00
/// Sets the [@xml:lang](#structfield.xml_lang) attribute on this `<open/>`
/// element.
2018-02-20 15:20:45 +00:00
pub fn with_lang(mut self, xml_lang: String) -> Open {
self.xml_lang = Some(xml_lang);
self
}
2018-08-08 17:41:03 +00:00
/// Checks whether the version matches the expected one.
2018-02-20 15:20:45 +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::*;
use crate::Element;
2018-02-20 15:20:45 +00:00
2018-10-28 12:10:48 +00:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
2023-06-20 12:14:15 +00:00
assert_size!(Open, 68);
2018-10-28 12:10:48 +00:00
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
2023-06-20 12:08:58 +00:00
assert_size!(Open, 136);
}
2018-02-20 15:20:45 +00:00
#[test]
fn test_simple() {
2018-12-18 14:32:05 +00:00
let elem: Element = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>"
.parse()
.unwrap();
2018-02-20 15:20:45 +00:00
let open = Open::try_from(elem).unwrap();
assert_eq!(open.from, None);
assert_eq!(open.to, None);
assert_eq!(open.id, None);
assert_eq!(open.version, None);
assert_eq!(open.xml_lang, None);
}
}