Added event based systems

This commit is contained in:
wrk 2023-06-09 03:36:07 +02:00
parent 688fae47bb
commit bb512f4757
4 changed files with 31 additions and 1 deletions

View File

@ -45,4 +45,8 @@ impl IrcContext {
pub fn mode(&mut self, channel: &str, modes: &str) {
self.queue(&format!("MODE {} {}", channel, modes))
}
pub fn who(&mut self, channel: &str) {
self.queue(&format!("WHO {}", channel))
}
}

View File

@ -6,7 +6,7 @@ macro_rules! make_irc_command_enum {
($($variant:ident: $value:expr),+) => {
#[allow(non_camel_case_types)]
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum IrcCommand {
UNKNOWN,
$($variant),+

View File

@ -194,6 +194,7 @@ pub struct Irc {
default_system: Option<StoredSystem>,
invalid_system: Option<StoredSystem>,
systems: HashMap<String, StoredSystem>,
event_systems: HashMap<IrcCommand, StoredSystem>,
tasks: Vec<(Duration, StoredSystem)>,
factory: Arc<RwLock<Factory>>,
}
@ -220,6 +221,7 @@ impl Irc {
default_system: None,
invalid_system: None,
systems: HashMap::default(),
event_systems: HashMap::default(),
tasks: Vec::new(),
factory: Arc::new(RwLock::new(Factory::default())),
})
@ -235,6 +237,16 @@ impl Irc {
self
}
pub async fn add_event_system<I, S: for<'a> System + Send + Sync + 'static>(
&mut self,
evt: IrcCommand,
system: impl for<'a> IntoSystem<I, System = S>,
) -> &mut Self {
self.event_systems
.insert(evt, Box::new(system.into_system()));
self
}
pub async fn add_default_system<I, S: for<'a> System + Send + Sync + 'static>(
&mut self,
system: impl for<'a> IntoSystem<I, System = S>,
@ -385,6 +397,7 @@ impl Irc {
}
async fn handle_message<'a>(&mut self, message: &'a IrcMessage<'a>) {
self.run_event_system(message).await;
match message.command {
IrcCommand::PING => self.event_ping(&message.parameters[0]).await,
IrcCommand::RPL_WELCOME => self.event_welcome(&message.parameters[1..].join(" ")).await,
@ -468,6 +481,18 @@ impl Irc {
)
}
pub async fn run_event_system<'a>(&mut self, message: &'a IrcMessage<'a>) {
let Some(system) = self.event_systems.get_mut(&message.command) else { return; };
system.run(
&message.prefix.clone().unwrap_or_default(),
"",
&message.parameters,
&mut *self.context.write().await,
&mut *self.factory.write().await,
);
}
pub async fn run_default_system<'a>(
&mut self,
prefix: &'a IrcPrefix<'a>,

View File

@ -137,6 +137,7 @@ impl<'a> SystemParam for Channel<'a> {
}
}
#[derive(Debug)]
pub struct AnyArguments<'a>(&'a [&'a str]);
impl<'a> Deref for AnyArguments<'a> {