1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::async_connect::AsyncConnect;
use crate::connect::Connect;
use std::future::Future;

pub type Error = Box<dyn std::error::Error>;

pub trait Linkable {
    type OUT: Send + Sync;
    fn then_async<F, FUT, NXT>(self: Self, f: F) -> AsyncConnect<Self, F>
    where
        F: Fn(Self::OUT) -> FUT,
        FUT: Future<Output = NXT> + Send + Sync,
        Self: Sized,
    {
        AsyncConnect {
            prev: self,
            next: f,
        }
    }

    fn then_async_result<F, FUT, NXT>(self: Self, f: F) -> AsyncConnect<Self, ErrorFuc<F>>
    where
        F: Fn(Self::OUT) -> FUT,
        FUT: Future<Output = Result<NXT, Error>> + Send + Sync,
        Self: Sized,
    {
        AsyncConnect {
            prev: self,
            next: ErrorFuc::new(f),
        }
    }

    fn then<F, NXT>(self: Self, f: F) -> Connect<Self, F>
    where
        F: Fn(Self::OUT) -> NXT,
        Self: Sized,
    {
        Connect {
            prev: self,
            next: f,
        }
    }

    fn then_result<F, NXT>(self: Self, f: F) -> Connect<Self, ErrorFuc<F>>
    where
        F: Fn(Self::OUT) -> Result<NXT, Error>,
        Self: Sized,
    {
        Connect {
            prev: self,
            next: ErrorFuc::new(f),
        }
    }
}

#[async_trait::async_trait]
pub trait Pipeline: Linkable {
    type IN: Send + Sync;
    // todo return Result
    async fn process(self: &Self, input: Self::IN) -> Result<Self::OUT, Error>;
}

pub struct ErrorFuc<F> {
    pub f: F,
}

impl<F> ErrorFuc<F> {
    fn new(f: F) -> Self {
        ErrorFuc { f }
    }
}