use gc_arena::{Collect, MutationContext};
use crate::{
and_then::{AndThen, AndThenWith},
flatten_result::FlattenOk,
map_result::{MapError, MapOk, MapOkWith},
Sequence,
};
pub trait SequenceResultExt<'gc, I, E>: Sized + Sequence<'gc, Output = Result<I, E>> {
fn map_ok<F, R>(self, f: F) -> MapOk<Self, F>
where
F: 'static + FnOnce(I) -> R,
{
MapOk::new(self, f)
}
fn map_ok_with<C, F, R>(self, c: C, f: F) -> MapOkWith<Self, C, F>
where
F: 'static + FnOnce(C, I) -> R,
{
MapOkWith::new(self, c, f)
}
fn map_err<F, R>(self, f: F) -> MapError<Self, F>
where
F: 'static + FnOnce(E) -> R,
{
MapError::new(self, f)
}
fn and_then<F, R>(self, f: F) -> AndThen<Self, F, I>
where
I: Collect,
F: 'static + FnOnce(MutationContext<'gc, '_>, I) -> Result<R, E>,
{
AndThen::new(self, f)
}
fn and_then_with<C, F, R>(self, c: C, f: F) -> AndThenWith<Self, C, F, I>
where
C: Collect,
I: Collect,
F: 'static + FnOnce(MutationContext<'gc, '_>, C, I) -> Result<R, E>,
{
AndThenWith::new(self, c, f)
}
fn and_chain<F, R, I2>(self, f: F) -> FlattenOk<AndThen<Self, F, I>, R>
where
I: Collect,
F: 'static + FnOnce(MutationContext<'gc, '_>, I) -> Result<R, E>,
R: Sequence<'gc, Output = Result<I2, E>>,
{
FlattenOk::new(AndThen::new(self, f))
}
fn and_chain_with<C, F, R, I2>(self, c: C, f: F) -> FlattenOk<AndThenWith<Self, C, F, I>, R>
where
C: Collect,
I: Collect,
F: 'static + FnOnce(MutationContext<'gc, '_>, C, I) -> Result<R, E>,
R: Sequence<'gc, Output = Result<I2, E>>,
{
FlattenOk::new(AndThenWith::new(self, c, f))
}
fn flatten_ok<I2>(self) -> FlattenOk<Self, I>
where
I: Sequence<'gc, Output = Result<I2, E>>,
{
FlattenOk::new(self)
}
}
impl<'gc, T, I, E> SequenceResultExt<'gc, I, E> for T where T: Sequence<'gc, Output = Result<I, E>> {}