From 9a5c95694b36c2b48426a3a994b1a0b5681dc504 Mon Sep 17 00:00:00 2001 From: Astro Date: Tue, 15 Oct 2019 22:02:14 +0200 Subject: [PATCH] add jid to Event::Online breaks the API --- examples/echo_bot.rs | 5 ++++- src/client/mod.rs | 3 ++- src/component/mod.rs | 2 +- src/event.rs | 14 +++++++++++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/echo_bot.rs b/examples/echo_bot.rs index 0d08dc96..a4144bf4 100644 --- a/examples/echo_bot.rs +++ b/examples/echo_bot.rs @@ -47,7 +47,10 @@ fn main() { if wait_for_stream_end { /* Do nothing */ } else if event.is_online() { - println!("Online!"); + let jid = event.get_jid() + .map(|jid| format!("{}", jid)) + .unwrap_or("unknown".to_owned()); + println!("Online at {}", jid); let presence = make_presence(); tx.start_send(Packet::Stanza(presence)).unwrap(); diff --git a/src/client/mod.rs b/src/client/mod.rs index fab4571c..bcd07c95 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -145,8 +145,9 @@ impl Stream for Client { ClientState::Disconnected => Ok(Async::Ready(None)), ClientState::Connecting(mut connect) => match connect.poll() { Ok(Async::Ready(stream)) => { + let jid = stream.jid.clone(); self.state = ClientState::Connected(stream); - Ok(Async::Ready(Some(Event::Online))) + Ok(Async::Ready(Some(Event::Online(jid)))) } Ok(Async::NotReady) => { self.state = ClientState::Connecting(connect); diff --git a/src/component/mod.rs b/src/component/mod.rs index 87381117..d822e883 100644 --- a/src/component/mod.rs +++ b/src/component/mod.rs @@ -90,7 +90,7 @@ impl Stream for Component { ComponentState::Connecting(mut connect) => match connect.poll() { Ok(Async::Ready(stream)) => { self.state = ComponentState::Connected(stream); - Ok(Async::Ready(Some(Event::Online))) + Ok(Async::Ready(Some(Event::Online(self.jid.clone())))) } Ok(Async::NotReady) => { self.state = ComponentState::Connecting(connect); diff --git a/src/event.rs b/src/event.rs index 94dd36c2..bd3dc402 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,10 +1,10 @@ -use xmpp_parsers::Element; +use xmpp_parsers::{Element, Jid}; /// High-level event on the Stream implemented by Client and Component #[derive(Debug)] pub enum Event { /// Stream is connected and initialized - Online, + Online(Jid), /// Stream end Disconnected, /// Received stanza/nonza @@ -15,11 +15,19 @@ impl Event { /// `Online` event? pub fn is_online(&self) -> bool { match *self { - Event::Online => true, + Event::Online(_) => true, _ => false, } } + /// Get the server-assigned JID for the `Online` event + pub fn get_jid(&self) -> Option<&Jid> { + match *self { + Event::Online(ref jid) => Some(jid), + _ => None, + } + } + /// `Stanza` event? pub fn is_stanza(&self, name: &str) -> bool { match *self {