ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T, E> {
    // Required methods
    fn ctx<C: IntoErrorContext>(
        self,
        msg: C,
    ) -> Result<T, Box<ComposableError<E>>>;
    fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
       where F: FnOnce() -> String;
}
Expand description

Extension trait for ergonomic context addition to Result types.

This trait provides methods for enriching errors with contextual information, wrapping them in a ComposableError for structured error handling.

§Examples

use error_rail::{ResultExt, ErrorContext};

fn read_config() -> Result<String, Box<error_rail::ComposableError<std::io::Error>>> {
    std::fs::read_to_string("config.toml")
        .ctx("failed to read configuration file")
}

fn parse_config() -> Result<i32, Box<error_rail::ComposableError<&'static str>>> {
    Err("invalid format")
        .ctx_with(|| format!("parsing failed at line {}", 42))
}

Required Methods§

Source

fn ctx<C: IntoErrorContext>(self, msg: C) -> Result<T, Box<ComposableError<E>>>

Adds a static context message to the error.

Wraps the error in a ComposableError with the provided context. Use this when the context message is cheap to construct or already available.

§Arguments
§Examples
use error_rail::ResultExt;

let result: Result<i32, &str> = Err("connection failed");
let enriched = result.ctx("database operation failed");
assert!(enriched.is_err());
Source

fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
where F: FnOnce() -> String,

Adds a lazily-evaluated context message to the error.

The context is only computed if the result is an error, making this suitable for expensive context generation (e.g., formatting, I/O).

§Arguments
  • f - A closure that produces the context string when called
§Examples
use error_rail::ResultExt;

let result: Result<i32, &str> = Err("timeout");
let enriched = result.ctx_with(|| format!("request failed after {} retries", 3));
assert!(enriched.is_err());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt<T, E> for Result<T, E>

Source§

fn ctx<C: IntoErrorContext>(self, msg: C) -> Result<T, Box<ComposableError<E>>>

Source§

fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
where F: FnOnce() -> String,

Implementors§