pub struct CommandRegistry<Ctx: ?Sized> { /* private fields */ }Expand description
Registry of slash command handlers.
Handlers are stored in a Vec, not a HashMap, because command count is small (< 40)
and registration happens once at agent initialization. Dispatch performs a linear scan
with longest-word-boundary match to support subcommands.
§Dispatch
See CommandRegistry::dispatch for the full dispatch algorithm.
§Borrow splitting
When stored as an Agent<C> field, the dispatch call site uses std::mem::take to
temporarily move the registry out of the agent, construct a context, dispatch, and
restore the registry. This avoids borrow-checker conflicts.
Implementations§
Source§impl<Ctx: ?Sized> CommandRegistry<Ctx>
impl<Ctx: ?Sized> CommandRegistry<Ctx>
Sourcepub fn register(&mut self, handler: impl CommandHandler<Ctx> + 'static)
pub fn register(&mut self, handler: impl CommandHandler<Ctx> + 'static)
Sourcepub async fn dispatch(
&self,
ctx: &mut Ctx,
input: &str,
) -> Option<Result<CommandOutput, CommandError>>
pub async fn dispatch( &self, ctx: &mut Ctx, input: &str, ) -> Option<Result<CommandOutput, CommandError>>
Dispatch a command string to the matching handler.
Returns None if the input does not start with / or no handler matches.
§Algorithm
- Return
Noneifinputdoes not start with/. - Find all handlers where
input == nameorinput.starts_with(name + " "). - Pick the handler with the longest matching name (subcommand resolution).
- Extract
args = input[name.len()..].trim(). - Call
handler.handle(ctx, args)and return the result.
§Errors
Returns Some(Err(_)) when the matched handler returns an error.
Sourcepub fn find_handler(&self, input: &str) -> Option<(usize, &'static str)>
pub fn find_handler(&self, input: &str) -> Option<(usize, &'static str)>
Find the handler that would be selected for the given input, without dispatching.
Returns Some((idx, name)) or None if no handler matches.
Primarily used in tests to verify routing.
Sourcepub fn list(&self) -> Vec<CommandInfo>
pub fn list(&self) -> Vec<CommandInfo>
List all registered commands for /help generation.
Returns metadata in registration order.