xmpp-rs/tokio-xmpp/src/event.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2020-03-05 00:25:24 +00:00
use super::Error;
use xmpp_parsers::{Element, Jid};
2017-07-12 23:47:05 +00:00
2018-08-02 17:58:19 +00:00
/// High-level event on the Stream implemented by Client and Component
2017-07-12 23:47:05 +00:00
#[derive(Debug)]
pub enum Event {
2018-08-02 17:58:19 +00:00
/// Stream is connected and initialized
Online {
/// Server-set Jabber-Id for your session
///
/// This may turn out to be a different JID resource than
/// expected, so use this one instead of the JID with which
/// the connection was setup.
bound_jid: Jid,
/// Was this session resumed?
///
/// Not yet implemented for the Client
resumed: bool,
},
2018-08-02 17:58:19 +00:00
/// Stream end
2020-03-05 00:25:24 +00:00
Disconnected(Error),
2018-08-02 17:58:19 +00:00
/// Received stanza/nonza
Stanza(Element),
2017-07-12 23:47:05 +00:00
}
impl Event {
2018-08-02 17:58:19 +00:00
/// `Online` event?
2017-07-12 23:47:05 +00:00
pub fn is_online(&self) -> bool {
2017-07-20 22:19:08 +00:00
match *self {
Event::Online { .. } => true,
2017-07-12 23:47:05 +00:00
_ => false,
}
}
/// Get the server-assigned JID for the `Online` event
pub fn get_jid(&self) -> Option<&Jid> {
match *self {
Event::Online { ref bound_jid, .. } => Some(bound_jid),
_ => None,
}
}
2018-08-02 17:58:19 +00:00
/// `Stanza` event?
2017-07-12 23:47:05 +00:00
pub fn is_stanza(&self, name: &str) -> bool {
2017-07-20 22:19:08 +00:00
match *self {
Event::Stanza(ref stanza) => stanza.name() == name,
2017-07-12 23:47:05 +00:00
_ => false,
}
}
2018-08-02 17:58:19 +00:00
/// If this is a `Stanza` event, get its data
pub fn as_stanza(&self) -> Option<&Element> {
2017-07-20 22:19:08 +00:00
match *self {
Event::Stanza(ref stanza) => Some(stanza),
2017-07-12 23:47:05 +00:00
_ => None,
}
}
2018-08-02 17:58:19 +00:00
/// If this is a `Stanza` event, unwrap into its data
pub fn into_stanza(self) -> Option<Element> {
match self {
Event::Stanza(stanza) => Some(stanza),
_ => None,
}
}
2017-07-12 23:47:05 +00:00
}