From 7f667041d95e63a0769074d98e5d6931f5eeffaf Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 13 Jul 2017 01:47:05 +0200 Subject: [PATCH] more of a ClientEvent api --- examples/echo_bot.rs | 41 +++++++++++++++++++---------------------- src/client/event.rs | 31 +++++++++++++++++++++++++++++++ src/client/mod.rs | 9 ++------- 3 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 src/client/event.rs diff --git a/examples/echo_bot.rs b/examples/echo_bot.rs index 4af8e8e..f5923f0 100644 --- a/examples/echo_bot.rs +++ b/examples/echo_bot.rs @@ -6,7 +6,7 @@ extern crate xml; use tokio_core::reactor::Core; use futures::{Future, Stream, Sink, future}; -use tokio_xmpp::{Client, ClientEvent}; +use tokio_xmpp::Client; fn main() { let mut core = Core::new().unwrap(); @@ -24,8 +24,8 @@ fn main() { ); }; let done = stream.for_each(|event| { - let result: Box> = match event { - ClientEvent::Online => { + let result: Box> = + if event.is_online() { println!("Online!"); let presence = make_presence(); @@ -33,28 +33,25 @@ fn main() { Box::new( future::ok(()) ) - }, - ClientEvent::Stanza(ref stanza) - if stanza.name == "message" - && stanza.get_attribute("type", None) != Some("error") => - { - let from = stanza.get_attribute("from", None); - let body = stanza.get_child("body", Some("jabber:client")) - .map(|el| el.content_str()); + } else if let Some(stanza) = event.as_stanza() { + if stanza.name == "message" && + stanza.get_attribute("type", None) != Some("error") { + let from = stanza.get_attribute("from", None); + let body = stanza.get_child("body", Some("jabber:client")) + .map(|el| el.content_str()); - match (from.as_ref(), body) { - (Some(from), Some(body)) => { - let reply = make_reply(from, body); - send(reply); - }, - _ => (), - }; + match (from.as_ref(), body) { + (Some(from), Some(body)) => { + let reply = make_reply(from, body); + send(reply); + }, + _ => (), + }; + } Box::new(future::ok(())) - }, - _ => { + } else { Box::new(future::ok(())) - }, - }; + }; result }); diff --git a/src/client/event.rs b/src/client/event.rs new file mode 100644 index 0000000..e54cff4 --- /dev/null +++ b/src/client/event.rs @@ -0,0 +1,31 @@ +use xml; + +#[derive(Debug)] +pub enum Event { + Online, + Disconnected, + Stanza(xml::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<&xml::Element> { + match self { + &Event::Stanza(ref stanza) => Some(stanza), + _ => None, + } + } +} diff --git a/src/client/mod.rs b/src/client/mod.rs index e23b90f..3f962d9 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -19,6 +19,8 @@ mod auth; use self::auth::*; mod bind; use self::bind::*; +mod event; +pub use self::event::Event as ClientEvent; pub struct Client { pub jid: Jid, @@ -99,13 +101,6 @@ impl Client { } } -#[derive(Debug)] -pub enum ClientEvent { - Online, - Disconnected, - Stanza(xml::Element), -} - impl Stream for Client { type Item = ClientEvent; type Error = String;