Skip to main content

CommandResult

Type Alias CommandResult 

Source
pub type CommandResult<T = ()> = Result<T>;
Expand description

Result type for Clawless commands

Commands in Clawless execute a piece of logic that might fail for various reasons. If such an error is unrecoverable during the execution of the command, it will cause the CLI to fail and exit with an error message.

To make it easier to handle errors when implementing commands, every command handler returns a CommandResult type. This makes it possible to use the question mark ? operator and return early when an unrecoverable error occurs.

The CommandResult is a type alias for anyhow::Result<T>, which provides a more ergonomic way to handle arbitrary errors. Since it isn’t possible to recover from the error, we do not need to provide a specific error type that a caller could handle gracefully.

The success type defaults to (). A command that returns Ok(()) ends the process with the exit code 0, and a command that returns an error ends it with the exit code 1 and the text of the error. A command that chooses its exit code and its text returns an Exit instead, as CommandResult<Exit>.

§Examples

use std::process::ExitCode;

use clawless_cli::error::CommandResult;
use clawless_core::exit::Exit;

fn check(major_version: u64) -> CommandResult<Exit> {
    if major_version == 0 {
        return Ok(Exit::builder()
            .code(ExitCode::from(3))
            .text("error: the version is not a stable release\n")
            .build());
    }

    Ok(Exit::from(()))
}

Aliased Type§

pub enum CommandResult<T = ()> {
    Ok(T),
    Err(Error),
}

Variants§

§1.0.0

Ok(T)

Contains the success value

§1.0.0

Err(Error)

Contains the error value