Skip to main content

Crate okerrr

Crate okerrr 

Source
Expand description

§okerrr

Declarative macros for handling Result and Option without repeating a match at every call site. Use okerrr! and okerrr_some! in new code; okerr! and okerr_some! are convenience spellings of the same macros.

§The pattern

A match lets an error handler bind its payload and return from the caller:

fn process(input: Result<i32, &'static str>) -> Result<i32, &'static str> {
    let value = match input {
        Ok(value) => value,
        Err(error) => return Err(error),
    };
    Ok(value * 2)
}

okerrr! keeps that behavior at the call site with less repeated structure:

use okerrr::okerrr;

fn process(input: Result<i32, &'static str>) -> Result<i32, &'static str> {
    let value = okerrr!(input, error => return Err(error));
    Ok(value * 2)
}

if let remains useful when only one branch matters. Here, binding the Err payload and using return, break, or continue in the caller are the reasons to use the macro instead of a closure-based fallback.

§Caller-side instrumentation

The macro does not log or require an error formatting trait. An error handler can call tracing::error! when that caller chooses to record the error. Subscriber and OpenTelemetry export setup belong to the application.

Macros§

okerr
Convenience alias for okerrr!. Prefer okerrr! in examples and new code.
okerr_some
Convenience alias for okerrr_some!. Prefer the canonical spelling in examples and new code.
okerrr
Canonical macro for handling Result<T, E> at the call site.
okerrr_some
Canonical macro for handling Option<T> at the call site.