2019-01-16 13:35:38 +00:00
|
|
|
use xmpp_parsers::Element;
|
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
|
2017-07-12 23:47:05 +00:00
|
|
|
Online,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Stream end
|
2017-07-12 23:47:05 +00:00
|
|
|
Disconnected,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Received stanza/nonza
|
2017-07-17 18:53:00 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-07-17 18:53:00 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 23:07:07 +00:00
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// If this is a `Stanza` event, unwrap into its data
|
2017-07-19 23:07:07 +00:00
|
|
|
pub fn into_stanza(self) -> Option<Element> {
|
|
|
|
match self {
|
|
|
|
Event::Stanza(stanza) => Some(stanza),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 23:47:05 +00:00
|
|
|
}
|