ErrorCategory

Trait ErrorCategory 

Source
pub trait ErrorCategory<E> {
    type ErrorFunctor<T>: WithError<E>;

    // Required methods
    fn lift<T>(value: T) -> Self::ErrorFunctor<T>;
    fn handle_error<T>(error: E) -> Self::ErrorFunctor<T>;
}
Expand description

Trait for types that can lift values and handle errors in a functorial way.

This trait provides a category-theoretic abstraction for error handling, allowing types to be lifted into an error-handling context and errors to be properly propagated.

§Type Parameters

  • E - The error type that this category handles

§Associated Types

  • ErrorFunctor<T> - The functor type that wraps values of type T with error handling

§Examples

use error_rail::traits::ErrorCategory;

fn example<E>() {
    let success: Result<i32, E> = <Result<(), E> as ErrorCategory<E>>::lift(42);
    assert!(success.is_ok());
}

Required Associated Types§

Source

type ErrorFunctor<T>: WithError<E>

The functor type that provides error handling for values of type T.

Required Methods§

Source

fn lift<T>(value: T) -> Self::ErrorFunctor<T>

Lifts a pure value into the error-handling context.

§Arguments
  • value - The value to lift into the functor
§Returns

The value wrapped in a successful error-handling context.

Source

fn handle_error<T>(error: E) -> Self::ErrorFunctor<T>

Creates an error-handling context representing a failure.

§Arguments
  • error - The error to wrap
§Returns

An error-handling context containing the error.

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<E> ErrorCategory<E> for Result<(), E>

Source§

type ErrorFunctor<T> = Result<T, E>

Source§

fn lift<T>(value: T) -> Result<T, E>

Source§

fn handle_error<T>(error: E) -> Result<T, E>

Implementors§

Source§

impl<E> ErrorCategory<E> for Validation<E, ()>

Implementation of ErrorCategory for Validation types.

This allows Validation<E, ()> to act as an error category, where:

  • lift creates Valid values
  • handle_error creates Invalid values with a single error

§Examples

use error_rail::traits::ErrorCategory;
use error_rail::validation::Validation;

let valid: Validation<String, i32> = <Validation<String, ()>>::lift(42);
assert!(valid.is_valid());

let invalid: Validation<String, i32> = <Validation<String, ()>>::handle_error("error".to_string());
assert!(invalid.is_invalid());