mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
6ef8dbefa3
This is a large change and as such, it needs good motivation. Let me remind you of the ultimate goal: we want a derive macro which allows us to FromXml/IntoXml, and that derive macro should be usable from `xmpp_parsers` and other crates. For that, any code generated by the derive macro mustn't depend on any code in the `xmpp_parsers` crate, because you cannot name the crate you are in portably (`xmpp_parsers::..` wouldn't resolve within `xmpp_parsers`, and `crate::..` would point at other crates if the macro was used in other crates). We also want to interoperate with code already implementing `TryFrom<Element>` and `Into<Element>` on structs. This ultimately requires that we have an error type which is shared by the two implementations and that error type must be declared in the `xso` crate to be usable by the macros. Thus, we port the error type over to use the type declared in `xso`. This changes the structure of the error type greatly; I do not think that `xso` should have to know about all the different types we are parsing there and they don't deserve special treatment. Wrapping them in a `Box<dyn ..>` seems more appropriate.
203 lines
6.3 KiB
Rust
203 lines
6.3 KiB
Rust
// 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 crate::iq::{IqResultPayload, IqSetPayload};
|
||
use crate::ns;
|
||
use crate::Element;
|
||
use jid::{FullJid, Jid};
|
||
use std::str::FromStr;
|
||
use xso::error::{Error, FromElementError};
|
||
|
||
/// The request for resource binding, which is the process by which a client
|
||
/// can obtain a full JID and start exchanging on the XMPP network.
|
||
///
|
||
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
||
#[derive(Debug, Clone, PartialEq)]
|
||
pub struct BindQuery {
|
||
/// Requests this resource, the server may associate another one though.
|
||
///
|
||
/// If this is None, we request no particular resource, and a random one
|
||
/// will be affected by the server.
|
||
resource: Option<String>,
|
||
}
|
||
|
||
impl BindQuery {
|
||
/// Creates a resource binding request.
|
||
pub fn new(resource: Option<String>) -> BindQuery {
|
||
BindQuery { resource }
|
||
}
|
||
}
|
||
|
||
impl IqSetPayload for BindQuery {}
|
||
|
||
impl TryFrom<Element> for BindQuery {
|
||
type Error = FromElementError;
|
||
|
||
fn try_from(elem: Element) -> Result<BindQuery, FromElementError> {
|
||
check_self!(elem, "bind", BIND);
|
||
check_no_attributes!(elem, "bind");
|
||
|
||
let mut resource = None;
|
||
for child in elem.children() {
|
||
if resource.is_some() {
|
||
return Err(Error::Other("Bind can only have one child.").into());
|
||
}
|
||
if child.is("resource", ns::BIND) {
|
||
check_no_attributes!(child, "resource");
|
||
check_no_children!(child, "resource");
|
||
resource = Some(child.text());
|
||
} else {
|
||
return Err(Error::Other("Unknown element in bind request.").into());
|
||
}
|
||
}
|
||
|
||
Ok(BindQuery { resource })
|
||
}
|
||
}
|
||
|
||
impl From<BindQuery> for Element {
|
||
fn from(bind: BindQuery) -> Element {
|
||
Element::builder("bind", ns::BIND)
|
||
.append_all(
|
||
bind.resource
|
||
.map(|resource| Element::builder("resource", ns::BIND).append(resource)),
|
||
)
|
||
.build()
|
||
}
|
||
}
|
||
|
||
/// The response for resource binding, containing the client’s full JID.
|
||
///
|
||
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
||
#[derive(Debug, Clone, PartialEq)]
|
||
pub struct BindResponse {
|
||
/// The full JID returned by the server for this client.
|
||
jid: FullJid,
|
||
}
|
||
|
||
impl IqResultPayload for BindResponse {}
|
||
|
||
impl From<BindResponse> for FullJid {
|
||
fn from(bind: BindResponse) -> FullJid {
|
||
bind.jid
|
||
}
|
||
}
|
||
|
||
impl From<BindResponse> for Jid {
|
||
fn from(bind: BindResponse) -> Jid {
|
||
Jid::from(bind.jid)
|
||
}
|
||
}
|
||
|
||
impl TryFrom<Element> for BindResponse {
|
||
type Error = FromElementError;
|
||
|
||
fn try_from(elem: Element) -> Result<BindResponse, FromElementError> {
|
||
check_self!(elem, "bind", BIND);
|
||
check_no_attributes!(elem, "bind");
|
||
|
||
let mut jid = None;
|
||
for child in elem.children() {
|
||
if jid.is_some() {
|
||
return Err(Error::Other("Bind can only have one child.").into());
|
||
}
|
||
if child.is("jid", ns::BIND) {
|
||
check_no_attributes!(child, "jid");
|
||
check_no_children!(child, "jid");
|
||
jid = Some(FullJid::from_str(&child.text()).map_err(Error::text_parse_error)?);
|
||
} else {
|
||
return Err(Error::Other("Unknown element in bind response.").into());
|
||
}
|
||
}
|
||
|
||
Ok(BindResponse {
|
||
jid: match jid {
|
||
None => {
|
||
return Err(Error::Other("Bind response must contain a jid element.").into())
|
||
}
|
||
Some(jid) => jid,
|
||
},
|
||
})
|
||
}
|
||
}
|
||
|
||
impl From<BindResponse> for Element {
|
||
fn from(bind: BindResponse) -> Element {
|
||
Element::builder("bind", ns::BIND)
|
||
.append(Element::builder("jid", ns::BIND).append(bind.jid))
|
||
.build()
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[cfg(target_pointer_width = "32")]
|
||
#[test]
|
||
fn test_size() {
|
||
assert_size!(BindQuery, 12);
|
||
assert_size!(BindResponse, 16);
|
||
}
|
||
|
||
#[cfg(target_pointer_width = "64")]
|
||
#[test]
|
||
fn test_size() {
|
||
assert_size!(BindQuery, 24);
|
||
assert_size!(BindResponse, 32);
|
||
}
|
||
|
||
#[test]
|
||
fn test_simple() {
|
||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
|
||
.parse()
|
||
.unwrap();
|
||
let bind = BindQuery::try_from(elem).unwrap();
|
||
assert_eq!(bind.resource, None);
|
||
|
||
let elem: Element =
|
||
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Hello™</resource></bind>"
|
||
.parse()
|
||
.unwrap();
|
||
let bind = BindQuery::try_from(elem).unwrap();
|
||
// FIXME: “™” should be resourceprep’d into “TM” here…
|
||
//assert_eq!(bind.resource.unwrap(), "HelloTM");
|
||
assert_eq!(bind.resource.unwrap(), "Hello™");
|
||
|
||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/Hello™</jid></bind>"
|
||
.parse()
|
||
.unwrap();
|
||
let bind = BindResponse::try_from(elem).unwrap();
|
||
assert_eq!(
|
||
bind.jid,
|
||
FullJid::new("coucou@linkmauve.fr/HelloTM").unwrap()
|
||
);
|
||
}
|
||
|
||
#[cfg(not(feature = "disable-validation"))]
|
||
#[test]
|
||
fn test_invalid_resource() {
|
||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
|
||
.parse()
|
||
.unwrap();
|
||
let error = BindQuery::try_from(elem).unwrap_err();
|
||
let message = match error {
|
||
FromElementError::Invalid(Error::Other(string)) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message, "Unknown attribute in resource element.");
|
||
|
||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
|
||
.parse()
|
||
.unwrap();
|
||
let error = BindQuery::try_from(elem).unwrap_err();
|
||
let message = match error {
|
||
FromElementError::Invalid(Error::Other(string)) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message, "Unknown child in resource element.");
|
||
}
|
||
}
|