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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use futures_util::future::BoxFuture;
use std::future::Future;
use teo_result::{Error, Result};
use crate::arguments::Arguments;
use crate::object::Object;
use crate::pipeline::Ctx;
use super::super::ctx::extract::ExtractFromPipelineCtx;

pub enum TransformResult<T> where T: Into<Object> {
    Object(T),
    Result(Result<T>),
}

impl<T> From<T> for TransformResult<T> where T: Into<Object> {
    fn from(value: T) -> Self {
        TransformResult::Object(value)
    }
}

impl<T, U> From<std::result::Result<T, U>> for TransformResult<T> where T: Into<Object>, U: Into<Error> {
    fn from(result: std::result::Result<T, U>) -> Self {
        match result {
            Ok(t) => TransformResult::Result(Ok(t)),
            Err(err) => TransformResult::Result(Err(err.into())),
        }
    }
}

pub trait TransformArgument<A, O: Into<Object>, R: Into<TransformResult<O>>>: Send + Sync + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, R>;
}

impl<A0, O, F, R, Fut> TransformArgument<(A0,), O, R> for F where
    A0: TryFrom<Object, Error=Error> + Send + Sync,
    F: Fn(A0) -> Fut + Sync + Send + Clone + 'static,
    O: Into<Object> + Sync + Send,
    R: Into<TransformResult<O>> + Send + Sync,
    Fut: Future<Output = R> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, R> {
        let value: A0 = ctx.value().clone().try_into().unwrap();
        Box::pin(self(value))
    }
}

impl<A0, A1, O, F, R, Fut> TransformArgument<(A0, A1), O, R> for F where
    A0: TryFrom<Object, Error=Error> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1) -> Fut + Sync + Send + 'static,
    O: Into<Object> + Sync + Send,
    R: Into<TransformResult<O>> + Send + Sync,
    Fut: Future<Output = R> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, R> {
        let value: A0 = ctx.value().clone().try_into().unwrap();
        let arg1: A1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(value, arg1))
    }
}

impl<A0, A1, A2, O, F, R, Fut> TransformArgument<(A0, A1, A2), O, R> for F where
    A0: TryFrom<Object, Error=Error> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1, A2) -> Fut + Sync + Send + 'static,
    O: Into<Object> + Sync + Send,
    R: Into<TransformResult<O>> + Send + Sync,
    Fut: Future<Output = R> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, R> {
        let value: A0 = ctx.value().clone().try_into().unwrap();
        let arg1: A1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let arg2: A2 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(value, arg1, arg2))
    }
}

impl<A0, A1, A2, A3, O, F, R, Fut> TransformArgument<(A0, A1, A2, A3), O, R> for F where
    A0: TryFrom<Object, Error=Error> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    A3: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1, A2, A3) -> Fut + Sync + Send + 'static,
    O: Into<Object> + Sync + Send,
    R: Into<TransformResult<O>> + Send + Sync,
    Fut: Future<Output = R> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, R> {
        let value: A0 = ctx.value().clone().try_into().unwrap();
        let arg1: A1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let arg2: A2 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let arg3: A3 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(value, arg1, arg2, arg3))
    }
}