use std::marker::PhantomData;
use std::sync::Arc;
use async_trait::async_trait;
use entelix_core::ExecutionContext;
use entelix_core::error::Result;
use crate::runnable::Runnable;
pub struct Configured<R, F, I, O>
where
R: Runnable<I, O> + 'static,
F: Fn(&mut ExecutionContext) + Send + Sync + 'static,
I: Send + 'static,
O: Send + 'static,
{
inner: Arc<R>,
configurer: Arc<F>,
_io: PhantomData<fn(I) -> O>,
}
impl<R, F, I, O> Configured<R, F, I, O>
where
R: Runnable<I, O> + 'static,
F: Fn(&mut ExecutionContext) + Send + Sync + 'static,
I: Send + 'static,
O: Send + 'static,
{
pub fn new(inner: R, configurer: F) -> Self {
Self {
inner: Arc::new(inner),
configurer: Arc::new(configurer),
_io: PhantomData,
}
}
}
impl<R, F, I, O> std::fmt::Debug for Configured<R, F, I, O>
where
R: Runnable<I, O> + 'static,
F: Fn(&mut ExecutionContext) + Send + Sync + 'static,
I: Send + 'static,
O: Send + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Configured").finish_non_exhaustive()
}
}
#[async_trait]
impl<R, F, I, O> Runnable<I, O> for Configured<R, F, I, O>
where
R: Runnable<I, O> + 'static,
F: Fn(&mut ExecutionContext) + Send + Sync + 'static,
I: Send + 'static,
O: Send + 'static,
{
async fn invoke(&self, input: I, ctx: &ExecutionContext) -> Result<O> {
let mut scoped = ctx.clone();
(self.configurer)(&mut scoped);
self.inner.invoke(input, &scoped).await
}
}