pub trait IntoResult<T, E> {
// Required method
fn into_result(self) -> Result<T, E>;
}
Expand description
Convenience trait to convert tuple of (success: T, errors: Vec<E>)
to a result : Result<T, ErrorTree<L, E>>
Required Methods§
Sourcefn into_result(self) -> Result<T, E>
fn into_result(self) -> Result<T, E>
Turns self
into a Result
.
For tuples of (success: T, errors: Vec<E>)
:
- It checks if
errors
is empty.- If true, it will return
Ok(success)
. - Otherwise it will return
Err(errors)
.
- If true, it will return
struct Error(String);
impl<L> From<Error> for ErrorTree<L, Error> {
fn from(e: Error) -> Self {
Self::leaf(e)
}
}
let result1: Result<(), _> = Err(Error("first".into())).label_error("one");
let result2: Result<(), _> = Err(Error("second".into())).label_error("two");
let final_result: Result<Vec<_>, ErrorTree<_, _>> = vec![result1, result2]
.into_iter()
.partition_result()
.into_result();
For errors: Vec<E>
:
- It checks if
errors
is empty. - If true, it will return
Ok(())
. - Otherwise, it will return
Err(errors)
.
Since the trait is implemented for tuples of (success: T, errors: Vec<E>)
and for Vec<E>
, it works well with partition_result
from the itertools
crate!
struct Error(String);
let error1 = ErrorTree::leaf(Error("first".into())).with_label("one");
let error2 = ErrorTree::leaf(Error("second".into())).with_label("two");
let final_result: Result<_, ErrorTree<_, _>> = vec![error1, error2]
.into_result();