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 presentErrorOutput<G>- The output type after mapping the error to typeG
§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§
type Success
type ErrorOutput<G>
Required Methods§
Sourcefn fmap_error<F, G>(self, f: F) -> Self::ErrorOutput<G>where
F: Fn(E) -> G,
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 typeEto typeG
§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()));Sourcefn to_result(self) -> Result<Self::Success, E>
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§
Implementors§
Source§impl<T, E> WithError<E> for Validation<E, T>
Implementation of WithError for Validation types.
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(["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));