Flesh out plugins event handling, add example stdin plugin (event producer) and echo plugin (event consumer). Next step: fleshing out user/channel structs

This commit is contained in:
Adrian Malacoda
2017-02-13 00:22:06 -06:00
parent a31b060dd3
commit 26a6b77632
12 changed files with 180 additions and 19 deletions

View File

@@ -1,6 +1,8 @@
use plugins::Plugin;
use toml::Table;
use Tenquestionmarks;
pub struct DiscordPlugin {
}
@@ -16,6 +18,4 @@ impl DiscordPlugin {
}
}
impl Plugin for DiscordPlugin {
}
impl Plugin for DiscordPlugin {}

34
src/plugins/echo.rs Normal file
View File

@@ -0,0 +1,34 @@
use plugins::Plugin;
use toml::Table;
use event::Event;
pub struct EchoPlugin {
prefix: String
}
impl EchoPlugin {
pub fn new (configuration: &Table) -> Box<Plugin> {
let prefix = configuration.get("prefix")
.and_then(|value| value.as_str())
.unwrap_or("!echo ");
Box::new(EchoPlugin {
prefix: String::from(prefix)
})
}
}
impl Plugin for EchoPlugin {
fn on_event (&self, event: Event) {
match event {
Event::Message { content: message, channel: channel, sender: sender } => {
if message.starts_with(self.prefix.as_str()) {
let substring = String::from(&message[self.prefix.chars().count()..]);
println!("Echo: {:?}", substring);
}
}
_ => ()
}
}
}

View File

@@ -1,6 +1,8 @@
use plugins::Plugin;
use toml::Table;
use Tenquestionmarks;
pub struct Hello {
name: String
}
@@ -11,7 +13,6 @@ impl Hello {
.and_then(|value| value.as_str())
.unwrap_or("world");
println!("Hello, {}!", name);
Box::new(Hello {
name: String::from(name)
})
@@ -19,5 +20,7 @@ impl Hello {
}
impl Plugin for Hello {
fn register (&self, tenquestionmarks: &Tenquestionmarks) {
println!("Hello, {}!", self.name);
}
}

View File

@@ -8,6 +8,8 @@ use plugins::Plugin;
use plugins::hello::Hello;
use plugins::discord::DiscordPlugin;
use plugins::lua::LuaPlugin;
use plugins::stdin::StdinPlugin;
use plugins::echo::EchoPlugin;
pub struct PluginLoader {
types: BTreeMap<&'static str, fn(&Table) -> Box<Plugin>>
@@ -19,6 +21,8 @@ impl PluginLoader {
types.insert("hello", Hello::new as fn(&Table) -> Box<Plugin>);
types.insert("discord", DiscordPlugin::new as fn(&Table) -> Box<Plugin>);
types.insert("lua", LuaPlugin::new as fn(&Table) -> Box<Plugin>);
types.insert("stdin", StdinPlugin::new as fn(&Table) -> Box<Plugin>);
types.insert("echo", EchoPlugin::new as fn(&Table) -> Box<Plugin>);
PluginLoader {
types: types
}

View File

@@ -1,6 +1,8 @@
use plugins::Plugin;
use toml::Table;
use Tenquestionmarks;
pub struct LuaPlugin {
}
@@ -11,6 +13,4 @@ impl LuaPlugin {
}
}
impl Plugin for LuaPlugin {
}
impl Plugin for LuaPlugin {}

View File

@@ -6,9 +6,18 @@ use toml::Table;
pub mod hello;
pub mod lua;
pub mod discord;
pub mod stdin;
pub mod echo;
pub mod loader;
pub trait Plugin {
use Tenquestionmarks;
use event::Event;
use std::sync::mpsc::{Sender, Receiver};
pub trait Plugin : Sync {
fn register (&self, tenquestionmarks: &Tenquestionmarks) {}
fn on_event (&self, event: Event) {}
fn run (&self, sender: Sender<Event>) {}
}

45
src/plugins/stdin.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::io;
use plugins::Plugin;
use toml::Table;
use Tenquestionmarks;
use User;
use std::sync::mpsc::Sender;
use event::Event;
pub struct StdinPlugin {
user: User
}
impl StdinPlugin {
pub fn new (configuration: &Table) -> Box<Plugin> {
Box::new(StdinPlugin {
user: User {
name: String::from("Dave")
}
})
}
}
impl Plugin for StdinPlugin {
fn register (&self, tenquestionmarks: &Tenquestionmarks) {
}
fn run (&self, sender: Sender<Event>) {
let user = &self.user;
loop {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
let message = Event::Message { sender: user, content: input, channel: None };
sender.send(message);
}
Err(error) => println!("error: {}", error),
}
}
}
}