diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-10-29 17:09:04 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-12-24 00:21:43 +0100 |
| commit | f3d679da59a64711ef96042668b26dffd1e662d5 (patch) | |
| tree | 1d24afeb7ab6a364b433e30bb23f821d19ccf90d | |
| parent | aa2e6dc0103c303aac0dd688d90c8547b22f8a47 (diff) | |
| download | frippy-f3d679da59a64711ef96042668b26dffd1e662d5.tar.gz frippy-f3d679da59a64711ef96042668b26dffd1e662d5.zip | |
Add Database trait to be used by the Factoids plugin
| -rw-r--r-- | bin/main.rs | 4 | ||||
| -rw-r--r-- | frippy_derive/src/lib.rs | 4 | ||||
| -rw-r--r-- | src/plugins/factoids/database.rs | 18 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 15 |
4 files changed, 32 insertions, 9 deletions
diff --git a/bin/main.rs b/bin/main.rs index e8cf790..1597a70 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -7,6 +7,8 @@ extern crate futures; #[macro_use] extern crate log; +use std::collections::HashMap; + use log::{LogRecord, LogLevel, LogLevelFilter, LogMetadata}; use tokio_core::reactor::Core; @@ -95,7 +97,7 @@ fn main() { bot.add_plugin(plugins::Emoji::new()); bot.add_plugin(plugins::Currency::new()); bot.add_plugin(plugins::KeepNick::new()); - bot.add_plugin(plugins::Factoids::new()); + bot.add_plugin(plugins::Factoids::new(HashMap::new())); if let Some(disabled_plugins) = disabled_plugins { for name in disabled_plugins { diff --git a/frippy_derive/src/lib.rs b/frippy_derive/src/lib.rs index 704d6ef..2622f0b 100644 --- a/frippy_derive/src/lib.rs +++ b/frippy_derive/src/lib.rs @@ -17,8 +17,10 @@ pub fn derive_plugin(data: TokenStream) -> TokenStream { fn expand_plugin(ast: &syn::DeriveInput) -> quote::Tokens { let name = &ast.ident; + let generics = &ast.generics; + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); quote! { - impl PluginName for #name { + impl #impl_generics PluginName for #name #ty_generics #where_clause { fn name(&self) -> &str { stringify!(#name) } diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs new file mode 100644 index 0000000..7a6f568 --- /dev/null +++ b/src/plugins/factoids/database.rs @@ -0,0 +1,18 @@ +use std::fmt; + +use std::collections::HashMap; + +pub trait Database: Send + Sync + fmt::Debug { + fn insert(&mut self, name: String, content: String) -> Option<String>; + fn get(&self, name: &str) -> Option<&str>; +} + +impl Database for HashMap<String, String> { + fn insert(&mut self, name: String, content: String) -> Option<String> { + self.insert(name, content) + } + + fn get(&self, name: &str) -> Option<&str> { + self.get(name).map(String::as_str) + } +} diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index c69042f..a13bba6 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -4,16 +4,17 @@ use self::rlua::prelude::*; use irc::client::prelude::*; use irc::error::Error as IrcError; -use std::collections::HashMap; use std::sync::Mutex; use plugin::*; +mod database; +use self::database::*; static LUA_SANDBOX: &'static str = include_str!("sandbox.lua"); #[derive(PluginName, Debug)] -pub struct Factoids { - factoids: Mutex<HashMap<String, String>>, +pub struct Factoids<T: Database> { + factoids: Mutex<T>, } macro_rules! try_lock { @@ -25,9 +26,9 @@ macro_rules! try_lock { } } -impl Factoids { - pub fn new() -> Factoids { - Factoids { factoids: Mutex::new(HashMap::new()) } +impl<T: Database> Factoids<T> { + pub fn new(db: T) -> Factoids<T> { + Factoids { factoids: Mutex::new(db) } } fn add(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { @@ -128,7 +129,7 @@ impl Factoids { } } -impl Plugin for Factoids { +impl<T: Database> Plugin for Factoids<T> { fn is_allowed(&self, _: &IrcServer, message: &Message) -> bool { match message.command { Command::PRIVMSG(_, ref content) => content.starts_with('!'), |
