laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Write commands for jobs you run from a terminal or a scheduler, such as a nightly import or a
//! one-off cleanup.
//!
//! [`macro@command`] turns an async function or a clap struct into a [`Command`], which you register
//! in [`Commands`]. While the command runs, its [`Console`] writes output, prompts for input, shows
//! progress, and traps signals. To learn more, check out the
//! [console guide](https://github.com/laravel/rust-framework/blob/0.x/docs/console.md).
//!
//! ```
//! use laravel::console::{Console, clap};
//! use laravel::container::Inject;
//!
//! struct Greetings {
//!     salutation: &'static str,
//! }
//!
//! /// Greet someone by name.
//! #[derive(clap::Parser)]
//! #[command(name = "greet")]
//! struct Greet {
//!     /// The person to greet.
//!     name: String,
//! }
//!
//! #[laravel::console::command]
//! impl Greet {
//!     async fn handle(self, console: &mut Console, greetings: Inject<Greetings>) -> laravel::Result<()> {
//!         console.info(format!("{}, {}!", greetings.salutation, self.name));
//!
//!         Ok(())
//!     }
//! }
//!
//! let app = laravel::Application::configure()
//!     .replace_env(laravel::config::Env::default())
//!     .with_services(|services| { services.instance(Greetings { salutation: "Hello" }); })
//!     .with_commands(|commands, _| { commands.add::<Greet>(); })
//!     .create();
//!
//! laravel::testing::__run(async {
//!     use laravel::testing::MakesConsoleRequests;
//!
//!     app.command("greet Taylor")
//!         .await
//!         .assert_successful()
//!         .assert_error_output_contains("Hello, Taylor!");
//! });
//! ```
//!
//! A command that returns `()` or `Ok(())` exits with status `0`, and one that returns a `u8`
//! exits with that status. When a command returns an error or panics, the console prints the
//! message and its causes to stderr, joined with `: `. Laravel then reports the error through the
//! application's error handler, and the command exits with status `1`. The built-in `stdout`
//! and `stderr` log channels skip an error that the console already printed.
//!
//! Invalid arguments exit with status `2`, and a returned [`PromptError::Cancelled`] exits with
//! status `130`. When nothing reads the output, as in `route:list | head`, the command stops
//! without printing an error and exits with status `141`, the status that `SIGPIPE` gives.
//!
//! With `--json`, an empty listing, such as `route:list --json`, writes `[]` to stdout. Without
//! `--json`, an empty listing writes a message to stderr and nothing to stdout.

#[doc(hidden)]
pub use laravel_console::console::__Registration;
pub(crate) use laravel_console::console::*;
#[doc(inline)]
pub use laravel_console::console::{
    Choice, ChoicePrompt, Command, CommandResult, Commands, Console, MultiselectPrompt, PasswordPrompt, Progress,
    ProgressGroup, ProgressUpdate, Prompt, PromptError, Shutdown, Signal, Signals, Suggest, Verbosity, clap, command,
    commands, escape_markup,
};
#[cfg(test)]
mod tests {
    use super::*;
    use crate::Result;
    use crate::foundation::StartupError;
    use crate::testing::MakesConsoleRequests;

    /// Say which registration ran.
    #[command("discovered:probe")]
    async fn probe(console: &mut Console) -> Result<()> {
        console.line("discovered");
        Ok(())
    }

    #[derive(clap::Parser)]
    #[command(name = "discovered:probe")]
    struct Replacement;

    #[command]
    impl Replacement {
        async fn handle(self, console: &mut Console) -> Result<()> {
            console.line("application");
            Ok(())
        }
    }

    inventory::submit! {
        __Registration {
            crate_name: "app",
            name: || "app::probe",
            definition: || <probe as clap::CommandFactory>::command(),
            register: |commands| { commands.add::<probe>(); },
        }
    }

    #[tokio::test]
    async fn app_commands_are_discovered_before_application_callbacks() {
        let discovered = crate::Application::configure()
            .replace_env(crate::config::Env::default())
            .create();
        discovered
            .command("discovered:probe")
            .await
            .assert_successful()
            .assert_output_contains("discovered");

        let replaced = crate::Application::configure()
            .replace_env(crate::config::Env::default())
            .with_commands(|commands, _| {
                commands.add::<Replacement>();
            })
            .create();
        replaced
            .command("discovered:probe")
            .await
            .assert_successful()
            .assert_output_contains("application");
    }

    inventory::submit! {
        __Registration {
            crate_name: "conflicting_package",
            name: || "conflicting_package::first",
            definition: || <probe as clap::CommandFactory>::command(),
            register: |commands| { commands.add::<probe>(); },
        }
    }
    inventory::submit! {
        __Registration {
            crate_name: "conflicting_package",
            name: || "conflicting_package::second",
            definition: || <Replacement as clap::CommandFactory>::command(),
            register: |commands| { commands.add::<Replacement>(); },
        }
    }

    #[test]
    fn two_discovered_commands_with_one_name_stop_the_application() {
        let error = crate::Application::configure()
            .replace_env(crate::config::Env::default())
            .with_commands(|commands, _| {
                commands.discover("conflicting_package").discover("conflicting_package");
            })
            .try_create()
            .unwrap_err();

        assert_eq!(
            error,
            StartupError::Commands(
                "command `discovered:probe` is registered by both `conflicting_package::first` and `conflicting_package::second`"
                    .to_owned()
            )
        );
    }
}