WithError

Trait WithError 

Source
pub trait WithError<E> {
    type Success;
    type ErrorOutput<G>;

    // Required methods
    fn fmap_error<F, G>(self, f: F) -> Self::ErrorOutput<G>
       where F: Fn(E) -> G;
    fn to_result(self) -> Result<Self::Success, E>;
}
Expand description

Abstraction over types that carry an error variant which can be remapped.

This trait provides a generic interface for types that contain both success and error cases, allowing transformation of the error type while preserving the success value.

§Type Parameters

  • E - The current error type contained in the implementor

§Associated Types

  • Success - The success value type when no error is present
  • ErrorOutput<G> - The output type after mapping the error to type G

§Examples

use error_rail::traits::WithError;

let result: Result<i32, &str> = Err("original error");
let mapped = result.fmap_error(|e| format!("Error: {}", e));
assert_eq!(mapped, Err("Error: original error".to_string()));

Required Associated Types§

Required Methods§

Source

fn fmap_error<F, G>(self, f: F) -> Self::ErrorOutput<G>
where F: Fn(E) -> G,

Maps the error value using f, producing a new container with error type G.

This operation leaves the success case untouched and only transforms the error.

§Arguments
  • f - A function that transforms the error from type E to type G
§Examples
use error_rail::traits::WithError;

let result: Result<i32, u32> = Err(404);
let mapped = result.fmap_error(|code| format!("HTTP {}", code));
assert_eq!(mapped, Err("HTTP 404".to_string()));
Source

fn to_result(self) -> Result<Self::Success, E>

Converts the container into a Result.

For types that are already Result, this is a no-op. For other types, this extracts the success/error into standard Result form.

§Examples
use error_rail::traits::WithError;

let result: Result<i32, &str> = Ok(42);
assert_eq!(result.to_result(), Ok(42));

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: Clone> WithError<E> for Result<T, E>

Source§

type Success = T

Source§

type ErrorOutput<G> = Result<T, G>

Source§

fn fmap_error<F, G>(self, f: F) -> Self::ErrorOutput<G>
where F: FnOnce(E) -> G,

Source§

fn to_result(self) -> Result<Self::Success, E>

Implementors§

Source§

impl<T, E: Clone> WithError<E> for Validation<E, T>

Implementation of WithError for Validation types.

This allows transforming the error type of a validation while preserving the success value and accumulating all errors through the transformation.

§Examples

use error_rail::traits::WithError;
use error_rail::validation::Validation;

let validation: Validation<&str, i32> = Validation::invalid_many(["err1", "err2"]);
let mapped = validation.fmap_error(|e| format!("Error: {}", e));
assert_eq!(mapped.iter_errors().count(), 2);

let valid: Validation<&str, i32> = Validation::valid(42);
let result = valid.to_result();
assert_eq!(result, Ok(42));