event.rs

 1use super::Error;
 2use xmpp_parsers::{Element, Jid};
 3
 4/// High-level event on the Stream implemented by Client and Component
 5#[derive(Debug)]
 6pub enum Event {
 7    /// Stream is connected and initialized
 8    Online {
 9        /// Server-set Jabber-Id for your session
10        ///
11        /// This may turn out to be a different JID resource than
12        /// expected, so use this one instead of the JID with which
13        /// the connection was setup.
14        bound_jid: Jid,
15        /// Was this session resumed?
16        ///
17        /// Not yet implemented for the Client
18        resumed: bool,
19    },
20    /// Stream end
21    Disconnected(Error),
22    /// Received stanza/nonza
23    Stanza(Element),
24}
25
26impl Event {
27    /// `Online` event?
28    pub fn is_online(&self) -> bool {
29        match *self {
30            Event::Online { .. } => true,
31            _ => false,
32        }
33    }
34
35    /// Get the server-assigned JID for the `Online` event
36    pub fn get_jid(&self) -> Option<&Jid> {
37        match *self {
38            Event::Online { ref bound_jid, .. } => Some(bound_jid),
39            _ => None,
40        }
41    }
42
43    /// `Stanza` event?
44    pub fn is_stanza(&self, name: &str) -> bool {
45        match *self {
46            Event::Stanza(ref stanza) => stanza.name() == name,
47            _ => false,
48        }
49    }
50
51    /// If this is a `Stanza` event, get its data
52    pub fn as_stanza(&self) -> Option<&Element> {
53        match *self {
54            Event::Stanza(ref stanza) => Some(stanza),
55            _ => None,
56        }
57    }
58
59    /// If this is a `Stanza` event, unwrap into its data
60    pub fn into_stanza(self) -> Option<Element> {
61        match self {
62            Event::Stanza(stanza) => Some(stanza),
63            _ => None,
64        }
65    }
66}