Skip to main content

CommandRegistry

Struct CommandRegistry 

Source
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>

Source

pub fn new() -> Self

Create an empty registry.

Source

pub fn register(&mut self, handler: impl CommandHandler<Ctx> + 'static)

Register a command handler.

§Panics

Panics if a handler with the same name is already registered.

Source

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
  1. Return None if input does not start with /.
  2. Find all handlers where input == name or input.starts_with(name + " ").
  3. Pick the handler with the longest matching name (subcommand resolution).
  4. Extract args = input[name.len()..].trim().
  5. Call handler.handle(ctx, args) and return the result.
§Errors

Returns Some(Err(_)) when the matched handler returns an error.

Source

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.

Source

pub fn list(&self) -> Vec<CommandInfo>

List all registered commands for /help generation.

Returns metadata in registration order.

Trait Implementations§

Source§

impl<Ctx: ?Sized> Default for CommandRegistry<Ctx>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Ctx> Freeze for CommandRegistry<Ctx>
where Ctx: ?Sized,

§

impl<Ctx> !RefUnwindSafe for CommandRegistry<Ctx>

§

impl<Ctx> Send for CommandRegistry<Ctx>
where Ctx: ?Sized,

§

impl<Ctx> Sync for CommandRegistry<Ctx>
where Ctx: ?Sized,

§

impl<Ctx> Unpin for CommandRegistry<Ctx>
where Ctx: ?Sized,

§

impl<Ctx> UnsafeUnpin for CommandRegistry<Ctx>
where Ctx: ?Sized,

§

impl<Ctx> !UnwindSafe for CommandRegistry<Ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.