2017-02-20 15:28:51 +00:00
|
|
|
use event::{Event, AbstractEvent};
|
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PluginProxyBinding {
|
|
|
|
sender: Sender<Element>,
|
|
|
|
dispatcher: Sender<AbstractEvent>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginProxyBinding {
|
|
|
|
pub fn new(sender: Sender<Element>, dispatcher: Sender<AbstractEvent>) -> PluginProxyBinding {
|
|
|
|
PluginProxyBinding {
|
|
|
|
sender: sender,
|
|
|
|
dispatcher: dispatcher,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum PluginProxy {
|
|
|
|
Unbound,
|
|
|
|
BoundTo(PluginProxyBinding),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginProxy {
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Returns a new `PluginProxy`.
|
2017-02-20 15:28:51 +00:00
|
|
|
pub fn new() -> PluginProxy {
|
|
|
|
PluginProxy::Unbound
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Binds the `PluginProxy` to a `PluginProxyBinding`.
|
2017-02-20 15:28:51 +00:00
|
|
|
pub fn bind(&mut self, inner: PluginProxyBinding) {
|
|
|
|
if let PluginProxy::BoundTo(_) = *self {
|
|
|
|
panic!("trying to bind an already bound plugin proxy!");
|
|
|
|
}
|
|
|
|
mem::replace(self, PluginProxy::BoundTo(inner));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_binding<R, F: FnOnce(&PluginProxyBinding) -> R>(&self, f: F) -> R {
|
|
|
|
match *self {
|
|
|
|
PluginProxy::Unbound => {
|
|
|
|
panic!("trying to use an unbound plugin proxy!");
|
|
|
|
},
|
|
|
|
PluginProxy::BoundTo(ref binding) => {
|
|
|
|
f(binding)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Dispatches an event.
|
2017-02-20 15:28:51 +00:00
|
|
|
pub fn dispatch<E: Event>(&self, event: E) {
|
|
|
|
self.with_binding(move |binding| {
|
|
|
|
binding.dispatcher.send(AbstractEvent::new(event))
|
|
|
|
.unwrap(); // TODO: may want to return the error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Sends a stanza.
|
2017-02-20 15:28:51 +00:00
|
|
|
pub fn send(&self, elem: Element) {
|
|
|
|
self.with_binding(move |binding| {
|
|
|
|
binding.sender.send(elem).unwrap(); // TODO: as above, may want to return the error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// A plugin handler return value.
|
|
|
|
///
|
|
|
|
/// The `Continue` variant means to do nothing, the `Unload` variant means to unload the plugin.
|
2017-02-20 15:28:51 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum PluginReturn {
|
|
|
|
Continue,
|
|
|
|
Unload,
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// A trait whch all plugins should implement.
|
2017-02-20 15:28:51 +00:00
|
|
|
pub trait Plugin: Any + PluginAny {
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Gets a mutable reference to the inner `PluginProxy`.
|
2017-02-20 15:28:51 +00:00
|
|
|
fn get_proxy(&mut self) -> &mut PluginProxy;
|
|
|
|
|
2017-02-21 16:18:32 +00:00
|
|
|
/// Handles a received stanza.
|
|
|
|
fn handle(&mut self, elem: &Element) -> PluginReturn;
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2017-02-20 15:28:51 +00:00
|
|
|
fn bind(&mut self, inner: PluginProxyBinding) {
|
|
|
|
self.get_proxy().bind(inner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PluginAny {
|
|
|
|
fn as_any(&self) -> &Any;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Any + Sized> PluginAny for T {
|
|
|
|
fn as_any(&self) -> &Any { self }
|
|
|
|
}
|