macrograph/graph.rs
1use std::collections::HashMap;
2
3use super::{
4 node::Node,
5 types::*,
6};
7
8pub struct Graph {
9 id_counter: i32,
10 pub nodes: HashMap<i32, NodeRef>,
11}
12
13impl Graph {
14 pub fn new() -> Self {
15 Self {
16 id_counter: 0,
17 nodes: HashMap::new(),
18 }
19 }
20
21 fn generate_id(&mut self) -> i32 {
22 let id = self.id_counter;
23 self.id_counter += 1;
24 id
25 }
26
27 pub fn create_node(&mut self, schema: &NodeSchemaRef) -> NodeRef {
28 let id = self.generate_id();
29
30 let node = Node::new(id, schema);
31
32 self.nodes.insert(id, node.clone());
33
34 node.clone()
35 }
36
37 pub fn node(&self, id: i32) -> Option<&NodeRef> {
38 self.nodes.get(&id)
39 }
40
41 // pub async fn execute_node(&self, id: i32) -> Option<i32> {
42 // let node = self.node(id);
43 //
44 // if let Some(node_arc) = node {
45 // let schema = node_arc.schema.clone();
46 //
47 // match &*schema {
48 // NodeSchema::Exec(schema) => match schema.execute {
49 // ExecuteFn::Sync(execute) => Some(execute(node_arc.clone())),
50 // ExecuteFn::Async(execute) => Some(execute(node_arc.clone()).await),
51 // },
52 // _ => todo!(),
53 // }
54 // } else {
55 // None
56 // }
57 // }
58
59 // pub async fn execute_connection(&self, output: OutputRef) {
60 // let
61 // }
62}
63
64// #[cfg(test)]
65// mod test {
66// use std::time::Duration;
67//
68// use crate::{
69// core::{schema::NodeSchema},
70// exec_fn,
71// };
72//
73// use super::Graph;
74//
75// #[test]
76// fn creates_node() {
77// let mut graph = Graph::new();
78//
79// let schema = NodeSchema::new_exec("test", |_n| {}, exec_fn!(|_n, _c| { 1 }));
80// let node = graph.create_node(&schema);
81//
82// assert_eq!(node.id, graph.node(0).unwrap().id)
83// }
84//
85// #[tokio::test]
86// async fn executes_sync_node() {
87// let mut graph = Graph::new();
88//
89// let schema = NodeSchema::new_exec("test", |_n| {}, exec_fn!(|_n, _c| { 69 }));
90// graph.create_node(&schema);
91//
92// assert_eq!(graph.execute_node(0).await.unwrap(), 69);
93// }
94//
95// #[tokio::test]
96// async fn executes_async_node() {
97// let mut graph = Graph::new();
98//
99// let schema = NodeSchema::new_exec(
100// "test",
101// |_n| {},
102// exec_fn!(|_n| async {
103// tokio::time::sleep(Duration::from_millis(1000)).await;
104// 69
105// }),
106// );
107// graph.create_node(&schema);
108//
109// assert_eq!(graph.execute_node(0).await.unwrap(), 69);
110// }
111// }