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 /src | |
| parent | aa2e6dc0103c303aac0dd688d90c8547b22f8a47 (diff) | |
| download | frippy-f3d679da59a64711ef96042668b26dffd1e662d5.tar.gz frippy-f3d679da59a64711ef96042668b26dffd1e662d5.zip | |
Add Database trait to be used by the Factoids plugin
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/factoids/database.rs | 18 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 15 |
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('!'), |
