From 3929022952ffbd5be0a9ba4ff074a735dea2aed1 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 30 Jun 2018 19:57:37 +0200 Subject: Plugins: Replace IrcClient with a trait This is to make it simpler to replace the client in the future. --- src/plugins/factoids/mod.rs | 31 ++++++++++++++++++++++--------- src/plugins/factoids/utils.rs | 8 ++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'src/plugins/factoids') diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index 6ff2132..10f5131 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -4,12 +4,14 @@ use self::rlua::prelude::*; use antidote::RwLock; use irc::client::prelude::*; use std::fmt; +use std::marker::PhantomData; use std::str::FromStr; use chrono::NaiveDateTime; use time; use plugin::*; +use FrippyClient; pub mod database; use self::database::Database; @@ -30,14 +32,16 @@ enum FactoidResponse { } #[derive(PluginName)] -pub struct Factoids { +pub struct Factoids { factoids: RwLock, + phantom: PhantomData, } -impl Factoids { - pub fn new(db: T) -> Factoids { +impl Factoids { + pub fn new(db: T) -> Self { Factoids { factoids: RwLock::new(db), + phantom: PhantomData, } } @@ -228,8 +232,9 @@ impl Factoids { } } -impl Plugin for Factoids { - fn execute(&self, _: &IrcClient, message: &Message) -> ExecutionStatus { +impl Plugin for Factoids { + type Client = C; + fn execute(&self, _: &Self::Client, message: &Message) -> ExecutionStatus { match message.command { Command::PRIVMSG(_, ref content) => if content.starts_with('!') { ExecutionStatus::RequiresThread @@ -240,7 +245,11 @@ impl Plugin for Factoids { } } - fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), FrippyError> { + fn execute_threaded( + &self, + client: &Self::Client, + message: &Message, + ) -> Result<(), FrippyError> { if let Command::PRIVMSG(_, mut content) = message.command.clone() { content.remove(0); @@ -262,7 +271,11 @@ impl Plugin for Factoids { Ok(()) } - fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), FrippyError> { + fn command( + &self, + client: &Self::Client, + mut command: PluginCommand, + ) -> Result<(), FrippyError> { use self::FactoidResponse::{Private, Public}; if command.tokens.is_empty() { @@ -310,14 +323,14 @@ impl Plugin for Factoids { Ok(()) } - fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { + fn evaluate(&self, _: &Self::Client, _: PluginCommand) -> Result { Err(String::from( "Evaluation of commands is not implemented for Factoids at this time", )) } } -impl fmt::Debug for Factoids { +impl fmt::Debug for Factoids { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Factoids {{ ... }}") } diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs index 9352a43..a7bf04a 100644 --- a/src/plugins/factoids/utils.rs +++ b/src/plugins/factoids/utils.rs @@ -38,7 +38,9 @@ pub fn download(_: &Lua, url: String) -> Result { fn convert_value(lua: &Lua, sval: SerdeValue, max_recurs: usize) -> Result { if max_recurs == 0 { - return Err(RuntimeError(String::from("Reached max recursion level - json is nested too deep"))); + return Err(RuntimeError(String::from( + "Reached max recursion level - json is nested too deep", + ))); } let lval = match sval { @@ -46,7 +48,9 @@ fn convert_value(lua: &Lua, sval: SerdeValue, max_recurs: usize) -> Result LuaValue::Boolean(b), SerdeValue::String(s) => LuaValue::String(lua.create_string(&s)?), SerdeValue::Number(n) => { - let f = n.as_f64().ok_or(RuntimeError(String::from("Failed to convert number into double",)))?; + let f = n.as_f64().ok_or(RuntimeError(String::from( + "Failed to convert number into double", + )))?; LuaValue::Number(f) } SerdeValue::Array(arr) => { -- cgit v1.2.3-70-g09d2