hatch_result

Crates.iohatch_result
lib.rshatch_result
version0.1.8
sourcesrc
created_at2023-01-28 19:29:31.179251
updated_at2023-02-01 18:30:24.246254
descriptionA result-like struct that returns on Ok, instead of on Err when ? operator is used
homepage
repositoryhttps://github.com/giluis/hatch_result
max_upload_size
id770541
size7,514
Luís Gil (giluis)

documentation

README

Hatch Result

A wrapper over std::result::Result that returns on Ok instead of Err when the ? operator is used.

The builtin Result's implementation of the ? operator mimics the short circuiting on a logical and: if one operation returns an error, immediately return from the function. If not, proceed to the next statements.

HatchResult's implementation of the ? operator mimics the short circuiting on a logical or: if one operation returns an Ok, immediately return from the function. If not, proceed to the next statements.

This crate also implements a hatch method on the builtin Result type that wraps the value in a HatchResult.

Examples

Typical use case

fn operation1() -> Result<u32, String> {
    Ok(4)
    // Err("Some error occurred")

}

fn operation2() -> Result<u32, String> {
    Ok(4)
    // Err("Some error occurred")
}

fn exit_early_if_possible() -> Result<u32, String> {
    let err1 = operation1().hatch()?;
    let err2 = operation2().hatch()?;
    handle_errors(err1,err2)
}

fn handle_errors(err1: String, err2: String) -> Result<u32, String> {
    Err(format!("Both operations failed:\n\top1: {err1}\n\top2: {err2}"))
}

Improves on matching

HatchResult is more concise (but equivalent) to the traditional approach:

fn exit_early_if_possible() -> Result<u32, String> {
    let err = match operation1() {
        Err(error) => error,
        ok_result => return ok_result
    };
    // ... 
}

Difference between regular Result and HatchResult

fn regular_result() -> Result<u32, String> {
    let value: u32 = Result::<u32, String>::Ok(4)?;
    Ok(value)
}

fn hatch_result() -> Result<u32, String> {
    let err: String = HatchResult::<u32, String>(Ok(3))?;
    Err(err)
}

Converting from Result

The hatch method "converts" a result to a HatchResult.
This allows you to exit early on an Ok result or handle the error.

fn operation_that_might_fail() -> Result<u32, String> {
    let result = // ... some computation
    result
}

fn hatch_result() -> Result<u32, String> {
    let error = operation_that_might_fail().hatch()?;
    panic!("There was an error: {error}");
}
Commit count: 27

cargo fmt