document the plugin module and make Plugin::handle a required method

This commit is contained in:
lumi 2017-02-21 17:18:32 +01:00
parent fea28e6171
commit 06380c55cc
2 changed files with 18 additions and 2 deletions

View file

@ -29,10 +29,12 @@ pub enum PluginProxy {
} }
impl PluginProxy { impl PluginProxy {
/// Returns a new `PluginProxy`.
pub fn new() -> PluginProxy { pub fn new() -> PluginProxy {
PluginProxy::Unbound PluginProxy::Unbound
} }
/// Binds the `PluginProxy` to a `PluginProxyBinding`.
pub fn bind(&mut self, inner: PluginProxyBinding) { pub fn bind(&mut self, inner: PluginProxyBinding) {
if let PluginProxy::BoundTo(_) = *self { if let PluginProxy::BoundTo(_) = *self {
panic!("trying to bind an already bound plugin proxy!"); panic!("trying to bind an already bound plugin proxy!");
@ -51,6 +53,7 @@ impl PluginProxy {
} }
} }
/// Dispatches an event.
pub fn dispatch<E: Event>(&self, event: E) { pub fn dispatch<E: Event>(&self, event: E) {
self.with_binding(move |binding| { self.with_binding(move |binding| {
binding.dispatcher.send(AbstractEvent::new(event)) binding.dispatcher.send(AbstractEvent::new(event))
@ -58,6 +61,7 @@ impl PluginProxy {
}); });
} }
/// Sends a stanza.
pub fn send(&self, elem: Element) { pub fn send(&self, elem: Element) {
self.with_binding(move |binding| { self.with_binding(move |binding| {
binding.sender.send(elem).unwrap(); // TODO: as above, may want to return the error binding.sender.send(elem).unwrap(); // TODO: as above, may want to return the error
@ -65,16 +69,24 @@ impl PluginProxy {
} }
} }
/// A plugin handler return value.
///
/// The `Continue` variant means to do nothing, the `Unload` variant means to unload the plugin.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PluginReturn { pub enum PluginReturn {
Continue, Continue,
Unload, Unload,
} }
/// A trait whch all plugins should implement.
pub trait Plugin: Any + PluginAny { pub trait Plugin: Any + PluginAny {
/// Gets a mutable reference to the inner `PluginProxy`.
fn get_proxy(&mut self) -> &mut PluginProxy; fn get_proxy(&mut self) -> &mut PluginProxy;
fn handle(&mut self, _elem: &Element) -> PluginReturn { PluginReturn::Continue }
/// Handles a received stanza.
fn handle(&mut self, elem: &Element) -> PluginReturn;
#[doc(hidden)]
fn bind(&mut self, inner: PluginProxyBinding) { fn bind(&mut self, inner: PluginProxyBinding) {
self.get_proxy().bind(inner); self.get_proxy().bind(inner);
} }

View file

@ -1,5 +1,5 @@
use error::Error; use error::Error;
use plugin::{Plugin, PluginProxy}; use plugin::{Plugin, PluginProxy, PluginReturn};
use minidom::Element; use minidom::Element;
@ -98,4 +98,8 @@ impl Plugin for PresencePlugin {
fn get_proxy(&mut self) -> &mut PluginProxy { fn get_proxy(&mut self) -> &mut PluginProxy {
&mut self.proxy &mut self.proxy
} }
fn handle(&mut self, _elem: &Element) -> PluginReturn {
PluginReturn::Continue
}
} }