intuicio_nodes/
server.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use crate::nodes::*;
use intuicio_core::registry::Registry;
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, HashSet},
    error::Error,
};
use typid::ID;

pub type NodeGraphId<T> = ID<NodeGraph<T>>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestAdd<T: NodeDefinition> {
    pub nodes: Vec<Node<T>>,
    pub connections: Vec<NodeConnection<T>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestRemove<T: NodeDefinition> {
    pub nodes: Vec<NodeId<T>>,
    pub connections: Vec<NodeConnection<T>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestUpdate<T: NodeDefinition> {
    pub nodes: Vec<Node<T>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestQueryRegion {
    pub fx: i64,
    pub fy: i64,
    pub tx: i64,
    pub ty: i64,
    pub extrude: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseQuery<T: NodeDefinition> {
    pub nodes: Vec<Node<T>>,
    pub connections: Vec<NodeConnection<T>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NodeGraphServerError {
    NodeGraphDoesNotExists(String),
    NodeNotFound { graph: String, node: String },
    ValidationErrors { graph: String, errors: Vec<String> },
}

impl std::fmt::Display for NodeGraphServerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeGraphServerError::NodeGraphDoesNotExists(id) => {
                write!(f, "Node graph does not exists: {}", id)
            }
            NodeGraphServerError::NodeNotFound { graph, node } => {
                write!(f, "Node graph: {} does not have node: {}", graph, node)
            }
            NodeGraphServerError::ValidationErrors { graph, errors } => {
                write!(f, "Node graph: {} validation errors:", graph)?;
                for error in errors {
                    write!(f, "{}", error)?;
                }
                Ok(())
            }
        }
    }
}

impl Error for NodeGraphServerError {}

pub struct NodeGraphServer<T: NodeDefinition + Clone> {
    graphs: HashMap<NodeGraphId<T>, NodeGraph<T>>,
}

impl<T: NodeDefinition + Clone> Default for NodeGraphServer<T> {
    fn default() -> Self {
        Self {
            graphs: Default::default(),
        }
    }
}

impl<T: NodeDefinition + Clone> NodeGraphServer<T> {
    pub fn graph(&self, id: NodeGraphId<T>) -> Result<&NodeGraph<T>, NodeGraphServerError> {
        self.graphs
            .get(&id)
            .ok_or_else(|| NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
    }

    pub fn graph_mut(
        &mut self,
        id: NodeGraphId<T>,
    ) -> Result<&mut NodeGraph<T>, NodeGraphServerError> {
        self.graphs
            .get_mut(&id)
            .ok_or_else(|| NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
    }

    pub fn create(&mut self) -> NodeGraphId<T> {
        let id = NodeGraphId::new();
        self.graphs.insert(id, NodeGraph::default());
        id
    }

    pub fn destroy(&mut self, id: NodeGraphId<T>) -> Result<NodeGraph<T>, NodeGraphServerError> {
        self.graphs
            .remove(&id)
            .ok_or_else(|| NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
    }

    pub fn list(&self) -> impl Iterator<Item = &NodeGraphId<T>> {
        self.graphs.keys()
    }

    pub fn add(
        &mut self,
        id: NodeGraphId<T>,
        request: RequestAdd<T>,
        registry: &Registry,
    ) -> Result<(), NodeGraphServerError> {
        if let Some(graph) = self.graphs.get_mut(&id) {
            for node in request.nodes {
                graph.add_node(node, registry);
            }
            for connection in request.connections {
                graph.connect_nodes(connection);
            }
            graph.refresh_spatial_cache();
            Ok(())
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
        }
    }

    pub fn remove(
        &mut self,
        id: NodeGraphId<T>,
        request: RequestRemove<T>,
        registry: &Registry,
    ) -> Result<(), NodeGraphServerError> {
        if let Some(graph) = self.graphs.get_mut(&id) {
            for connection in request.connections {
                graph.disconnect_nodes(
                    connection.from_node,
                    connection.to_node,
                    &connection.from_pin,
                    &connection.to_pin,
                );
            }
            for id in request.nodes {
                graph.remove_node(id, registry);
            }
            graph.refresh_spatial_cache();
            Ok(())
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
        }
    }

    pub fn update(
        &mut self,
        id: NodeGraphId<T>,
        request: RequestUpdate<T>,
    ) -> Result<(), NodeGraphServerError> {
        if let Some(graph) = self.graphs.get_mut(&id) {
            for source in &request.nodes {
                if graph.node(source.id()).is_none() {
                    return Err(NodeGraphServerError::NodeNotFound {
                        graph: id.to_string(),
                        node: source.id().to_string(),
                    });
                }
            }
            for source in request.nodes {
                let id = source.id();
                *graph.node_mut(id).unwrap() = source;
            }
            graph.refresh_spatial_cache();
            Ok(())
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
        }
    }

    pub fn clear(&mut self, id: NodeGraphId<T>) -> Result<(), NodeGraphServerError> {
        if let Some(graph) = self.graphs.get_mut(&id) {
            graph.clear();
            graph.refresh_spatial_cache();
            Ok(())
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(id.to_string()))
        }
    }

    pub fn query_all(
        &self,
        graph: NodeGraphId<T>,
    ) -> Result<ResponseQuery<T>, NodeGraphServerError> {
        if let Some(graph) = self.graphs.get(&graph) {
            Ok(ResponseQuery {
                nodes: graph.nodes().cloned().collect(),
                connections: graph.connections().cloned().collect(),
            })
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(
                graph.to_string(),
            ))
        }
    }

    pub fn query_region(
        &self,
        graph: NodeGraphId<T>,
        request: RequestQueryRegion,
    ) -> Result<ResponseQuery<T>, NodeGraphServerError> {
        if let Some(graph) = self.graphs.get(&graph) {
            let RequestQueryRegion {
                fx,
                fy,
                tx,
                ty,
                extrude,
            } = request;
            let nodes = graph
                .query_region_nodes(fx, fy, tx, ty, extrude)
                .filter_map(|id| graph.node(id))
                .cloned()
                .collect::<Vec<_>>();
            let connections = nodes
                .iter()
                .flat_map(|node| graph.node_connections(node.id()))
                .cloned()
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();
            Ok(ResponseQuery { nodes, connections })
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(
                graph.to_string(),
            ))
        }
    }

    pub fn suggest_all_nodes(
        x: i64,
        y: i64,
        registry: &Registry,
    ) -> Vec<ResponseSuggestionNode<T>> {
        NodeGraph::suggest_all_nodes(x, y, registry)
    }

    pub fn validate(
        &self,
        graph: NodeGraphId<T>,
        registry: &Registry,
    ) -> Result<(), NodeGraphServerError> {
        if let Some(item) = self.graphs.get(&graph) {
            match item.validate(registry) {
                Ok(_) => Ok(()),
                Err(errors) => Err(NodeGraphServerError::ValidationErrors {
                    graph: graph.to_string(),
                    errors: errors.into_iter().map(|error| error.to_string()).collect(),
                }),
            }
        } else {
            Err(NodeGraphServerError::NodeGraphDoesNotExists(
                graph.to_string(),
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use intuicio_core::prelude::*;
    use serde::{de::DeserializeOwned, Deserialize, Serialize};

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TypeInfo;

    impl std::fmt::Display for TypeInfo {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "")
        }
    }

    impl NodeTypeInfo for TypeInfo {
        fn type_query(&self) -> TypeQuery {
            Default::default()
        }

        fn are_compatible(&self, _: &Self) -> bool {
            true
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    enum Nodes {
        Start,
        Expression(i32),
        Result,
        Convert(String),
    }

    impl NodeDefinition for Nodes {
        type TypeInfo = TypeInfo;

        fn node_label(&self, _: &Registry) -> String {
            format!("{:?}", self)
        }

        fn node_pins_in(&self, _: &Registry) -> Vec<NodePin<Self::TypeInfo>> {
            match self {
                Nodes::Start => vec![],
                Nodes::Expression(_) => vec![NodePin::property("Value")],
                Nodes::Result => vec![
                    NodePin::execute("In", false),
                    NodePin::parameter("Data", TypeInfo),
                ],
                Nodes::Convert(_) => vec![
                    NodePin::execute("In", false),
                    NodePin::property("Name"),
                    NodePin::parameter("Data in", TypeInfo),
                ],
            }
        }

        fn node_pins_out(&self, _: &Registry) -> Vec<NodePin<Self::TypeInfo>> {
            match self {
                Nodes::Start => vec![NodePin::execute("Out", false)],
                Nodes::Expression(_) => vec![NodePin::parameter("Data", TypeInfo)],
                Nodes::Result => vec![],
                Nodes::Convert(_) => vec![
                    NodePin::execute("Out", false),
                    NodePin::parameter("Data out", TypeInfo),
                ],
            }
        }

        fn node_is_start(&self, _: &Registry) -> bool {
            matches!(self, Self::Start)
        }

        fn node_suggestions(
            _: i64,
            _: i64,
            _: NodeSuggestion<Self>,
            _: &Registry,
        ) -> Vec<ResponseSuggestionNode<Self>> {
            vec![]
        }
    }

    fn mock_transfer<T: Serialize + DeserializeOwned>(value: T) -> T {
        let content = serde_json::to_string(&value).unwrap();
        serde_json::from_str(&content).unwrap()
    }

    #[test]
    fn test_server() {
        let registry = Registry::default().with_basic_types();
        let mut server = NodeGraphServer::default();
        let graph = server.create();
        let start = Node::new(0, 0, Nodes::Start);
        let expression = Node::new(0, 0, Nodes::Expression(42));
        let convert = Node::new(0, 0, Nodes::Convert("foo".to_owned()));
        let result = Node::new(0, 0, Nodes::Result);
        server
            .add(
                graph,
                mock_transfer(RequestAdd {
                    connections: vec![
                        NodeConnection::new(start.id(), convert.id(), "Out", "In"),
                        NodeConnection::new(convert.id(), result.id(), "Out", "In"),
                        NodeConnection::new(expression.id(), convert.id(), "Data", "Data in"),
                    ],
                    nodes: vec![
                        start.clone(),
                        expression.clone(),
                        convert.clone(),
                        result.clone(),
                    ],
                }),
                &registry,
            )
            .unwrap();
        let temp = server.query_all(graph).unwrap();
        assert_eq!(temp.nodes.len(), 4);
        assert_eq!(temp.connections.len(), 3);
        server
            .remove(
                graph,
                RequestRemove {
                    nodes: vec![result.id(), convert.id()],
                    connections: vec![],
                },
                &registry,
            )
            .unwrap();
        let temp = server.query_all(graph).unwrap();
        assert_eq!(temp.nodes.len(), 2);
        assert_eq!(temp.connections.len(), 0);
        assert!(matches!(
            server.update(
                graph,
                mock_transfer(RequestUpdate {
                    nodes: vec![expression.clone(), convert.clone()],
                }),
            ),
            Err(NodeGraphServerError::NodeNotFound { .. })
        ));
        let temp = server.query_all(graph).unwrap();
        assert_eq!(temp.nodes.len(), 2);
        assert_eq!(temp.connections.len(), 0);
        server
            .update(
                graph,
                mock_transfer(RequestUpdate {
                    nodes: vec![expression.clone(), start.clone()],
                }),
            )
            .unwrap();
        let temp = server.query_all(graph).unwrap();
        assert_eq!(temp.nodes.len(), 2);
        assert_eq!(temp.connections.len(), 0);
    }
}