feat: command handling, make initializing VCs concurrent

This commit is contained in:
2026-03-25 16:55:54 -04:00
parent 67bdc9e451
commit d9e0801ec9
7 changed files with 183 additions and 66 deletions

View File

@@ -1,6 +1,7 @@
use std::{
ffi::{CStr, CString},
fmt::Debug,
sync::Arc,
};
use blart::TreeMap;
@@ -9,12 +10,16 @@ use twilight_model::application::{
command::Command, interaction::application_command::CommandData,
};
use crate::VCs;
mod join;
mod leave;
mod opt_out;
#[derive(Debug, Clone)]
pub struct State {}
pub struct State {
pub vcs: Arc<VCs>,
}
type Return = ();
type BoxedHandler = Box<dyn Fn(State, CommandData) -> BoxFuture<'static, Return>>;
@@ -53,7 +58,7 @@ impl Router {
self.map.insert(name.parse().unwrap(), boxed_handler);
}
pub async fn handle(&self, args: State, command_data: CommandData) -> Return {
pub async fn handle(&self, state: State, command_data: CommandData) -> Return {
let name = &command_data.name;
let key = CStr::from_bytes_with_nul(name.as_bytes()).unwrap();
@@ -62,19 +67,27 @@ impl Router {
.get(key)
.expect("asked to handle an inexistent command");
handler(args, command_data).await
handler(state, command_data).await
}
}
impl<'a> FromIterator<(&'a CommandData, BoxedHandler)> for Router {
fn from_iter<T: IntoIterator<Item = (&'a CommandData, BoxedHandler)>>(iter: T) -> Self {
let mut router = Router::default();
impl<'a> FromIterator<(&'a Command, BoxedHandler)> for Router {
#[inline]
fn from_iter<T: IntoIterator<Item = (&'a Command, BoxedHandler)>>(iter: T) -> Self {
let mut this = Self::default();
for (command, handler) in iter {
this.extend(iter);
this
}
}
impl<'a> Extend<(&'a Command, BoxedHandler)> for Router {
#[inline]
fn extend<T: IntoIterator<Item = (&'a Command, BoxedHandler)>>(&mut self, iter: T) {
for (command, boxed_handler) in iter {
let name = &command.name;
router.add_route(name, handler);
self.add_route_already_boxed(name, boxed_handler);
}
router
}
}