bind: Split Bind into request/response.
This commit is contained in:
parent
1ded40b614
commit
ecee3e9ee8
1 changed files with 91 additions and 40 deletions
131
src/bind.rs
131
src/bind.rs
|
|
@ -17,76 +17,111 @@ use std::convert::TryFrom;
|
||||||
///
|
///
|
||||||
/// See https://xmpp.org/rfcs/rfc6120.html#bind
|
/// See https://xmpp.org/rfcs/rfc6120.html#bind
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Bind {
|
pub struct BindRequest {
|
||||||
/// Requests no particular resource, a random one will be affected by the
|
|
||||||
/// server.
|
|
||||||
None,
|
|
||||||
|
|
||||||
/// Requests this resource, the server may associate another one though.
|
/// Requests this resource, the server may associate another one though.
|
||||||
Resource(String),
|
///
|
||||||
|
/// If this is None, we request no particular resource, and a random one
|
||||||
/// The full JID returned by the server for this client.
|
/// will be affected by the server.
|
||||||
Jid(FullJid),
|
resource: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bind {
|
impl BindRequest {
|
||||||
/// Creates a resource binding request.
|
/// Creates a resource binding request.
|
||||||
pub fn new(resource: Option<String>) -> Bind {
|
pub fn new(resource: Option<String>) -> BindRequest {
|
||||||
match resource {
|
BindRequest { resource }
|
||||||
None => Bind::None,
|
|
||||||
Some(resource) => Bind::Resource(resource),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IqSetPayload for Bind {}
|
impl IqSetPayload for BindRequest {}
|
||||||
impl IqResultPayload for Bind {}
|
|
||||||
|
|
||||||
impl TryFrom<Element> for Bind {
|
impl TryFrom<Element> for BindRequest {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(elem: Element) -> Result<Bind, Error> {
|
fn try_from(elem: Element) -> Result<BindRequest, Error> {
|
||||||
check_self!(elem, "bind", BIND);
|
check_self!(elem, "bind", BIND);
|
||||||
check_no_attributes!(elem, "bind");
|
check_no_attributes!(elem, "bind");
|
||||||
|
|
||||||
let mut bind = Bind::None;
|
let mut resource = None;
|
||||||
for child in elem.children() {
|
for child in elem.children() {
|
||||||
if bind != Bind::None {
|
if resource.is_some() {
|
||||||
return Err(Error::ParseError("Bind can only have one child."));
|
return Err(Error::ParseError("Bind can only have one child."));
|
||||||
}
|
}
|
||||||
if child.is("resource", ns::BIND) {
|
if child.is("resource", ns::BIND) {
|
||||||
check_no_attributes!(child, "resource");
|
check_no_attributes!(child, "resource");
|
||||||
check_no_children!(child, "resource");
|
check_no_children!(child, "resource");
|
||||||
bind = Bind::Resource(child.text());
|
resource = Some(child.text());
|
||||||
} else if child.is("jid", ns::BIND) {
|
|
||||||
check_no_attributes!(child, "jid");
|
|
||||||
check_no_children!(child, "jid");
|
|
||||||
bind = Bind::Jid(FullJid::from_str(&child.text())?);
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::ParseError("Unknown element in bind."));
|
return Err(Error::ParseError("Unknown element in bind request."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(bind)
|
Ok(BindRequest { resource })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Bind> for Element {
|
impl From<BindRequest> for Element {
|
||||||
fn from(bind: Bind) -> Element {
|
fn from(bind: BindRequest) -> Element {
|
||||||
Element::builder("bind")
|
Element::builder("bind")
|
||||||
.ns(ns::BIND)
|
.ns(ns::BIND)
|
||||||
.append(match bind {
|
.append(match bind.resource {
|
||||||
Bind::None => vec![],
|
None => vec![],
|
||||||
Bind::Resource(resource) => vec![Element::builder("resource")
|
Some(resource) => vec![Element::builder("resource")
|
||||||
.ns(ns::BIND)
|
.ns(ns::BIND)
|
||||||
.append(resource)
|
.append(resource)
|
||||||
.build()],
|
.build()],
|
||||||
Bind::Jid(jid) => vec![Element::builder("jid").ns(ns::BIND).append(jid).build()],
|
|
||||||
})
|
})
|
||||||
.build()
|
.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 TryFrom<Element> for BindResponse {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: Element) -> Result<BindResponse, Error> {
|
||||||
|
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::ParseError("Bind can only have one child."));
|
||||||
|
}
|
||||||
|
if child.is("jid", ns::BIND) {
|
||||||
|
check_no_attributes!(child, "jid");
|
||||||
|
check_no_children!(child, "jid");
|
||||||
|
jid = Some(FullJid::from_str(&child.text())?);
|
||||||
|
} else {
|
||||||
|
return Err(Error::ParseError("Unknown element in bind response."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(BindResponse { jid: match jid {
|
||||||
|
None => return Err(Error::ParseError("Bind response must contain a jid element.")),
|
||||||
|
Some(jid) => jid,
|
||||||
|
} })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<BindResponse> for Element {
|
||||||
|
fn from(bind: BindResponse) -> Element {
|
||||||
|
Element::builder("bind")
|
||||||
|
.ns(ns::BIND)
|
||||||
|
.append(Element::builder("jid").ns(ns::BIND).append(bind.jid).build())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -94,13 +129,15 @@ mod tests {
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(Bind, 40);
|
assert_size!(BindRequest, 12);
|
||||||
|
assert_size!(BindResponse, 36);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "64")]
|
#[cfg(target_pointer_width = "64")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(Bind, 80);
|
assert_size!(BindRequest, 24);
|
||||||
|
assert_size!(BindResponse, 72);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -108,8 +145,22 @@ mod tests {
|
||||||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let bind = Bind::try_from(elem).unwrap();
|
let bind = BindRequest::try_from(elem).unwrap();
|
||||||
assert_eq!(bind, Bind::None);
|
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 = BindRequest::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/HelloTM</jid></bind>"
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
|
let bind = BindResponse::try_from(elem).unwrap();
|
||||||
|
assert_eq!(bind.jid, FullJid::new("coucou", "linkmauve.fr", "HelloTM"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "disable-validation"))]
|
#[cfg(not(feature = "disable-validation"))]
|
||||||
|
|
@ -118,7 +169,7 @@ mod tests {
|
||||||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let error = Bind::try_from(elem).unwrap_err();
|
let error = BindRequest::try_from(elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
@ -128,7 +179,7 @@ mod tests {
|
||||||
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let error = Bind::try_from(elem).unwrap_err();
|
let error = BindRequest::try_from(elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue