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
use std::future::Future;
use futures_util::future::BoxFuture;
use teo_teon::Value;
use crate::pipeline::Ctx;
use crate::pipeline::item::validator::ValidateResult;
use teo_result::Error;
use crate::arguments::Arguments;
use crate::pipeline::ctx::extract::ExtractFromPipelineCtx;

pub trait CompareArgument<A, O: Into<ValidateResult>, E: Into<Error> + std::error::Error>: Send + Sync + 'static {
    fn call(&self, old: Value, new: Value, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O>;
}

impl<V1, V2, O, F, Fut, E> CompareArgument<(V1, V2), O, E> for F where
    V1: TryFrom<Value, Error=E> + Send + Sync,
    V2: TryFrom<Value, Error=E> + Send + Sync,
    O: Into<ValidateResult> + Send + Sync,
    F: Fn(V1, V2) -> Fut + Sync + Send + 'static,
    E: Into<Error> + std::error::Error,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, old: Value, new: Value, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        let old: V1 = old.try_into().unwrap();
        let new: V2 = new.try_into().unwrap();
        Box::pin(self(old, new))
    }
}

impl<V1, V2, A1, O, F, E, Fut> CompareArgument<(V1, V2, A1), O, E> for F where
    V1: TryFrom<Value, Error=E> + Send + Sync,
    V2: TryFrom<Value, Error=E> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    O: Into<ValidateResult> + Send + Sync,
    F: Fn(V1, V2, A1) -> Fut + Sync + Send + 'static,
    E: Into<Error> + std::error::Error,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, old: Value, new: Value, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        let old: V1 = old.try_into().unwrap();
        let new: V2 = new.try_into().unwrap();
        let a1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(old, new, a1))
    }
}

impl<V1, V2, A1, A2, O, F, E, Fut> CompareArgument<(V1, V2, A1, A2), O, E> for F where
    V1: TryFrom<Value, Error=E> + Send + Sync,
    V2: TryFrom<Value, Error=E> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    O: Into<ValidateResult> + Send + Sync,
    F: Fn(V1, V2, A1, A2) -> Fut + Sync + Send + 'static,
    E: Into<Error> + std::error::Error,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, old: Value, new: Value, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        let old: V1 = old.try_into().unwrap();
        let new: V2 = new.try_into().unwrap();
        let a1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let a2 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(old, new, a1, a2))
    }
}

impl<V1, V2, A1, A2, A3, O, F, E, Fut> CompareArgument<(V1, V2, A1, A2, A3), O, E> for F where
    V1: TryFrom<Value, Error=E> + Send + Sync,
    V2: TryFrom<Value, Error=E> + Send + Sync,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    A3: ExtractFromPipelineCtx + Send + Sync,
    O: Into<ValidateResult> + Send + Sync,
    F: Fn(V1, V2, A1, A2, A3) -> Fut + Sync + Send + 'static,
    E: Into<Error> + std::error::Error,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, old: Value, new: Value, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        let old: V1 = old.try_into().unwrap();
        let new: V2 = new.try_into().unwrap();
        let a1 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let a2 = ExtractFromPipelineCtx::extract(&args, &ctx);
        let a3 = ExtractFromPipelineCtx::extract(&args, &ctx);
        Box::pin(self(old, new, a1, a2, a3))
    }
}