2024.01.27 - init add back generics

This commit is contained in:
ModulatingForce 2024-01-27 21:40:08 -05:00
parent 61dd22c90d
commit b9ea781ed8
5 changed files with 109 additions and 70 deletions

View file

@ -145,9 +145,10 @@ impl Chat {
pub struct BotInstance pub struct BotInstance<F>
// where where
// F: std::future::Future + ?Sized, // F: std::future::Future + ?Sized,
F: std::future::Future
{ {
prefix : char, prefix : char,
bot_channel : ChType, bot_channel : ChType,
@ -156,7 +157,7 @@ pub struct BotInstance
// pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel // pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
pub chat : Chat, pub chat : Chat,
// botmodules : HashMap<ModType,Vec<EnType>>, // botmodules : HashMap<ModType,Vec<EnType>>,
pub botmodules : ModulesManager, pub botmodules : ModulesManager<F>,
twitch_oauth : String, twitch_oauth : String,
pub bot_channels : Vec<ChType>, pub bot_channels : Vec<ChType>,
/*bot_commands : Vec[BotCommand], /*bot_commands : Vec[BotCommand],
@ -168,16 +169,16 @@ pub struct BotInstance
impl BotInstance impl<F> BotInstance<F>
// where where
// F: std::future::Future + 'static, F: std::future::Future + 'static,
// //F: 'static, //F: 'static,
{ {
pub fn init() -> BotInstance pub fn init() -> BotInstance<F>
// where where
// F: std::future::Future + 'static, F: std::future::Future + 'static,
{ {
dotenv().ok(); 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 { // while let Some(message) = self.incoming_messages.recv().await {
// Below can be used to debug if I want to capture all messages // // Below can be used to debug if I want to capture all messages
// println!("Received message: {:?}", message); // // println!("Received message: {:?}", message);
match message { // match message {
ServerMessage::Notice(msg) => { // ServerMessage::Notice(msg) => {
if let Some(chnl) = msg.channel_login { // if let Some(chnl) = msg.channel_login {
println!("NOTICE : (#{}) {}", chnl, msg.message_text); // println!("NOTICE : (#{}) {}", chnl, msg.message_text);
} // }
} // }
ServerMessage::Privmsg(msg) => { // ServerMessage::Privmsg(msg) => {
println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text); // println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
println!("Privmsg section"); // println!("Privmsg section");
// b.listener_main_prvmsg(&msg); // // b.listener_main_prvmsg(&msg);
self.listener_main_prvmsg(&msg).await; // self.listener_main_prvmsg(&msg).await;
// - BotCommand listener should likely need to be called within the above // // - BotCommand listener should likely need to be called within the above
}, // },
ServerMessage::Whisper(msg) => { // ServerMessage::Whisper(msg) => {
println!("(w) {}: {}", msg.sender.name, msg.message_text); // println!("(w) {}: {}", msg.sender.name, msg.message_text);
}, // },
ServerMessage::Join(msg) => { // ServerMessage::Join(msg) => {
println!("JOINED: {}", msg.channel_login); // println!("JOINED: {}", msg.channel_login);
}, // },
ServerMessage::Part(msg) => { // ServerMessage::Part(msg) => {
println!("PARTED: {}", msg.channel_login); // println!("PARTED: {}", msg.channel_login);
}, // },
_ => {} // _ => {}
} // }
} // }
}); // });
join_handle.await.unwrap(); // join_handle.await.unwrap();
} // }
// ----------------- // -----------------

View file

@ -58,25 +58,25 @@ pub enum ModStatusType {
// pub use EnType::Enabled; // pub use EnType::Enabled;
#[derive(Debug)] //#[derive(Debug)]
pub enum BotAction pub enum BotAction<F>
// where // where
// F: std::future::Future + ?Sized, // F: std::future::Future + ?Sized,
{ {
//C(BotCommand), //C(BotCommand),
L(Listener), L(Listener<F>),
R(Routine), R(Routine),
} }
pub trait BotActionTrait pub trait BotActionTrait<F>
// where where
// F: std::future::Future, F: std::future::Future,
{ {
// async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage); // async fn execute(&self,m:&mut BotInstance,n:&PrivmsgMessage);
// fn add_to_bot(self, bot:BotInstance) -> Result<String,Box<dyn Error>>; // fn add_to_bot(self, bot:BotInstance) -> Result<String,Box<dyn Error>>;
fn add_to_bot(self, bot:BotInstance); fn add_to_bot(self, bot:BotInstance<F>);
fn add_to_modmgr(self, modmgr:ModulesManager) -> ModulesManager; fn add_to_modmgr(self, modmgr:ModulesManager<F>) -> ModulesManager<F>;
} }
@ -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<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
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<F> = Box<dyn Fn(Chat,PrivmsgMessage) -> Pin<Box<dyn Future<Output = F>>>>;
pub fn asyncbox<F, T>(f: fn(Chat,PrivmsgMessage) -> T) -> ExecBody<F>
where
T: Future<Output = F> + 'static,
//T: Future<Output = F> ,
{
Box::new(move |a,b| Box::pin(f(a,b)))
}
}
}
// #[derive(Debug)] // #[derive(Debug)]
// pub struct Listener { // pub struct Listener {
// pub module : ModType, // pub module : ModType,
@ -172,8 +209,8 @@ pub trait BotActionTrait
// pub help : String // pub help : String
// } // }
#[derive(Debug)] // #[derive(Debug)]
pub struct Listener pub struct Listener<F>
// where // where
// F: std::future::Future + ?Sized, // 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(&mut botinstance::Chat,&PrivmsgMessage) -> F ,
// pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F , // pub exec_body : fn(botinstance::Chat,PrivmsgMessage) -> F ,
//pub exec_body : fn(String,&PrivmsgMessage) -> F , //pub exec_body : fn(String,&PrivmsgMessage) -> F ,
pub exec_body : bot_actions::actions_util::ExecBody<F>,
pub help : String pub help : String
} }
impl Listener impl<F> Listener<F>
// where // where
// F: std::future::Future, // F: std::future::Future,
{ {
@ -203,16 +241,16 @@ impl Listener
} }
} }
impl BotActionTrait for Listener impl<F> BotActionTrait<F> for Listener<F>
// where where
// F: std::future::Future, F: std::future::Future,
{ {
// fn add_to_bot(&self) -> Result<String,Box<dyn Error>> { // fn add_to_bot(&self) -> Result<String,Box<dyn Error>> {
// println!("Calling Add to Bot"); // println!("Calling Add to Bot");
// Ok(String::from("Hello")) // Ok(String::from("Hello"))
// } // }
fn add_to_bot(self, bot:BotInstance) { fn add_to_bot(self, bot:BotInstance<F>) {
// let mut mgr = bot.botmodules; // let mut mgr = bot.botmodules;
// let nmod = self.module.clone(); // let nmod = self.module.clone();
// mgr.add_botaction(nmod, BotAction::C(self)); // mgr.add_botaction(nmod, BotAction::C(self));
@ -225,7 +263,7 @@ impl BotActionTrait for Listener
// fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>) { // fn add_to_modmgr(self, modmgr:&mut ModulesManager<F>) {
//fn add_to_modmgr(self, mut modmgr: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<F>) -> ModulesManager<F> {
// // let mut mgr = bot.botmodules; // // let mut mgr = bot.botmodules;
// // let nmod = self.module.clone(); // // let nmod = self.module.clone();
// // mgr.add_botaction(nmod, BotAction::C(self)); // // mgr.add_botaction(nmod, BotAction::C(self));
@ -269,21 +307,21 @@ impl BotActionTrait for Listener
struct Routine {} struct Routine {}
#[derive(Debug)] //#[derive(Debug)]
pub struct ModulesManager pub struct ModulesManager<F>
// where // where
// F: std::future::Future + ?Sized, // F: std::future::Future + ?Sized,
{ {
statusdb: HashMap<ModType,Vec<ModStatusType>>, statusdb: HashMap<ModType,Vec<ModStatusType>>,
botactions: HashMap<ModType,Vec<BotAction>>, botactions: HashMap<ModType,Vec<BotAction<F>>>,
} }
impl ModulesManager impl<F> ModulesManager<F>
// where // where
// F: std::future::Future, // F: std::future::Future,
{ {
pub fn init() -> ModulesManager pub fn init() -> ModulesManager<F>
// where // where
// F: std::future::Future , // F: std::future::Future ,
{ {
@ -412,7 +450,7 @@ impl ModulesManager
Ok("") 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<F> ) -> ModulesManager<F> {
//pub fn add_botaction(&mut self, in_module:ModType, in_action:BotAction ) -> () { //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 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), // - 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 // Check All Other BotAction Command Names & Aliases to ensure they don't conflict
fn find_conflict_module(mgr:& ModulesManager, act:& BotAction) -> Option<ModType> fn find_conflict_module<F>(mgr:& ModulesManager<F>, act:& BotAction<F>) -> Option<ModType>
// where // where
// F: std::future::Future, // F: std::future::Future,
{ {

View file

@ -19,7 +19,7 @@ mod experiments;
// [ ] init() function that accepts bot instance - this is passed to init() on submodules // [ ] init() function that accepts bot instance - this is passed to init() on submodules
pub fn init(mgr:ModulesManager) -> ModulesManager pub fn init<F>(mgr:ModulesManager<F>) -> ModulesManager<F>
// where // where
// F: std::future::Future, // F: std::future::Future,
{ {

View file

@ -17,7 +17,7 @@ use crate::core::botmodules::{ModulesManager,Listener,BotModule,BotActionTrait,
use crate::core::botinstance::{self}; use crate::core::botinstance::{self};
use twitch_irc::message::PrivmsgMessage; use twitch_irc::message::PrivmsgMessage;
pub fn init(mgr:ModulesManager) -> ModulesManager pub fn init<F>(mgr:ModulesManager<F>) -> ModulesManager<F>
// where // where
// F: std::future::Future, // F: std::future::Future,
{ {