teo_runtime/pipeline/item/
validator.rs

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::future::Future;
use futures_util::future::BoxFuture;
use self::Validity::*;
use teo_result::{Error, Result};
use crate::arguments::Arguments;
use crate::pipeline::Ctx;
use crate::pipeline::ctx::extract::ExtractFromPipelineCtx;
use crate::value::Value;

#[derive(Clone)]
pub enum Validity {
    Valid,
    Invalid(String)
}

impl Validity {
    pub(crate) fn is_valid(&self) -> bool {
        match self {
            Valid => true,
            _ => false,
        }
    }

    pub(crate) fn invalid_reason(&self) -> Option<&str> {
        match self {
            Invalid(reason) => Some(&reason),
            _ => None,
        }
    }
}

impl From<&str> for Validity {
    fn from(reason: &str) -> Self {
        Invalid(reason.to_string())
    }
}

impl From<String> for Validity {
    fn from(reason: String) -> Self {
        Invalid(reason)
    }
}

impl From<bool> for Validity {
    fn from(valid: bool) -> Self {
        match valid {
            true => Valid,
            false => Invalid("value is invalid".to_owned())
        }
    }
}

impl From<()> for Validity {
    fn from(_: ()) -> Self {
        Valid
    }
}

impl From<Option<String>> for Validity {
    fn from(value: Option<String>) -> Self {
        match value {
            Some(v) => Validity::Invalid(v),
            None => Validity::Valid,
        }
    }
}

impl From<Option<&str>> for Validity {
    fn from(value: Option<&str>) -> Self {
        match value {
            Some(v) => Validity::Invalid(v.to_owned()),
            None => Validity::Valid,
        }
    }
}

pub enum ValidateResult {
    Validity(Validity),
    Result(Result<Validity>),
}

impl<T> From<T> for ValidateResult where T: Into<Validity> {
    fn from(value: T) -> Self {
        ValidateResult::Validity(value.into())
    }
}

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

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

impl<A0, O, F, Fut, E> ValidateArgument<(A0,), O> for F where
    A0: TryFrom<Value, Error=E> + Send + Sync,
    Error: From<E>,
    E: std::error::Error,
    F: Fn(A0) -> Fut + Sync + Send + Clone + 'static,
    O: Into<ValidateResult> + Send + Sync,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, _: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        let value: A0 = ctx.value().clone().try_into().unwrap();
        Box::pin(self(value))
    }
}

impl<A0, A1, O, F, Fut, E> ValidateArgument<(A0, A1), O> for F where
    A0: TryFrom<Value, Error=E> + Send + Sync,
    Error: From<E>,
    E: std::error::Error,
    A1: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1) -> Fut + Sync + Send + 'static,
    O: Into<ValidateResult> + Send + Sync,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        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, Fut, E> ValidateArgument<(A0, A1, A2), O> for F where
    A0: TryFrom<Value, Error=E> + Send + Sync,
    Error: From<E>,
    E: std::error::Error,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1, A2) -> Fut + Sync + Send + 'static,
    O: Into<ValidateResult> + Send + Sync,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        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, Fut, E> ValidateArgument<(A0, A1, A2, A3), O> for F where
    A0: TryFrom<Value, Error=E> + Send + Sync,
    Error: From<E>,
    E: std::error::Error,
    A1: ExtractFromPipelineCtx + Send + Sync,
    A2: ExtractFromPipelineCtx + Send + Sync,
    A3: ExtractFromPipelineCtx + Send + Sync,
    F: Fn(A0, A1, A2, A3) -> Fut + Sync + Send + 'static,
    O: Into<ValidateResult> + Send + Sync,
    Fut: Future<Output = O> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, O> {
        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))
    }
}