2017-02-21 16:38:29 +00:00
|
|
|
//! Provides the plugin infrastructure.
|
|
|
|
|
2017-05-27 14:56:44 +00:00
|
|
|
use event::{Event, Dispatcher, SendElement, Priority, Propagation};
|
2017-02-20 15:28:51 +00:00
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
|
2017-05-27 16:41:54 +00:00
|
|
|
use std::sync::Arc;
|
2017-02-20 15:28:51 +00:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PluginProxyBinding {
|
2017-05-27 16:41:54 +00:00
|
|
|
dispatcher: Arc<Dispatcher>,
|
2017-02-20 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginProxyBinding {
|
2017-05-27 16:41:54 +00:00
|
|
|
pub fn new(dispatcher: Arc<Dispatcher>) -> PluginProxyBinding {
|
2017-02-20 15:28:51 +00:00
|
|
|
PluginProxyBinding {
|
|
|
|
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| {
|
2017-05-09 22:13:54 +00:00
|
|
|
// TODO: proper error handling
|
2017-05-27 16:41:54 +00:00
|
|
|
binding.dispatcher.dispatch(event);
|
2017-02-20 15:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:13:54 +00:00
|
|
|
/// Registers an event handler.
|
2017-05-27 14:56:44 +00:00
|
|
|
pub fn register_handler<E, F>(&self, priority: Priority, func: F)
|
|
|
|
where
|
|
|
|
E: Event,
|
|
|
|
F: Fn(&E) -> Propagation + 'static {
|
2017-02-20 15:28:51 +00:00
|
|
|
self.with_binding(move |binding| {
|
2017-05-09 22:13:54 +00:00
|
|
|
// TODO: proper error handling
|
2017-05-27 16:41:54 +00:00
|
|
|
binding.dispatcher.register(priority, func);
|
2017-02-20 15:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:13:54 +00:00
|
|
|
/// Sends a stanza.
|
|
|
|
pub fn send(&self, elem: Element) {
|
|
|
|
self.dispatch(SendElement(elem));
|
|
|
|
}
|
2017-02-20 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
#[doc(hidden)]
|
2017-02-20 15:28:51 +00:00
|
|
|
fn bind(&mut self, inner: PluginProxyBinding) {
|
|
|
|
self.get_proxy().bind(inner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:13:54 +00:00
|
|
|
pub trait PluginInit {
|
2017-05-27 17:40:00 +00:00
|
|
|
fn init(dispatcher: &Dispatcher, me: Arc<Plugin>);
|
2017-05-09 22:13:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 15:28:51 +00:00
|
|
|
pub trait PluginAny {
|
|
|
|
fn as_any(&self) -> &Any;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:13:54 +00:00
|
|
|
impl<T: Any + Sized + Plugin> PluginAny for T {
|
2017-02-20 15:28:51 +00:00
|
|
|
fn as_any(&self) -> &Any { self }
|
|
|
|
}
|