Strange use of Result Types

Open
#100 0 comments 35 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
cli

Research direction

Start in source/foundation/commands.rs at the Serve::handle implementation cited in the issue. Trace how command handlers represent exit codes and errors, including how console.error is rendered and how callers handle the result. Decide whether this branch should return an error and whether the result type should change; done means the behavior is consistent with the command conventions and covered by relevant tests.

Written by the indexing model from the issue text.

Description

I just looked at this code in source/foundation/commands.rs:51

#[crate::console::command(long_running)]
impl Serve {
    async fn handle(self, app: &Application, console: &mut Console) -> crate::Result<u8> {
        if console.is_captured() {
            console.error("the `serve` command requires the application's binary; use HTTP requests to test the application in-process");
            return Ok(1);
        }

        let host = self.host.unwrap_or_else(|| app.config().server.host.clone());
        let port = self.port.unwrap_or_else(|| app.config().server.port);

        use crate::ErrorContext as _;

        app.clone()
            .serve_with_output((host.as_str(), port), console)
            .await
            .with_context(|| format!("failed to serve on `{host}:{port}`"))?;

        Ok(0)
    }
}

And I wonder, is there any reason to use Ok(0) and Ok(1) instead of Ok(()) and Err(<the stuff thats in console.error>) to handle this, or at least Ok(<u8>) and Err() for the console.error path? It feels like its a 1:1 port of PHP code without harnessing the extreme power of the type system. So people believe me, I even asked a clanker to validate my observation:

The signature Result<u8> is defensible in general, for commands where a non-zero exit is a legitimate outcome rather than an error (grep, diff, check).

The usage in this code is not. console.error(...); return Ok(1); is an error disguised as a success. It throws away the error value, and the in-process caller, which is the only one that can reach that branch, gets a bare 1 instead of something it can inspect or propagate. The only thing it buys is control over how the message is formatted, and that problem belongs in the error renderer.

That branch should return Err. Unless other commands need non-zero success codes, the signature should be crate::Result<()>.

Just my observation after a few seconds in the code base. I really love the idea to get to use a modern language with a type system that actually helps building stuff. But we should also be careful not to just let AI do 1:1 ports from PHP so it just re-implements all the weird stuff from PHP in Rust.

I am very happy to see this direction and I'm really looking forward to the new era.

Dominant language
Rust
Stars
2.2k
Forks
105
Avg merge
7h 19m
Merged PRs (30d)
41

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust

All issues in laravel/rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.