Added event based systems
This commit is contained in:
parent
688fae47bb
commit
bb512f4757
@ -45,4 +45,8 @@ impl IrcContext {
|
|||||||
pub fn mode(&mut self, channel: &str, modes: &str) {
|
pub fn mode(&mut self, channel: &str, modes: &str) {
|
||||||
self.queue(&format!("MODE {} {}", channel, modes))
|
self.queue(&format!("MODE {} {}", channel, modes))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn who(&mut self, channel: &str) {
|
||||||
|
self.queue(&format!("WHO {}", channel))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ macro_rules! make_irc_command_enum {
|
|||||||
|
|
||||||
($($variant:ident: $value:expr),+) => {
|
($($variant:ident: $value:expr),+) => {
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum IrcCommand {
|
pub enum IrcCommand {
|
||||||
UNKNOWN,
|
UNKNOWN,
|
||||||
$($variant),+
|
$($variant),+
|
||||||
|
25
src/lib.rs
25
src/lib.rs
@ -194,6 +194,7 @@ pub struct Irc {
|
|||||||
default_system: Option<StoredSystem>,
|
default_system: Option<StoredSystem>,
|
||||||
invalid_system: Option<StoredSystem>,
|
invalid_system: Option<StoredSystem>,
|
||||||
systems: HashMap<String, StoredSystem>,
|
systems: HashMap<String, StoredSystem>,
|
||||||
|
event_systems: HashMap<IrcCommand, StoredSystem>,
|
||||||
tasks: Vec<(Duration, StoredSystem)>,
|
tasks: Vec<(Duration, StoredSystem)>,
|
||||||
factory: Arc<RwLock<Factory>>,
|
factory: Arc<RwLock<Factory>>,
|
||||||
}
|
}
|
||||||
@ -220,6 +221,7 @@ impl Irc {
|
|||||||
default_system: None,
|
default_system: None,
|
||||||
invalid_system: None,
|
invalid_system: None,
|
||||||
systems: HashMap::default(),
|
systems: HashMap::default(),
|
||||||
|
event_systems: HashMap::default(),
|
||||||
tasks: Vec::new(),
|
tasks: Vec::new(),
|
||||||
factory: Arc::new(RwLock::new(Factory::default())),
|
factory: Arc::new(RwLock::new(Factory::default())),
|
||||||
})
|
})
|
||||||
@ -235,6 +237,16 @@ impl Irc {
|
|||||||
self
|
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>(
|
pub async fn add_default_system<I, S: for<'a> System + Send + Sync + 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
system: impl for<'a> IntoSystem<I, System = S>,
|
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>) {
|
async fn handle_message<'a>(&mut self, message: &'a IrcMessage<'a>) {
|
||||||
|
self.run_event_system(message).await;
|
||||||
match message.command {
|
match message.command {
|
||||||
IrcCommand::PING => self.event_ping(&message.parameters[0]).await,
|
IrcCommand::PING => self.event_ping(&message.parameters[0]).await,
|
||||||
IrcCommand::RPL_WELCOME => self.event_welcome(&message.parameters[1..].join(" ")).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>(
|
pub async fn run_default_system<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
prefix: &'a IrcPrefix<'a>,
|
prefix: &'a IrcPrefix<'a>,
|
||||||
|
@ -137,6 +137,7 @@ impl<'a> SystemParam for Channel<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct AnyArguments<'a>(&'a [&'a str]);
|
pub struct AnyArguments<'a>(&'a [&'a str]);
|
||||||
|
|
||||||
impl<'a> Deref for AnyArguments<'a> {
|
impl<'a> Deref for AnyArguments<'a> {
|
||||||
|
Loading…
Reference in New Issue
Block a user