cmdreg
English | 中文
A lightweight, string-keyed command dispatcher for Rust with axum-style extractors and optional auto-registration macros.
Designed for applications that dispatch operations by name — such as IPC bridges, plugin systems, FFI layers, or embedded scripting hosts.
Features
- String-keyed dispatch — register and invoke handlers by name (
"fs.read","workspace.open") - Axum-style extractors — use
Json(args)to automatically deserialize arguments from JSON - Sync & async handlers — first-class support for both, in a unified API
#[command]macro — annotate functions to auto-register them (opt-in viamacrosfeature)- Callback commands — register and invoke callback-style handlers
- Zero boilerplate — no traits to implement on your handler functions
Quick Start
[]
= "0.1"
= "1.0"
Register & invoke commands manually
use ;
// Sync handler
reg_command.unwrap;
let result = invoke_command.unwrap;
// Async handler
async
reg_command_async.unwrap;
Auto-registration with #[command] macro
Enable the macros feature:
[]
= { = "0.1", = ["macros"] }
use ;
async
// Without a prefix — registers as "ping"
// At startup:
Feature Flags
| Feature | Default | Description |
|---|---|---|
macros |
off | Enables #[command] macro and reg_all_commands() |
full |
off | Enables all optional features |
How It Works
CommandMap<K, F>— a genericHashMapwrapper keyed by string-like types.- Global registries —
LazyLock<Arc<RwLock<...>>>singletons for sync, async, and callback commands. - Handler traits —
CommandHandler<T>(sync) andCommandHandlerAsync<T>(async) are auto-implemented for functions with up to 10 extractor parameters. - Extractors —
Json<T>deserializesCommandContextinto your typed arguments, similar to axum's extractor pattern. #[command("prefix")]/#[command]— a proc-macro that generates a registration function and submits it toinventoryfor collection at link time. When no prefix is given, the function name is used directly as the command key.
Requirements
- Rust nightly (uses
closure_lifetime_binder) - tokio runtime (for async command dispatch)