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
pub mod transform;
pub mod validator;
pub mod callback;
pub mod compare;

use std::future::Future;
use std::sync::Arc;
use educe::Educe;
use crate::arguments::Arguments;
use crate::pipeline::ctx::Ctx;
use teo_result::Result;
use futures_util::future::BoxFuture;
use serde::Serialize;
use teo_parser::r#type::Type;
use crate::object::Object;

#[derive(Educe)]
#[educe(Debug)]
#[derive(Serialize)]
pub struct Item {
    pub path: Vec<String>,
    #[educe(Debug(ignore))] #[serde(skip)]
    pub(crate) call: Arc<dyn Call>,
}

pub trait Call: Send + Sync {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, Result<Object>>;
}

impl<F, Fut> Call for F where
        F: Fn(Arguments, Ctx) -> Fut + Sync + Send,
        Fut: Future<Output = Result<Object>> + Send + 'static {
    fn call(&self, args: Arguments, ctx: Ctx) -> BoxFuture<'static, Result<Object>> {
        Box::pin(self(args, ctx))
    }
}

#[derive(Educe, Serialize, Clone)]
#[educe(Debug)]
pub struct BoundedItem {
    pub path: Vec<String>,
    pub arguments: Arguments,
    #[educe(Debug(ignore))] #[serde(skip)]
    pub(crate) call: Arc<dyn Call>,
    pub(crate) cast_output_type: Option<Type>,
}

impl BoundedItem {

    pub(crate) async fn call(&self, args: Arguments, ctx: Ctx) -> Result<Object> {
        self.call.call(args, ctx).await
    }
}