Skip to main content

Crate larust_console

Crate larust_console 

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

CommandInfo
A registered command’s name and description, as returned by CommandRegistry::list - e.g. for xr command:list to print.
CommandRegistry
Maps a command name to the handler that runs it - built fresh by whatever calls CommandRegistry::dispatch (typically the generated app’s own main.rs, via routes::console::commands()), not a process-wide static: exactly one process ever reads it, for exactly as long as that one xr <command> invocation runs, so there’s no cross-request sharing need the way larust-events’ listener registry or larust_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.