From 6c1cd8a2c9e0b3600bffaa6151e61252528a363b Mon Sep 17 00:00:00 2001 From: wrk Date: Wed, 31 May 2023 10:39:40 +0200 Subject: [PATCH] default + invalid systems! --- src/events.rs | 28 ++++++------ src/lib.rs | 116 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/events.rs b/src/events.rs index 96ea24b..a062b2c 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,7 +1,7 @@ use log::{debug, info, warn}; use std::time::Duration; -use crate::{Irc, IrcPrefix}; +use crate::{system::Response, Irc, IrcPrefix}; impl Irc { pub(crate) async fn event_ping(&mut self, ping_token: &str) { @@ -124,22 +124,26 @@ impl Irc { return; } + let arguments = elements.collect::>(); + let mut context = self.context.write().await; if !context.systems.contains_key(&sys_name) { + for line in context.run_default_system(prefix, &arguments).await { + context.privmsg(channel, &line) + } return; } - let response = context - .run_system(prefix, elements.collect(), &sys_name) - .await; - match response { - crate::system::Response::Lines(lines) => { - for line in lines { - context.privmsg(channel, &line) - } - } - crate::system::Response::Empty => return, - crate::system::Response::InvalidArgument => return, + let response = context.run_system(prefix, &arguments, &sys_name).await; + + let lines = match response { + Response::Lines(lines) => lines, + Response::InvalidArgument => context.run_invalid_system(prefix, &arguments).await, + _ => return, + }; + + for line in lines { + context.privmsg(channel, &line) } } } diff --git a/src/lib.rs b/src/lib.rs index 7769148..a6e4a3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,6 +168,8 @@ pub struct Context { identified: bool, send_queue: VecDeque, + default_system: Option, + invalid_system: Option, systems: HashMap, interval_tasks: Vec<(Duration, StoredSystem)>, factory: Arc>, @@ -266,43 +268,54 @@ impl Context { } } - pub fn add_system System + Send + Sync + 'static>( - &mut self, - name: &str, - system: impl for<'a> IntoSystem, - ) -> &mut Self { - self.systems - .insert(name.to_owned(), Box::new(system.into_system())); - self - } - - pub fn add_interval_task System + Send + Sync + 'static>( - &mut self, - duration: Duration, - system_task: impl for<'a> IntoSystem, - ) -> &mut Self { - self.interval_tasks - .push((duration, Box::new(system_task.into_system()))); - self - } - - pub async fn add_resource(&mut self, res: R) -> &mut Self { - self.factory - .write() - .await - .resources - .insert(TypeId::of::(), Box::new(res)); - self - } - pub async fn run_system<'a>( &mut self, prefix: &'a IrcPrefix<'a>, - arguments: Vec<&'a str>, + arguments: &'a[&'a str], name: &str, ) -> Response { let system = self.systems.get_mut(name).unwrap(); - system.run(prefix, &arguments, &mut *self.factory.write().await) + system.run(prefix, arguments, &mut *self.factory.write().await) + } + + pub async fn run_default_system<'a>( + &mut self, + prefix: &'a IrcPrefix<'a>, + arguments: &'a[&'a str], + ) -> Vec { + if self.default_system.is_none() { + return vec![]; + } + + if let Response::Lines(lines) = self.default_system.as_mut().unwrap().run( + prefix, + arguments, + &mut *self.factory.write().await, + ) { + lines + } else { + return vec![]; + } + } + + pub async fn run_invalid_system<'a>( + &mut self, + prefix: &'a IrcPrefix<'a>, + arguments: &'a[&'a str], + ) -> Vec { + if self.invalid_system.is_none() { + return vec![]; + } + + if let Response::Lines(lines) = self.invalid_system.as_mut().unwrap().run( + prefix, + arguments, + &mut *self.factory.write().await, + ) { + lines + } else { + return vec![]; + } } pub async fn run_interval_tasks(&mut self, tx: mpsc::Sender>) { @@ -355,6 +368,8 @@ impl Irc { config, identified: false, send_queue: VecDeque::new(), + default_system: None, + invalid_system: None, systems: HashMap::default(), interval_tasks: Vec::new(), factory: Arc::new(RwLock::new(Factory::default())), @@ -375,7 +390,31 @@ impl Irc { ) -> &mut Self { { let mut context = self.context.write().await; - context.add_system(name, system); + context + .systems + .insert(name.to_owned(), Box::new(system.into_system())); + } + self + } + + pub async fn add_default_system System + Send + Sync + 'static>( + &mut self, + system: impl for<'a> IntoSystem, + ) -> &mut Self { + { + let mut context = self.context.write().await; + context.default_system = Some(Box::new(system.into_system())); + } + self + } + + pub async fn add_invalid_system System + Send + Sync + 'static>( + &mut self, + system: impl for<'a> IntoSystem, + ) -> &mut Self { + { + let mut context = self.context.write().await; + context.invalid_system = Some(Box::new(system.into_system())); } self } @@ -387,15 +426,22 @@ impl Irc { ) -> &mut Self { { let mut context = self.context.write().await; - context.add_interval_task(duration, system); + context + .interval_tasks + .push((duration, Box::new(system.into_system()))); } self } pub async fn add_resource(&mut self, res: R) -> &mut Self { { - let mut context = self.context.write().await; - context.add_resource(res).await; + let context = self.context.write().await; + context + .factory + .write() + .await + .resources + .insert(TypeId::of::(), Box::new(res)); } self }