ResultIntoContext

Trait ResultIntoContext 

Source
pub trait ResultIntoContext: ResultExt {
    // Required methods
    fn into_ctx<C2: ThinContext>(self) -> Result<Self::Ok, Report<C2>>;
    fn and_then_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
       where C2: ThinContext,
             F: FnOnce(Self::Ok) -> Result<U, Report<C2>>;
    fn map_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
       where C2: ThinContext,
             F: FnOnce(Self::Ok) -> U;
}
Expand description

Extension trait for Result<T, Report<C>> that provides context conversion methods.

Required Methods§

Source

fn into_ctx<C2: ThinContext>(self) -> Result<Self::Ok, Report<C2>>

Convert the error context type of this result.

This method transforms a Result<T, E> into a Result<T, Report<C>>, converting any error type into a report with C being a ThinContext

§Example
use bigerror::{ResultIntoContext, ThinContext, Report};

#[derive(ThinContext)]
struct MyParseError;

fn parse_data() -> Result<i32, Report<MyParseError>> {
    "42".parse().into_ctx() // Converts ParseIntError to Report<MyParseError>
}

assert_eq!(parse_data().unwrap(), 42);
Source

fn and_then_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
where C2: ThinContext, F: FnOnce(Self::Ok) -> Result<U, Report<C2>>,

Chain operations while converting error context (similar to Result::and_then).

This method allows you to chain operations that might fail, automatically converting any errors from the current result to the target context type before passing the success value to the closure. This is equivalent to Result::and_then but with automatic error context conversion.

§Example
use bigerror::{
    DbError, NotFound, OptionReport, Report, ResultIntoContext, expect_field,
};

fn find_user(id: u64) -> Result<User, Report<NotFound>> {
    Some(User {
        id,
        email: Some("user@example.com".to_string()),
    })
    .expect_or()
}
struct User {
    id: u64,
    email: Option<String>,
}

fn find_user_and_get_email(user_id: u64) -> Result<String, Report<DbError>> {
    // Simulate finding a user (might return NotFound)
    find_user(user_id).and_then_ctx(|user| {
        // Extract email from user
        expect_field!(user.email).into_ctx()
    })
}
Source

fn map_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
where C2: ThinContext, F: FnOnce(Self::Ok) -> U,

Map the success value while converting error context (similar to Result::map).

This method transforms the success value of a result using the provided closure, while automatically converting any errors to the target context type. This is equivalent to Result::map but with automatic error context conversion.

§Example
use std::path::Path;
use bigerror::{ResultIntoContext, Report, FsError};

fn count_lines_in_file(path: &Path) -> Result<usize, Report<FsError>> {
    std::fs::read_to_string(path)
        .map_ctx(|content| content.lines().count()) // Count lines and do conversion
}

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: IntoReport> ResultIntoContext for Result<T, E>
where <E as IntoReport>::Context: Sized + 'static,

Source§

fn into_ctx<C2: ThinContext>(self) -> Result<T, Report<C2>>

Source§

fn and_then_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
where C2: ThinContext, F: FnOnce(T) -> Result<U, Report<C2>>,

Source§

fn map_ctx<U, F, C2>(self, op: F) -> Result<U, Report<C2>>
where C2: ThinContext, F: FnOnce(T) -> U,

Implementors§