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_first(self) -> Result<Self::Success, E>;
    fn to_result_all(self) -> Result<Self::Success, ErrorVec<E>>;
}
Expand description

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

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.

Source

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

Converts the container into a Result, taking only the first error if invalid.

Source

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

Converts the container into a Result, preserving all errors if invalid.

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> 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_first(self) -> Result<Self::Success, E>

Source§

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

Implementors§

Source§

impl<T, E> 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(vec!["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));