summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/factoids/database.rs18
-rw-r--r--src/plugins/factoids/mod.rs15
2 files changed, 26 insertions, 7 deletions
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('!'),