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 crate::error::{Error, ProtocolError};
|
||||
use crate::event::Stanza;
|
||||
use crate::jid::{FullJid, Jid};
|
||||
use crate::xmlstream::{ReadError, XmppStream, XmppStreamElement};
|
||||
|
||||
|
|
@ -22,19 +23,21 @@ pub async fn bind<S: AsyncBufRead + AsyncWrite + Unpin>(
|
|||
.resource()
|
||||
.and_then(|resource| Some(resource.to_string()));
|
||||
let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource));
|
||||
stream.send(&XmppStreamElement::Iq(iq)).await?;
|
||||
stream.send(&iq).await?;
|
||||
|
||||
loop {
|
||||
match stream.next().await {
|
||||
Some(Ok(XmppStreamElement::Iq(iq))) if iq.id == BIND_REQ_ID => match iq.payload {
|
||||
IqType::Result(Some(payload)) => match BindResponse::try_from(payload) {
|
||||
Ok(v) => {
|
||||
return Ok(Some(v.into()));
|
||||
}
|
||||
Err(_) => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||
},
|
||||
_ => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||
},
|
||||
Some(Ok(XmppStreamElement::Stanza(Stanza::Iq(iq)))) if iq.id == BIND_REQ_ID => {
|
||||
match iq.payload {
|
||||
IqType::Result(Some(payload)) => match BindResponse::try_from(payload) {
|
||||
Ok(v) => {
|
||||
return Ok(Some(v.into()));
|
||||
}
|
||||
Err(_) => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||
},
|
||||
_ => return Err(ProtocolError::InvalidBindResponse.into()),
|
||||
}
|
||||
}
|
||||
Some(Ok(_)) => {}
|
||||
Some(Err(ReadError::SoftTimeout)) => {}
|
||||
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))) => {
|
||||
// TODO: do something smart about this.
|
||||
}
|
||||
Poll::Ready(Some(Ok(XmppStreamElement::Iq(stanza)))) => {
|
||||
Poll::Ready(Some(Ok(XmppStreamElement::Stanza(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::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())));
|
||||
return Poll::Ready(Some(Event::Stanza(stanza)));
|
||||
}
|
||||
Poll::Ready(Some(Ok(_))) => {
|
||||
// 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>> {
|
||||
loop {
|
||||
match Pin::new(&mut self.stream).poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(XmppStreamElement::Iq(stanza)))) => {
|
||||
return Poll::Ready(Some(Stanza::Iq(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(XmppStreamElement::Stanza(stanza)))) => {
|
||||
return Poll::Ready(Some(stanza))
|
||||
}
|
||||
Poll::Ready(Some(Ok(_))) =>
|
||||
// unexpected
|
||||
|
|
|
|||
|
|
@ -102,11 +102,7 @@ impl TryFrom<Stanza> for Iq {
|
|||
|
||||
impl From<Stanza> for XmppStreamElement {
|
||||
fn from(other: Stanza) -> Self {
|
||||
match other {
|
||||
Stanza::Iq(st) => Self::Iq(st),
|
||||
Stanza::Message(st) => Self::Message(st),
|
||||
Stanza::Presence(st) => Self::Presence(st),
|
||||
}
|
||||
Self::Stanza(other)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,26 +6,17 @@
|
|||
|
||||
use xso::{AsXml, FromXml};
|
||||
|
||||
use xmpp_parsers::{
|
||||
component, iq::Iq, message::Message, presence::Presence, sasl, starttls,
|
||||
stream_error::ReceivedStreamError,
|
||||
};
|
||||
use xmpp_parsers::{component, sasl, starttls, stream_error::ReceivedStreamError};
|
||||
|
||||
use crate::Stanza;
|
||||
|
||||
/// Any valid XMPP stream-level element.
|
||||
#[derive(FromXml, AsXml, Debug)]
|
||||
#[xml()]
|
||||
pub enum XmppStreamElement {
|
||||
/// IQ stanza
|
||||
/// Stanza
|
||||
#[xml(transparent)]
|
||||
Iq(Iq),
|
||||
|
||||
/// Message stanza
|
||||
#[xml(transparent)]
|
||||
Message(Message),
|
||||
|
||||
/// Presence stanza
|
||||
#[xml(transparent)]
|
||||
Presence(Presence),
|
||||
Stanza(Stanza),
|
||||
|
||||
/// SASL-related nonza
|
||||
#[xml(transparent)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue