use std::sync::Arc;
use std::time::Duration;
use entelix_core::transports::RetryPolicy;
use entelix_core::{ExecutionContext, Result};
use crate::configured::Configured;
use crate::fallback::Fallback;
use crate::mapping::Mapping;
use crate::retrying::Retrying;
use crate::runnable::Runnable;
use crate::sequence::RunnableSequence;
use crate::stream::{BoxStream, StreamChunk, StreamMode};
use crate::timed::Timed;
#[async_trait::async_trait]
pub trait RunnableExt<I, O>: Runnable<I, O> + Sized + 'static
where
I: Send + 'static,
O: Send + 'static,
{
fn pipe<P, R>(self, next: R) -> RunnableSequence<I, O, P>
where
P: Send + 'static,
R: Runnable<O, P> + 'static,
{
RunnableSequence::new(Arc::new(self), Arc::new(next))
}
fn with_retry(self, policy: RetryPolicy) -> Retrying<Self, I, O>
where
I: Clone,
{
Retrying::new(self, policy)
}
fn with_fallbacks<F>(self, fallbacks: Vec<F>) -> Fallback<Self, F, I, O>
where
F: Runnable<I, O> + 'static,
I: Clone,
{
Fallback::new(self, fallbacks)
}
fn map<F, P>(self, f: F) -> Mapping<Self, F, I, O, P>
where
F: Fn(O) -> P + Send + Sync + 'static,
P: Send + 'static,
{
Mapping::new(self, f)
}
fn with_config<F>(self, configurer: F) -> Configured<Self, F, I, O>
where
F: Fn(&mut ExecutionContext) + Send + Sync + 'static,
{
Configured::new(self, configurer)
}
fn with_timeout(self, timeout: Duration) -> Timed<Self, I, O> {
Timed::new(self, timeout)
}
async fn stream_with(
&self,
input: I,
mode: StreamMode,
ctx: &ExecutionContext,
) -> Result<BoxStream<'_, Result<StreamChunk<O>>>> {
self.stream(input, mode, ctx).await
}
}
impl<T, I, O> RunnableExt<I, O> for T
where
T: Runnable<I, O> + Sized + 'static,
I: Send + 'static,
O: Send + 'static,
{
}