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/remind/mod.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/plugins/remind/mod.rs') diff --git a/src/plugins/remind/mod.rs b/src/plugins/remind/mod.rs index fa7cf70..fa44b0b 100644 --- a/src/plugins/remind/mod.rs +++ b/src/plugins/remind/mod.rs @@ -1,12 +1,16 @@ -use antidote::RwLock; -use irc::client::prelude::*; +use std::marker::PhantomData; use std::thread::{sleep, spawn}; use std::{fmt, sync::Arc, time::Duration}; +use antidote::RwLock; +use irc::client::prelude::*; + use chrono::{self, NaiveDateTime}; use time; use plugin::*; +use FrippyClient; + pub mod database; mod parser; use self::database::Database; @@ -38,7 +42,7 @@ fn get_events(db: &RwLock, in_next: chrono::Duration) -> Vec(client: &IrcClient, db: Arc>) { +fn run(client: &C, db: Arc>) { let look_ahead = chrono::Duration::minutes(2); let mut events = get_events(&db, look_ahead); @@ -88,18 +92,20 @@ fn run(client: &IrcClient, db: Arc>) { } #[derive(PluginName)] -pub struct Remind { +pub struct Remind { events: Arc>, has_reminder: RwLock, + phantom: PhantomData, } -impl Remind { +impl Remind { pub fn new(db: T) -> Self { let events = Arc::new(RwLock::new(db)); Remind { events, has_reminder: RwLock::new(false), + phantom: PhantomData, } } @@ -188,8 +194,9 @@ impl Remind { } } -impl Plugin for Remind { - fn execute(&self, client: &IrcClient, msg: &Message) -> ExecutionStatus { +impl Plugin for Remind { + type Client = C; + fn execute(&self, client: &Self::Client, msg: &Message) -> ExecutionStatus { if let Command::JOIN(_, _, _) = msg.command { let mut has_reminder = self.has_reminder.write(); @@ -206,11 +213,15 @@ impl Plugin for Remind { ExecutionStatus::Done } - fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), FrippyError> { + fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> { panic!("Remind should not use frippy's threading") } - fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), FrippyError> { + fn command( + &self, + client: &Self::Client, + mut command: PluginCommand, + ) -> Result<(), FrippyError> { if command.tokens.is_empty() { client .send_notice(&command.source, &ErrorKind::InvalidCommand.to_string()) @@ -248,14 +259,14 @@ impl Plugin for Remind { Ok(()) } - fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { + fn evaluate(&self, _: &Self::Client, _: PluginCommand) -> Result { Err(String::from( "Evaluation of commands is not implemented for remind at this time", )) } } -impl fmt::Debug for Remind { +impl fmt::Debug for Remind { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Remind {{ ... }}") } -- cgit v1.2.3-70-g09d2