Expand description
Named CLI commands - Laravel’s Artisan::command('name', $closure),
as a real trait rather than a closure, matching this codebase’s own
established shape for anything dispatched by a stable string tag
(larust_queue::Job::JOB_TYPE, larust_notifications::Notification:: NOTIFICATION_TYPE) rather than introducing a different shape just for
this one case.
Deliberately not built on larust_queue::JobRegistry’s exact
mechanics, even though the shape looks similar at a glance: a Job is
serialized, persisted, and reconstructed by a worker process that may
not be the one that dispatched it - JobRegistry::register::<J>()’s
closure exists specifically to defer deserializing J until a
worker actually claims one. A Command runs synchronously, in the
same process, the moment its name is typed - there’s nothing to
persist or deserialize, so register takes no Default/Deserialize
bound at all, and Command::handle is an associated function (no
&self) rather than an instance method, since a command has no
meaningful state of its own beyond the args it’s handed - the same
reasoning Authenticatable::find_for_auth being a bare associated
function already established for this codebase.
§Example
use larust_console::{Command, CommandRegistry};
use larust_core::AppError;
struct ReportPosts;
impl Command for ReportPosts {
const NAME: &'static str = "report:posts";
const DESCRIPTION: &'static str = "Prints the current post count";
async fn handle(_args: &[String]) -> Result<(), AppError> {
println!("42 posts");
Ok(())
}
}
let registry = CommandRegistry::new().register::<ReportPosts>();
let ran = registry.dispatch("report:posts", &[]).await?;
assert!(ran);Structs§
- Command
Info - A registered command’s name and description, as returned by
CommandRegistry::list- e.g. forxr command:listto print. - Command
Registry - Maps a command name to the handler that runs it - built fresh by
whatever calls
CommandRegistry::dispatch(typically the generated app’s ownmain.rs, viaroutes::console::commands()), not a process-wide static: exactly one process ever reads it, for exactly as long as that onexr <command>invocation runs, so there’s no cross-request sharing need the waylarust-events’ listener registry orlarust_http::route’s named-route registry have.
Traits§
- Command
- A named CLI command, run once, synchronously, in the same process that
dispatched it - see this crate’s own module doc for the full
comparison against
larust_queue::Job.