document the plugin
module and make Plugin::handle
a required method
This commit is contained in:
parent
fea28e6171
commit
06380c55cc
2 changed files with 18 additions and 2 deletions
|
@ -29,10 +29,12 @@ pub enum PluginProxy {
|
|||
}
|
||||
|
||||
impl PluginProxy {
|
||||
/// Returns a new `PluginProxy`.
|
||||
pub fn new() -> PluginProxy {
|
||||
PluginProxy::Unbound
|
||||
}
|
||||
|
||||
/// Binds the `PluginProxy` to a `PluginProxyBinding`.
|
||||
pub fn bind(&mut self, inner: PluginProxyBinding) {
|
||||
if let PluginProxy::BoundTo(_) = *self {
|
||||
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) {
|
||||
self.with_binding(move |binding| {
|
||||
binding.dispatcher.send(AbstractEvent::new(event))
|
||||
|
@ -58,6 +61,7 @@ impl PluginProxy {
|
|||
});
|
||||
}
|
||||
|
||||
/// Sends a stanza.
|
||||
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
|
||||
|
@ -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)]
|
||||
pub enum PluginReturn {
|
||||
Continue,
|
||||
Unload,
|
||||
}
|
||||
|
||||
/// A trait whch all plugins should implement.
|
||||
pub trait Plugin: Any + PluginAny {
|
||||
/// Gets a mutable reference to the inner `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) {
|
||||
self.get_proxy().bind(inner);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use error::Error;
|
||||
use plugin::{Plugin, PluginProxy};
|
||||
use plugin::{Plugin, PluginProxy, PluginReturn};
|
||||
|
||||
use minidom::Element;
|
||||
|
||||
|
@ -98,4 +98,8 @@ impl Plugin for PresencePlugin {
|
|||
fn get_proxy(&mut self) -> &mut PluginProxy {
|
||||
&mut self.proxy
|
||||
}
|
||||
|
||||
fn handle(&mut self, _elem: &Element) -> PluginReturn {
|
||||
PluginReturn::Continue
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue