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 MapFn<A, I, T, U>: Send + 'static {
fn execute(&self, scope: &Scope<I>, value: T) -> Result<U>;
}
impl<F, I, T, U> MapFn<ForScope, I, T, U> for F
where
F: Fn(&Scope<I>) -> Result<U> + Send + 'static,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, _: T) -> Result<U> {
catch(|| self(scope))
}
}
impl<F, I, T, U> MapFn<ForId, I, T, U> for F
where
F: Fn(&I) -> Result<U> + Send + 'static,
I: Display,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
)]
#[inline]
fn execute(&self, scope: &Scope<I>, _: T) -> Result<U> {
catch(|| self(scope.try_as_id()?))
}
}
impl<F, I, T, U> MapFn<ForValue, I, T, U> for F
where
F: Fn(T) -> Result<U> + Send + 'static,
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<U> {
catch(|| self(value))
}
}
impl<F, I, T, U> MapFn<ForScopeValue, I, T, U> for F
where
F: Fn(&Scope<I>, T) -> Result<U> + Send + 'static,
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<U> {
catch(|| self(scope, value))
}
}
impl<F, I, T, U> MapFn<ForIdValue, I, T, U> for F
where
F: Fn(&I, T) -> Result<U> + Send + 'static,
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<U> {
catch(|| self(scope.try_as_id()?, value))
}
}
macro_rules! impl_map_fn_for_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U> MapFn<ForSplat, I, ($($T,)+), U> for F
where
F: Fn($($T),+) -> Result<U> + Send + 'static,
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<U> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self($($T),+))
}
}
};
}
macro_rules! impl_map_fn_for_scope_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U> MapFn<ForScopeSplat, I, ($($T,)+), U> for F
where
F: Fn(&Scope<I>, $($T),+) -> Result<U> + Send + 'static,
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<U> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self(scope, $($T),+))
}
}
};
}
macro_rules! impl_map_fn_for_id_splat {
($($T:ident),+) => {
impl<F, I, $($T,)+ U> MapFn<ForIdSplat, I, ($($T,)+), U> for F
where
F: Fn(&I, $($T),+) -> Result<U> + Send + 'static,
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<U> {
#[allow(non_snake_case)]
let ($($T,)+) = value;
catch(|| self(scope.try_as_id()?, $($T),+))
}
}
};
}
macro_rules! impl_map_fn {
($($T:ident),+) => {
impl_map_fn_for_splat!($($T),+);
impl_map_fn_for_scope_splat!($($T),+);
impl_map_fn_for_id_splat!($($T),+);
};
}
impl_map_fn!(T1);
impl_map_fn!(T1, T2);
impl_map_fn!(T1, T2, T3);
impl_map_fn!(T1, T2, T3, T4);
impl_map_fn!(T1, T2, T3, T4, T5);
impl_map_fn!(T1, T2, T3, T4, T5, T6);
impl_map_fn!(T1, T2, T3, T4, T5, T6, T7);
impl_map_fn!(T1, T2, T3, T4, T5, T6, T7, T8);