use std::fmt::Display;
use zrx_scheduler::step::Result;
use zrx_scheduler::Scope;
use crate::stream::function::arguments::{
ForId, ForIdSplat, ForIdValue, ForScope, ForScopeSplat, ForScopeValue,
ForSplat, ForValue,
};
use crate::stream::function::catch;
pub trait FlatMapFn<A, I, T, U, R>: Send + 'static
where
R: IntoIterator<Item = (I, U)>,
{
fn execute(&self, scope: &Scope<I>, value: T) -> Result<R>;
}
impl<F, I, T, U, R> FlatMapFn<ForScope, I, T, U, R> for F
where
F: Fn(&Scope<I>) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, _: T) -> Result<R> {
catch(|| self(scope))
}
}
impl<F, I, T, U, R> FlatMapFn<ForId, I, T, U, R> for F
where
F: Fn(&I) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, _: T) -> Result<R> {
catch(|| self(scope.try_as_id()?))
}
}
impl<F, I, T, U, R> FlatMapFn<ForValue, I, T, U, R> for F
where
F: Fn(T) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, value: T) -> Result<R> {
catch(|| self(value))
}
}
impl<F, I, T, U, R> FlatMapFn<ForScopeValue, I, T, U, R> for F
where
F: Fn(&Scope<I>, T) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, value: T) -> Result<R> {
catch(|| self(scope, value))
}
}
impl<F, I, T, U, R> FlatMapFn<ForIdValue, I, T, U, R> for F
where
F: Fn(&I, T) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, value: T) -> Result<R> {
catch(|| self(scope.try_as_id()?, value))
}
}
macro_rules! impl_flat_map_fn_for_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U, R> FlatMapFn<ForSplat, I, ($($T,)+), U, R>
for F
where
F: Fn($($T),+) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(
level = "debug", skip_all, fields(scope = %scope)
)
)]
#[inline]
fn execute(
&self, scope: &Scope<I>, value: ($($T,)+)
) -> Result<R> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self($($T),+))
}
}
};
}
macro_rules! impl_flat_map_fn_for_scope_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U, R> FlatMapFn<ForScopeSplat, I, ($($T,)+), U, R>
for F
where
F: Fn(&Scope<I>, $($T),+) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(
level = "debug", skip_all, fields(scope = %scope)
)
)]
#[inline]
fn execute(
&self, scope: &Scope<I>, value: ($($T,)+)
) -> Result<R> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self(scope, $($T),+))
}
}
};
}
macro_rules! impl_flat_map_fn_for_id_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U, R> FlatMapFn<ForIdSplat, I, ($($T,)+), U, R>
for F
where
F: Fn(&I, $($T),+) -> Result<R> + Send + 'static,
R: IntoIterator<Item = (I, U)>,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(
level = "debug", skip_all, fields(scope = %scope)
)
)]
#[inline]
fn execute(
&self, scope: &Scope<I>, value: ($($T,)+)
) -> Result<R> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self(scope.try_as_id()?, $($T),+))
}
}
};
}
macro_rules! impl_flat_map_fn {
($($T:ident),+) => {
impl_flat_map_fn_for_splat!($($T),+);
impl_flat_map_fn_for_scope_splat!($($T),+);
impl_flat_map_fn_for_id_splat!($($T),+);
};
}
impl_flat_map_fn!(T1);
impl_flat_map_fn!(T1, T2);
impl_flat_map_fn!(T1, T2, T3);
impl_flat_map_fn!(T1, T2, T3, T4);
impl_flat_map_fn!(T1, T2, T3, T4, T5);
impl_flat_map_fn!(T1, T2, T3, T4, T5, T6);
impl_flat_map_fn!(T1, T2, T3, T4, T5, T6, T7);
impl_flat_map_fn!(T1, T2, T3, T4, T5, T6, T7, T8);