unify Client::Event and Component::Event into Event

This commit is contained in:
Astro 2017-07-23 02:46:47 +02:00
parent c0f3fc4afb
commit 993fdcab8f
5 changed files with 14 additions and 52 deletions

View file

@ -15,13 +15,12 @@ use super::xmpp_codec::Packet;
use super::xmpp_stream; use super::xmpp_stream;
use super::starttls::{NS_XMPP_TLS, StartTlsClient}; use super::starttls::{NS_XMPP_TLS, StartTlsClient};
use super::happy_eyeballs::Connecter; use super::happy_eyeballs::Connecter;
use super::event::Event;
mod auth; mod auth;
use self::auth::ClientAuth; use self::auth::ClientAuth;
mod bind; mod bind;
use self::bind::ClientBind; use self::bind::ClientBind;
mod event;
pub use self::event::Event as ClientEvent;
pub struct Client { pub struct Client {
pub jid: Jid, pub jid: Jid,
@ -112,7 +111,7 @@ impl Client {
} }
impl Stream for Client { impl Stream for Client {
type Item = ClientEvent; type Item = Event;
type Error = String; type Error = String;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
@ -127,7 +126,7 @@ impl Stream for Client {
match connect.poll() { match connect.poll() {
Ok(Async::Ready(stream)) => { Ok(Async::Ready(stream)) => {
self.state = ClientState::Connected(stream); self.state = ClientState::Connected(stream);
Ok(Async::Ready(Some(ClientEvent::Online))) Ok(Async::Ready(Some(Event::Online)))
}, },
Ok(Async::NotReady) => { Ok(Async::NotReady) => {
self.state = ClientState::Connecting(connect); self.state = ClientState::Connecting(connect);
@ -142,11 +141,11 @@ impl Stream for Client {
Ok(Async::Ready(None)) => { Ok(Async::Ready(None)) => {
// EOF // EOF
self.state = ClientState::Disconnected; self.state = ClientState::Disconnected;
Ok(Async::Ready(Some(ClientEvent::Disconnected))) Ok(Async::Ready(Some(Event::Disconnected)))
}, },
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => { Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
self.state = ClientState::Connected(stream); self.state = ClientState::Connected(stream);
Ok(Async::Ready(Some(ClientEvent::Stanza(stanza)))) Ok(Async::Ready(Some(Event::Stanza(stanza))))
}, },
Ok(Async::NotReady) | Ok(Async::NotReady) |
Ok(Async::Ready(_)) => { Ok(Async::Ready(_)) => {

View file

@ -1,38 +0,0 @@
use minidom::Element;
#[derive(Debug)]
pub enum Event {
Online,
Disconnected,
Stanza(Element),
}
impl Event {
pub fn is_online(&self) -> bool {
match *self {
Event::Online => true,
_ => false,
}
}
pub fn is_stanza(&self, name: &str) -> bool {
match *self {
Event::Stanza(ref stanza) => stanza.name() == name,
_ => false,
}
}
pub fn as_stanza(&self) -> Option<&Element> {
match *self {
Event::Stanza(ref stanza) => Some(stanza),
_ => None,
}
}
pub fn into_stanza(self) -> Option<Element> {
match self {
Event::Stanza(stanza) => Some(stanza),
_ => None,
}
}
}

View file

@ -11,11 +11,10 @@ use jid::{Jid, JidParseError};
use super::xmpp_codec::Packet; use super::xmpp_codec::Packet;
use super::xmpp_stream; use super::xmpp_stream;
use super::happy_eyeballs::Connecter; use super::happy_eyeballs::Connecter;
use super::event::Event;
mod auth; mod auth;
use self::auth::ComponentAuth; use self::auth::ComponentAuth;
mod event;
pub use self::event::Event as ComponentEvent;
pub struct Component { pub struct Component {
pub jid: Jid, pub jid: Jid,
@ -67,7 +66,7 @@ impl Component {
} }
impl Stream for Component { impl Stream for Component {
type Item = ComponentEvent; type Item = Event;
type Error = String; type Error = String;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
@ -82,7 +81,7 @@ impl Stream for Component {
match connect.poll() { match connect.poll() {
Ok(Async::Ready(stream)) => { Ok(Async::Ready(stream)) => {
self.state = ComponentState::Connected(stream); self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(ComponentEvent::Online))) Ok(Async::Ready(Some(Event::Online)))
}, },
Ok(Async::NotReady) => { Ok(Async::NotReady) => {
self.state = ComponentState::Connecting(connect); self.state = ComponentState::Connecting(connect);
@ -101,11 +100,11 @@ impl Stream for Component {
Ok(Async::Ready(None)) => { Ok(Async::Ready(None)) => {
// EOF // EOF
self.state = ComponentState::Disconnected; self.state = ComponentState::Disconnected;
Ok(Async::Ready(Some(ComponentEvent::Disconnected))) Ok(Async::Ready(Some(Event::Disconnected)))
}, },
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => { Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
self.state = ComponentState::Connected(stream); self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(ComponentEvent::Stanza(stanza)))) Ok(Async::Ready(Some(Event::Stanza(stanza))))
}, },
Ok(Async::Ready(_)) => { Ok(Async::Ready(_)) => {
self.state = ComponentState::Connected(stream); self.state = ComponentState::Connected(stream);

View file

@ -20,7 +20,9 @@ mod stream_start;
mod starttls; mod starttls;
pub use starttls::StartTlsClient; pub use starttls::StartTlsClient;
mod happy_eyeballs; mod happy_eyeballs;
mod event;
pub use event::Event;
mod client; mod client;
pub use client::{Client, ClientEvent}; pub use client::Client;
mod component; mod component;
pub use component::{Component, ComponentEvent}; pub use component::Component;