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§
Sourcefn ctx<C: IntoErrorContext>(self, msg: C) -> Result<T, Box<ComposableError<E>>>
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
msg- Any type implementingIntoErrorContext, such as&str,String, orErrorContext
§Examples
use error_rail::ResultExt;
let result: Result<i32, &str> = Err("connection failed");
let enriched = result.ctx("database operation failed");
assert!(enriched.is_err());Sourcefn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
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.