chore: lay out the ground work for a Discord command router

This commit is contained in:
2026-03-23 01:27:40 -04:00
parent abe6ff58ca
commit df6056d549
4 changed files with 115 additions and 23 deletions

72
src/command/mod.rs Normal file
View File

@@ -0,0 +1,72 @@
use std::{
ffi::{CStr, CString},
fmt::Debug,
};
use blart::TreeMap;
use futures::future::BoxFuture;
use twilight_model::application::{
command::Command, interaction::application_command::CommandData,
};
#[derive(Debug, Clone)]
pub struct State {}
type Return = ();
type BoxedHandler = Box<dyn Fn(State, CommandData) -> BoxFuture<'static, Return>>;
fn box_handler<Handler, Fut>(handler: Handler) -> BoxedHandler
where
Fut: Future<Output = Return> + Send + 'static,
Handler: Fn(State, CommandData) -> Fut + 'static,
{
Box::new(move |state, command_data| Box::pin(handler(state, command_data)))
}
pub fn all() -> Vec<(&'static Command, BoxedHandler)> {
vec![]
}
#[derive(Default)]
pub struct Router {
map: TreeMap<CString, BoxedHandler>,
}
impl Router {
fn add_route<'s, 'a, Fut, Handler>(&'s mut self, name: &'a str, handler: Handler)
where
Fut: Future<Output = Return> + Send + 'static,
Handler: Fn(State, CommandData) -> Fut + 'static,
{
self.add_route_already_boxed(name, box_handler(handler));
}
fn add_route_already_boxed<'s, 'a>(&'s mut self, name: &'a str, boxed_handler: BoxedHandler) {
self.map.insert(name.parse().unwrap(), boxed_handler);
}
pub async fn handle(&self, args: State, command_data: CommandData) -> Return {
let name = &command_data.name;
let key = CStr::from_bytes_with_nul(name.as_bytes()).unwrap();
let handler = self
.map
.get(key)
.expect("asked to handle an inexistent command");
handler(args, 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();
for (command, handler) in iter {
let name = &command.name;
router.add_route(name, handler);
}
router
}
}