async_pipeline/
link.rs

1use crate::async_connect::AsyncConnect;
2use crate::connect::Connect;
3use std::future::Future;
4
5pub type Error = Box<dyn std::error::Error>;
6
7pub trait Linkable {
8    type OUT: Send + Sync;
9    fn then_async<F, FUT, NXT>(self: Self, f: F) -> AsyncConnect<Self, F>
10    where
11        F: Fn(Self::OUT) -> FUT,
12        FUT: Future<Output = NXT> + Send + Sync,
13        Self: Sized,
14    {
15        AsyncConnect {
16            prev: self,
17            next: f,
18        }
19    }
20
21    fn then_async_result<F, FUT, NXT>(self: Self, f: F) -> AsyncConnect<Self, ErrorFuc<F>>
22    where
23        F: Fn(Self::OUT) -> FUT,
24        FUT: Future<Output = Result<NXT, Error>> + Send + Sync,
25        Self: Sized,
26    {
27        AsyncConnect {
28            prev: self,
29            next: ErrorFuc::new(f),
30        }
31    }
32
33    fn then<F, NXT>(self: Self, f: F) -> Connect<Self, F>
34    where
35        F: Fn(Self::OUT) -> NXT,
36        Self: Sized,
37    {
38        Connect {
39            prev: self,
40            next: f,
41        }
42    }
43
44    fn then_result<F, NXT>(self: Self, f: F) -> Connect<Self, ErrorFuc<F>>
45    where
46        F: Fn(Self::OUT) -> Result<NXT, Error>,
47        Self: Sized,
48    {
49        Connect {
50            prev: self,
51            next: ErrorFuc::new(f),
52        }
53    }
54}
55
56#[async_trait::async_trait]
57pub trait Pipeline: Linkable {
58    type IN: Send + Sync;
59    // todo return Result
60    async fn process(self: &Self, input: Self::IN) -> Result<Self::OUT, Error>;
61}
62
63pub struct ErrorFuc<F> {
64    pub f: F,
65}
66
67impl<F> ErrorFuc<F> {
68    fn new(f: F) -> Self {
69        ErrorFuc { f }
70    }
71}