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/tell/mod.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/plugins/tell/mod.rs') diff --git a/src/plugins/tell/mod.rs b/src/plugins/tell/mod.rs index 07b2478..eb7a244 100644 --- a/src/plugins/tell/mod.rs +++ b/src/plugins/tell/mod.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use antidote::RwLock; use irc::client::data::User; use irc::client::prelude::*; @@ -8,6 +10,7 @@ use std::time::Duration; use time; use plugin::*; +use FrippyClient; use self::error::*; use error::ErrorKind as FrippyErrorKind; @@ -19,22 +22,20 @@ pub mod database; use self::database::Database; #[derive(PluginName)] -pub struct Tell { +pub struct Tell { tells: RwLock, + phantom: PhantomData, } -impl Tell { - pub fn new(db: T) -> Tell { +impl Tell { + pub fn new(db: T) -> Self { Tell { tells: RwLock::new(db), + phantom: PhantomData, } } - fn tell_command( - &self, - client: &IrcClient, - command: PluginCommand, - ) -> Result { + fn tell_command(&self, client: &C, command: PluginCommand) -> Result { if command.tokens.len() < 2 { return Ok(self.invalid_command().to_owned()); } @@ -105,7 +106,7 @@ impl Tell { }) } - fn on_namelist(&self, client: &IrcClient, channel: &str) -> Result<(), FrippyError> { + fn on_namelist(&self, client: &C, channel: &str) -> Result<(), FrippyError> { let receivers = self.tells .read() .get_receivers() @@ -128,7 +129,7 @@ impl Tell { } } - fn send_tells(&self, client: &IrcClient, receiver: &str) -> Result<(), FrippyError> { + fn send_tells(&self, client: &C, receiver: &str) -> Result<(), FrippyError> { if client.current_nickname() == receiver { return Ok(()); } @@ -187,8 +188,9 @@ impl Tell { } } -impl Plugin for Tell { - fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus { +impl Plugin for Tell { + type Client = C; + fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus { let res = match message.command { Command::JOIN(_, _, _) => self.send_tells(client, message.source_nickname().unwrap()), Command::NICK(ref nick) => self.send_tells(client, nick), @@ -211,11 +213,11 @@ impl Plugin for Tell { } } - fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), FrippyError> { + fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> { panic!("Tell should not use threading") } - fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { + fn command(&self, client: &Self::Client, command: PluginCommand) -> Result<(), FrippyError> { if command.tokens.is_empty() { client .send_notice(&command.source, &self.invalid_command()) @@ -242,13 +244,13 @@ impl Plugin for Tell { Ok(()) } - fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { + fn evaluate(&self, _: &Self::Client, _: PluginCommand) -> Result { Err(String::from("This Plugin does not implement any commands.")) } } use std::fmt; -impl fmt::Debug for Tell { +impl fmt::Debug for Tell { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Tell {{ ... }}") } -- cgit v1.2.3-70-g09d2