summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock9
-rw-r--r--Cargo.toml1
-rw-r--r--plugin_derive/Cargo.toml11
-rw-r--r--plugin_derive/src/lib.rs27
-rw-r--r--src/lib.rs10
-rw-r--r--src/plugin.rs29
-rw-r--r--src/plugins/currency.rs14
-rw-r--r--src/plugins/emoji.rs13
8 files changed, 75 insertions, 39 deletions
diff --git a/Cargo.lock b/Cargo.lock
index cb2fc71..51aaac8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5,6 +5,7 @@ dependencies = [
"clippy 0.0.165 (registry+https://github.com/rust-lang/crates.io-index)",
"irc 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "plugin_derive 0.1.0",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -665,6 +666,14 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "plugin_derive"
+version = "0.1.0"
+dependencies = [
+ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "pulldown-cmark"
version = "0.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 396e737..e0701ad 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,6 +26,7 @@ lazy_static = "0.2.9"
serde = "1.0.15"
serde_json = "1.0.3"
+plugin_derive = { path = "plugin_derive" }
unicode_names = { git = 'https://github.com/Jokler/unicode_names', branch = 'update-to-latest-unicode' }
clippy = {version = "*", optional = true}
diff --git a/plugin_derive/Cargo.toml b/plugin_derive/Cargo.toml
new file mode 100644
index 0000000..0b62f9f
--- /dev/null
+++ b/plugin_derive/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "plugin_derive"
+version = "0.1.0"
+authors = ["Jokler <jokler.contact@gmail.com>"]
+
+[lib]
+proc-macro = true
+
+[dependencies]
+syn = "0.11.11"
+quote = "0.3.15"
diff --git a/plugin_derive/src/lib.rs b/plugin_derive/src/lib.rs
new file mode 100644
index 0000000..704d6ef
--- /dev/null
+++ b/plugin_derive/src/lib.rs
@@ -0,0 +1,27 @@
+
+//! Provides the plugin derive macro
+
+extern crate proc_macro;
+extern crate syn;
+#[macro_use]
+extern crate quote;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_derive(PluginName)]
+pub fn derive_plugin(data: TokenStream) -> TokenStream {
+ let ast = syn::parse_derive_input(&data.to_string()).unwrap();
+ let gen = expand_plugin(&ast);
+ gen.parse().unwrap()
+}
+
+fn expand_plugin(ast: &syn::DeriveInput) -> quote::Tokens {
+ let name = &ast.ident;
+ quote! {
+ impl PluginName for #name {
+ fn name(&self) -> &str {
+ stringify!(#name)
+ }
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index ca1aafe..04210a6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,11 +13,12 @@
#[macro_use]
extern crate lazy_static;
+#[macro_use]
+extern crate plugin_derive;
extern crate irc;
extern crate regex;
-#[macro_use]
mod plugin;
mod plugins;
@@ -28,8 +29,7 @@ use irc::client::prelude::*;
use irc::proto::Command::PRIVMSG;
use irc::error::Error as IrcError;
-use plugin::Plugin;
-use plugin::PluginCommand;
+use plugin::*;
/// Runs the bot
///
@@ -49,7 +49,7 @@ pub fn run() {
// if they use an incorrect plugin name
let plugin_names: Vec<String> = plugins
.iter()
- .map(|p| p.lock().unwrap().to_string().to_lowercase())
+ .map(|p| p.lock().unwrap().name().to_lowercase())
.collect();
// The main loop over received messages
@@ -101,7 +101,7 @@ pub fn run() {
// Check if the command is for this plugin
if let Some(mut c) = command {
if !c.tokens.is_empty() &&
- plugin.to_string().to_lowercase() == c.tokens[0].to_lowercase() {
+ plugin.name().to_lowercase() == c.tokens[0].to_lowercase() {
// The first token contains the name of the plugin
c.tokens.remove(0);
diff --git a/src/plugin.rs b/src/plugin.rs
index 3791bf1..0a4034d 100644
--- a/src/plugin.rs
+++ b/src/plugin.rs
@@ -2,38 +2,19 @@ use std::fmt;
use irc::client::prelude::*;
use irc::error::Error as IrcError;
-pub trait Plugin: Send + Sync + fmt::Display + fmt::Debug {
+pub trait Plugin: PluginName + Send + Sync + fmt::Debug {
fn is_allowed(&self, server: &IrcServer, message: &Message) -> bool;
fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError>;
fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>;
}
+pub trait PluginName: Send + Sync + fmt::Debug {
+ fn name(&self) -> &str;
+}
+
#[derive(Clone, Debug)]
pub struct PluginCommand {
pub source: String,
pub target: String,
pub tokens: Vec<String>,
}
-
-macro_rules! register_plugin {
- ($t:ident) => {
- use std::fmt;
-
- #[derive(Debug)]
- pub struct $t {
- _name: &'static str,
- }
-
- impl fmt::Display for $t {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}", self._name)
- }
- }
-
- impl $t {
- pub fn new() -> $t {
- $t { _name: stringify!($t) }
- }
- }
- };
-}
diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs
index b2320ac..d29e560 100644
--- a/src/plugins/currency.rs
+++ b/src/plugins/currency.rs
@@ -6,14 +6,14 @@ extern crate regex;
use std::io::Read;
use irc::client::prelude::*;
use irc::error::Error as IrcError;
-use plugin::Plugin;
use self::reqwest::Client;
use self::reqwest::header::Connection;
use self::serde_json::Value;
-use PluginCommand;
+use plugin::*;
-register_plugin!(Currency);
+#[derive(PluginName, Debug)]
+pub struct Currency;
struct ConvertionRequest<'a> {
value: f64,
@@ -62,6 +62,11 @@ impl<'a> ConvertionRequest<'a> {
}
impl Currency {
+
+ pub fn new() -> Currency {
+ Currency {}
+ }
+
fn eval_command<'a>(&self, tokens: &'a [String]) -> Option<ConvertionRequest<'a>> {
let parsed = match tokens[0].parse() {
Ok(v) => v,
@@ -144,5 +149,4 @@ impl Plugin for Currency {
}
#[cfg(test)]
-mod tests {
-}
+mod tests {}
diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs
index c7054d8..b08b1b7 100644
--- a/src/plugins/emoji.rs
+++ b/src/plugins/emoji.rs
@@ -2,12 +2,16 @@ extern crate unicode_names;
use irc::client::prelude::*;
use irc::error::Error as IrcError;
-use plugin::Plugin;
-use PluginCommand;
-register_plugin!(Emoji);
+use plugin::*;
+#[derive(PluginName, Debug)]
+pub struct Emoji;
impl Emoji {
+ pub fn new() -> Emoji {
+ Emoji {}
+ }
+
fn emoji(&self, server: &IrcServer, content: &str, target: &str) -> Result<(), IrcError> {
let mut names: Vec<String> = Vec::new();
@@ -73,5 +77,4 @@ impl Plugin for Emoji {
}
#[cfg(test)]
-mod tests {
-}
+mod tests {}