Skip to main content

Crate okerrr

Crate okerrr 

Source
Expand description

§okerrr

A no_std declarative macro for dispatching a Result::Err payload through diverging case clauses.

§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, case error => return Err(error));
    Ok(value * 2)
}

The Ok payload continues in the surrounding scope. Each case pattern matches the raw Err payload, and every handler must diverge. A handler can return, break, continue, panic, loop forever, or call another never-returning expression.

The clause-oriented style takes inspiration from Elixir’s case control flow. Patterns, if guards, exhaustiveness, ownership, and divergence keep their Rust semantics. Unlike a closure fallback, control-flow expressions act on the surrounding function or loop. The input is evaluated exactly once.

An expression containing .await works when the invocation is already in an async context; the macro does not await implicitly.

§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§

okerrr
Extracts an Ok payload or dispatches the raw error through diverging cases.