Result Combinators
Combinator functions for chaining and transforming Result types
Description
Utility functions for working with Rust’s Result<T, E> type. Provides combinators for error handling, transformation, and fallback operations.
Features
- Error mapping and transformation
- Fallback chains
- Early return patterns
- Error accumulation
Code
pub trait ResultExt<T, E> { fn map_err_into<U: From<E>>(self) -> Result<T, U>; fn or_else_ok<F>(self, f: F) -> Result<T, E> where F: FnOnce(E) -> T; fn and_then_async<F, Fut, U, E2>(self, f: F) -> impl std::future::Future<Output = Result<U, E2>> where F: FnOnce(T) -> Fut, Fut: std::future::Future<Output = Result<U, E2>>, E: Into<E2>;}impl<T, E> ResultExt<T, E> for Result<T, E> { fn map_err_into<U: From<E>>(self) -> Result<T, U> { self.map_err(Into::into) } fn or_else_ok<F>(self, f: F) -> Result<T, E> where F: FnOnce(E) -> T, { self.or_else(|e| Ok(f(e))) } fn and_then_async<F, Fut, U, E2>(self, f: F) -> impl std::future::Future<Output = Result<U, E2>> where F: FnOnce(T) -> Fut, Fut: std::future::Future<Output = Result<U, E2>>, E: Into<E2>, { async move { match self { Ok(val) => f(val).await, Err(e) => Err(e.into()), } } }}pub fn combine_results<T, E>(results: Vec<Result<T, E>>) -> Result<Vec<T>, Vec<E>> { let mut values = Vec::new(); let mut errors = Vec::new(); for result in results { match result { Ok(val) => values.push(val), Err(e) => errors.push(e), } } if errors.is_empty() { Ok(values) } else { Err(errors) }}
use result_combinators::{ResultExt, combine_results};// Error conversionlet res: Result<i32, String> = Err("error".to_string());let converted: Result<i32, Box<dyn std::error::Error>> = res.map_err_into();// Fallback valuelet value = res.or_else_ok(|_| 0);// Combine multiple resultslet results = vec![Ok(1), Ok(2), Err("error".to_string())];let combined = combine_results(results);
Comments
No comments yet. Be the first to comment!
Please login to leave a comment.