xmlstream: use Stanza for XmppStreamElement
Oftentimes, Stanza is at hand anyway, so this saves some typing.
This commit is contained in:
parent
c6f928d5c8
commit
56ce57ecde
5 changed files with 23 additions and 60 deletions
|
|
@ -7,6 +7,7 @@ use xmpp_parsers::iq::{Iq, IqType};
|
||||||
use xmpp_parsers::stream_features::StreamFeatures;
|
use xmpp_parsers::stream_features::StreamFeatures;
|
||||||
|
|
||||||
use crate::error::{Error, ProtocolError};
|
use crate::error::{Error, ProtocolError};
|
||||||
|
use crate::event::Stanza;
|
||||||
use crate::jid::{FullJid, Jid};
|
use crate::jid::{FullJid, Jid};
|
||||||
use crate::xmlstream::{ReadError, XmppStream, XmppStreamElement};
|
use crate::xmlstream::{ReadError, XmppStream, XmppStreamElement};
|
||||||
|
|
||||||
|
|
@ -22,19 +23,21 @@ pub async fn bind<S: AsyncBufRead + AsyncWrite + Unpin>(
|
||||||
.resource()
|
.resource()
|
||||||
.and_then(|resource| Some(resource.to_string()));
|
.and_then(|resource| Some(resource.to_string()));
|
||||||
let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource));
|
let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource));
|
||||||
stream.send(&XmppStreamElement::Iq(iq)).await?;
|
stream.send(&iq).await?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match stream.next().await {
|
match stream.next().await {
|
||||||
Some(Ok(XmppStreamElement::Iq(iq))) if iq.id == BIND_REQ_ID => match iq.payload {
|
Some(Ok(XmppStreamElement::Stanza(Stanza::Iq(iq)))) if iq.id == BIND_REQ_ID => {
|
||||||
IqType::Result(Some(payload)) => match BindResponse::try_from(payload) {
|
match iq.payload {
|
||||||
Ok(v) => {
|
IqType::Result(Some(payload)) => match BindResponse::try_from(payload) {
|
||||||
return Ok(Some(v.into()));
|
Ok(v) => {
|
||||||
}
|
return Ok(Some(v.into()));
|
||||||
Err(_) => return Err(ProtocolError::InvalidBindResponse.into()),
|
}
|
||||||
},
|
Err(_) => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||||
_ => return Err(ProtocolError::InvalidBindResponse.into()),
|
},
|
||||||
},
|
_ => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(Ok(_)) => {}
|
Some(Ok(_)) => {}
|
||||||
Some(Err(ReadError::SoftTimeout)) => {}
|
Some(Err(ReadError::SoftTimeout)) => {}
|
||||||
Some(Err(ReadError::HardError(e))) => return Err(e.into()),
|
Some(Err(ReadError::HardError(e))) => return Err(e.into()),
|
||||||
|
|
|
||||||
|
|
@ -141,35 +141,14 @@ impl<C: ServerConnector> Stream for Client<C> {
|
||||||
Poll::Ready(Some(Err(ReadError::SoftTimeout))) => {
|
Poll::Ready(Some(Err(ReadError::SoftTimeout))) => {
|
||||||
// TODO: do something smart about this.
|
// TODO: do something smart about this.
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Iq(stanza)))) => {
|
Poll::Ready(Some(Ok(XmppStreamElement::Stanza(stanza)))) => {
|
||||||
// Receive stanza
|
// Receive stanza
|
||||||
self.state = ClientState::Connected {
|
self.state = ClientState::Connected {
|
||||||
stream,
|
stream,
|
||||||
features,
|
features,
|
||||||
bound_jid,
|
bound_jid,
|
||||||
};
|
};
|
||||||
// TODO: use specific stanza types instead of going back to elements...
|
return Poll::Ready(Some(Event::Stanza(stanza)));
|
||||||
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
||||||
}
|
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Message(stanza)))) => {
|
|
||||||
// Receive stanza
|
|
||||||
self.state = ClientState::Connected {
|
|
||||||
stream,
|
|
||||||
features,
|
|
||||||
bound_jid,
|
|
||||||
};
|
|
||||||
// TODO: use specific stanza types instead of going back to elements...
|
|
||||||
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
||||||
}
|
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Presence(stanza)))) => {
|
|
||||||
// Receive stanza
|
|
||||||
self.state = ClientState::Connected {
|
|
||||||
stream,
|
|
||||||
features,
|
|
||||||
bound_jid,
|
|
||||||
};
|
|
||||||
// TODO: use specific stanza types instead of going back to elements...
|
|
||||||
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Ok(_))) => {
|
Poll::Ready(Some(Ok(_))) => {
|
||||||
// We ignore these for now.
|
// We ignore these for now.
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,8 @@ impl<C: ServerConnector> Stream for Component<C> {
|
||||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||||
loop {
|
loop {
|
||||||
match Pin::new(&mut self.stream).poll_next(cx) {
|
match Pin::new(&mut self.stream).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Iq(stanza)))) => {
|
Poll::Ready(Some(Ok(XmppStreamElement::Stanza(stanza)))) => {
|
||||||
return Poll::Ready(Some(Stanza::Iq(stanza)))
|
return Poll::Ready(Some(stanza))
|
||||||
}
|
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Message(stanza)))) => {
|
|
||||||
return Poll::Ready(Some(Stanza::Message(stanza)))
|
|
||||||
}
|
|
||||||
Poll::Ready(Some(Ok(XmppStreamElement::Presence(stanza)))) => {
|
|
||||||
return Poll::Ready(Some(Stanza::Presence(stanza)))
|
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Ok(_))) =>
|
Poll::Ready(Some(Ok(_))) =>
|
||||||
// unexpected
|
// unexpected
|
||||||
|
|
|
||||||
|
|
@ -102,11 +102,7 @@ impl TryFrom<Stanza> for Iq {
|
||||||
|
|
||||||
impl From<Stanza> for XmppStreamElement {
|
impl From<Stanza> for XmppStreamElement {
|
||||||
fn from(other: Stanza) -> Self {
|
fn from(other: Stanza) -> Self {
|
||||||
match other {
|
Self::Stanza(other)
|
||||||
Stanza::Iq(st) => Self::Iq(st),
|
|
||||||
Stanza::Message(st) => Self::Message(st),
|
|
||||||
Stanza::Presence(st) => Self::Presence(st),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,26 +6,17 @@
|
||||||
|
|
||||||
use xso::{AsXml, FromXml};
|
use xso::{AsXml, FromXml};
|
||||||
|
|
||||||
use xmpp_parsers::{
|
use xmpp_parsers::{component, sasl, starttls, stream_error::ReceivedStreamError};
|
||||||
component, iq::Iq, message::Message, presence::Presence, sasl, starttls,
|
|
||||||
stream_error::ReceivedStreamError,
|
use crate::Stanza;
|
||||||
};
|
|
||||||
|
|
||||||
/// Any valid XMPP stream-level element.
|
/// Any valid XMPP stream-level element.
|
||||||
#[derive(FromXml, AsXml, Debug)]
|
#[derive(FromXml, AsXml, Debug)]
|
||||||
#[xml()]
|
#[xml()]
|
||||||
pub enum XmppStreamElement {
|
pub enum XmppStreamElement {
|
||||||
/// IQ stanza
|
/// Stanza
|
||||||
#[xml(transparent)]
|
#[xml(transparent)]
|
||||||
Iq(Iq),
|
Stanza(Stanza),
|
||||||
|
|
||||||
/// Message stanza
|
|
||||||
#[xml(transparent)]
|
|
||||||
Message(Message),
|
|
||||||
|
|
||||||
/// Presence stanza
|
|
||||||
#[xml(transparent)]
|
|
||||||
Presence(Presence),
|
|
||||||
|
|
||||||
/// SASL-related nonza
|
/// SASL-related nonza
|
||||||
#[xml(transparent)]
|
#[xml(transparent)]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue