From b9ea781ed8790243a6b623191bf6ffd2b2b96593 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Sat, 27 Jan 2024 21:40:08 -0500 Subject: [PATCH] 2024.01.27 - init add back generics --- src/core/botinstance.rs | 89 +++++++++++++++++++------------------- src/core/botmodules.rs | 84 +++++++++++++++++++++++++---------- src/main.rs | 2 +- src/modules.rs | 2 +- src/modules/experiments.rs | 2 +- 5 files changed, 109 insertions(+), 70 deletions(-) diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index c5b7c07..32a64ae 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -145,9 +145,10 @@ impl Chat { -pub struct BotInstance -// where -// F: std::future::Future + ?Sized, +pub struct BotInstance +where + // F: std::future::Future + ?Sized, + F: std::future::Future { prefix : char, bot_channel : ChType, @@ -156,7 +157,7 @@ pub struct BotInstance // pub ratelimiters : HashMap, // used to limit messages sent per channel pub chat : Chat, // botmodules : HashMap>, - pub botmodules : ModulesManager, + pub botmodules : ModulesManager, twitch_oauth : String, pub bot_channels : Vec, /*bot_commands : Vec[BotCommand], @@ -168,16 +169,16 @@ pub struct BotInstance -impl BotInstance -// where -// F: std::future::Future + 'static, -// //F: 'static, +impl BotInstance +where + F: std::future::Future + 'static, + //F: 'static, { - pub fn init() -> BotInstance - // where - // F: std::future::Future + 'static, + pub fn init() -> BotInstance + where + F: std::future::Future + 'static, { dotenv().ok(); @@ -297,51 +298,51 @@ impl BotInstance - pub async fn run(mut self) -> () { + // pub async fn run(mut self) -> () { - let join_handle = tokio::spawn(async move { + // let join_handle = tokio::spawn(async move { - while let Some(message) = self.incoming_messages.recv().await { - // Below can be used to debug if I want to capture all messages - // println!("Received message: {:?}", message); + // while let Some(message) = self.incoming_messages.recv().await { + // // Below can be used to debug if I want to capture all messages + // // println!("Received message: {:?}", message); - match message { - ServerMessage::Notice(msg) => { - if let Some(chnl) = msg.channel_login { - println!("NOTICE : (#{}) {}", chnl, msg.message_text); - } - } - ServerMessage::Privmsg(msg) => { - println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); + // match message { + // ServerMessage::Notice(msg) => { + // if let Some(chnl) = msg.channel_login { + // println!("NOTICE : (#{}) {}", chnl, msg.message_text); + // } + // } + // ServerMessage::Privmsg(msg) => { + // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); - println!("Privmsg section"); + // println!("Privmsg section"); - // b.listener_main_prvmsg(&msg); - self.listener_main_prvmsg(&msg).await; - // - BotCommand listener should likely need to be called within the above + // // b.listener_main_prvmsg(&msg); + // self.listener_main_prvmsg(&msg).await; + // // - BotCommand listener should likely need to be called within the above - }, - ServerMessage::Whisper(msg) => { - println!("(w) {}: {}", msg.sender.name, msg.message_text); - }, - ServerMessage::Join(msg) => { - println!("JOINED: {}", msg.channel_login); - }, - ServerMessage::Part(msg) => { - println!("PARTED: {}", msg.channel_login); - }, - _ => {} - } - } - }); + // }, + // ServerMessage::Whisper(msg) => { + // println!("(w) {}: {}", msg.sender.name, msg.message_text); + // }, + // ServerMessage::Join(msg) => { + // println!("JOINED: {}", msg.channel_login); + // }, + // ServerMessage::Part(msg) => { + // println!("PARTED: {}", msg.channel_login); + // }, + // _ => {} + // } + // } + // }); - join_handle.await.unwrap(); - } + // join_handle.await.unwrap(); + // } // ----------------- diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index f75831f..67d44c7 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -58,25 +58,25 @@ pub enum ModStatusType { // pub use EnType::Enabled; -#[derive(Debug)] -pub enum BotAction +//#[derive(Debug)] +pub enum BotAction // where // F: std::future::Future + ?Sized, { //C(BotCommand), - L(Listener), + L(Listener), R(Routine), } -pub trait BotActionTrait -// where -// F: std::future::Future, +pub trait BotActionTrait +where + F: std::future::Future, { // async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage); // fn add_to_bot(self, bot:BotInstance) -> Result>; - fn add_to_bot(self, bot:BotInstance); - fn add_to_modmgr(self, modmgr:ModulesManager) -> ModulesManager; + fn add_to_bot(self, bot:BotInstance); + fn add_to_modmgr(self, modmgr:ModulesManager) -> ModulesManager; } @@ -164,6 +164,43 @@ pub trait BotActionTrait // } +pub mod bot_actions { + + // use std::boxed::Box; + // use std::pin::Pin; + // use std::future::Future; + + // use crate::core::botinstance::Chat; + // use twitch_irc::message::PrivmsgMessage; + + + // type ExecBody = Box Pin>>>; + + pub mod actions_util { + + use std::future::Future; + use std::boxed::Box; + use std::pin::Pin; + + use crate::core::botinstance::Chat; + use twitch_irc::message::PrivmsgMessage; + + pub type ExecBody = Box Pin>>>; + + pub fn asyncbox(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody + where + T: Future + 'static, + //T: Future , + { + Box::new(move |a,b| Box::pin(f(a,b))) + } + } + + +} + + + // #[derive(Debug)] // pub struct Listener { // pub module : ModType, @@ -172,8 +209,8 @@ pub trait BotActionTrait // pub help : String // } -#[derive(Debug)] -pub struct Listener +// #[derive(Debug)] +pub struct Listener // where // F: std::future::Future + ?Sized, { @@ -184,10 +221,11 @@ pub struct Listener //pub exec_body : fn(&mut botinstance::Chat,&PrivmsgMessage) -> F , // pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F , //pub exec_body : fn(String,&PrivmsgMessage) -> F , + pub exec_body : bot_actions::actions_util::ExecBody, pub help : String } -impl Listener +impl Listener // where // F: std::future::Future, { @@ -203,16 +241,16 @@ impl Listener } } -impl BotActionTrait for Listener -// where -// F: std::future::Future, +impl BotActionTrait for Listener +where + F: std::future::Future, { // fn add_to_bot(&self) -> Result> { // println!("Calling Add to Bot"); // Ok(String::from("Hello")) // } - fn add_to_bot(self, bot:BotInstance) { + fn add_to_bot(self, bot:BotInstance) { // let mut mgr = bot.botmodules; // let nmod = self.module.clone(); // mgr.add_botaction(nmod, BotAction::C(self)); @@ -225,7 +263,7 @@ impl BotActionTrait for Listener // fn add_to_modmgr(self, modmgr:&mut ModulesManager) { //fn add_to_modmgr(self, mut modmgr:ModulesManager) { - fn add_to_modmgr(self, mut modmgr:ModulesManager) -> ModulesManager { + fn add_to_modmgr(self, mut modmgr:ModulesManager) -> ModulesManager { // // let mut mgr = bot.botmodules; // // let nmod = self.module.clone(); // // mgr.add_botaction(nmod, BotAction::C(self)); @@ -269,21 +307,21 @@ impl BotActionTrait for Listener struct Routine {} -#[derive(Debug)] -pub struct ModulesManager +//#[derive(Debug)] +pub struct ModulesManager // where // F: std::future::Future + ?Sized, { statusdb: HashMap>, - botactions: HashMap>, + botactions: HashMap>>, } -impl ModulesManager +impl ModulesManager // where // F: std::future::Future, { - pub fn init() -> ModulesManager + pub fn init() -> ModulesManager // where // F: std::future::Future , { @@ -412,7 +450,7 @@ impl ModulesManager Ok("") } - pub fn add_botaction(mut self, in_module:ModType, in_action:BotAction ) -> ModulesManager { + pub fn add_botaction(mut self, in_module:ModType, in_action:BotAction ) -> ModulesManager { //pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () { /* adds a BotAction to the Modules Manager - This will require a BotModule passed as well @@ -441,7 +479,7 @@ impl ModulesManager // - If BotAction to Add is a BotCommand , In Module Manager DB (botactions), // Check All Other BotAction Command Names & Aliases to ensure they don't conflict - fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option + fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option // where // F: std::future::Future, { diff --git a/src/main.rs b/src/main.rs index e001f36..ee7b3dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use crate::core::botinstance::BotInstance; #[tokio::main] pub async fn main() { - // let bot:BotInstance> = BotInstance::init(); + //let bot:BotInstance> = BotInstance::init(); let bot = BotInstance::init(); diff --git a/src/modules.rs b/src/modules.rs index f4cc19a..c69fdd8 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -19,7 +19,7 @@ mod experiments; // [ ] init() function that accepts bot instance - this is passed to init() on submodules -pub fn init(mgr:ModulesManager) -> ModulesManager +pub fn init(mgr:ModulesManager) -> ModulesManager // where // F: std::future::Future, { diff --git a/src/modules/experiments.rs b/src/modules/experiments.rs index cc1d9bc..fba5427 100644 --- a/src/modules/experiments.rs +++ b/src/modules/experiments.rs @@ -17,7 +17,7 @@ use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait, use crate::core::botinstance::{self}; use twitch_irc::message::PrivmsgMessage; -pub fn init(mgr:ModulesManager) -> ModulesManager +pub fn init(mgr:ModulesManager) -> ModulesManager // where // F: std::future::Future, {