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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
pub mod error;
pub mod port;
pub mod value;

use std::{
    collections::{BTreeSet, HashMap},
    future::Future,
    io,
    net::SocketAddr,
    sync::Arc,
};

use axum::{
    http::StatusCode,
    routing::{get, post},
    Extension, Json, Router,
};

pub use error::{Error, Result};
pub use port::Port;
use serde::Serialize;
use tokio::net::TcpListener;
pub use value::{PortValue, PortValueType};

#[derive(Debug, Clone, Serialize)]
pub struct CodeBlockAppBuilder {
    inputs: Vec<Port>,
    outputs: Vec<Port>,
}

impl CodeBlockAppBuilder {
    pub fn new() -> Self {
        CodeBlockAppBuilder {
            inputs: Vec::new(),
            outputs: Vec::new(),
        }
    }

    /// Append input ports of the code block, it is required to have at least one input
    pub fn inputs(mut self, inputs: impl IntoIterator<Item = Port>) -> Self {
        self.inputs.extend(inputs);
        self
    }

    // Append output ports of the code block
    pub fn outputs(mut self, outputs: impl IntoIterator<Item = Port>) -> Self {
        self.outputs.extend(outputs);
        self
    }

    pub fn build(self) -> Result<CodeBlockApp> {
        if self.inputs.is_empty() {
            return Err(Error::EmptyPorts("inputs"));
        }

        let mut inputs = BTreeSet::new();
        let mut outputs = BTreeSet::new();

        for input in self.inputs {
            let name = input.name.clone();
            inputs
                .insert(input)
                .then_some(())
                .ok_or(Error::NameConflict(name))?;
        }

        for output in self.outputs {
            let name = output.name.clone();
            outputs
                .insert(output)
                .then_some(())
                .ok_or(Error::NameConflict(name))?;
        }

        Ok(CodeBlockApp { inputs, outputs })
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct CodeBlockApp {
    inputs: BTreeSet<Port>,
    outputs: BTreeSet<Port>,
}

impl CodeBlockApp {
    /// Serve the code block
    pub fn serve<Fut>(
        self,
        handler: fn(HashMap<String, PortValue>) -> Fut,
    ) -> impl Future<Output = io::Result<()>>
    where
        Fut: Future<Output = Result<HashMap<String, PortValue>, String>> + Send + 'static,
    {
        let app = Router::new()
            .route("/ports", get(get_ports))
            .route("/run", post(move |app, req| run(app, req, handler)))
            .layer(Extension(Arc::new(self)));

        let port = std::env::var("PORT")
            .ok()
            .and_then(|p| p.parse().ok())
            .unwrap_or(3000);

        async move {
            let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port))).await?;
            axum::serve(listener, app).await
        }
    }

    fn validate_inputs(&self, inputs: &HashMap<String, PortValue>) -> Result<()> {
        self.inputs
            .iter()
            .map(|i| {
                inputs
                    .contains_key(&i.name)
                    .then_some(())
                    .ok_or(Error::MissingPort(i.name.clone()))
            })
            .collect::<Result<()>>()
    }

    fn validate_outputs(&self, outputs: &HashMap<String, PortValue>) -> Result<()> {
        self.outputs
            .iter()
            .map(|i| {
                outputs
                    .contains_key(&i.name)
                    .then_some(())
                    .ok_or(Error::MissingPort(i.name.clone()))
            })
            .collect::<Result<()>>()
    }
}

async fn get_ports(Extension(app): Extension<Arc<CodeBlockApp>>) -> Json<CodeBlockApp> {
    Json(app.as_ref().clone())
}

async fn run<Fut>(
    Extension(app): Extension<Arc<CodeBlockApp>>,
    Json(req): Json<HashMap<String, PortValue>>,
    handler: fn(HashMap<String, PortValue>) -> Fut,
) -> Result<Json<HashMap<String, PortValue>>, (StatusCode, String)>
where
    Fut: Future<Output = Result<HashMap<String, PortValue>, String>> + 'static + Send,
{
    app.validate_inputs(&req)
        .map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;

    let outputs = handler(req)
        .await
        .map(|res| Json(res))
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e))?;

    app.validate_outputs(&outputs)
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    Ok(outputs)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::{CodeBlockAppBuilder, Port, PortValue, PortValueType};

    #[test]
    fn test() {
        _ = CodeBlockAppBuilder::new()
            .inputs([Port::new(String::from("port1"), PortValueType::Bool)])
            .build()
            .unwrap()
            .serve(handler);
    }

    async fn handler(
        _req: HashMap<String, PortValue>,
    ) -> Result<HashMap<String, PortValue>, String> {
        Ok(HashMap::new())
    }
}