this repo has no description
1use std::collections::HashMap;
2
3use anyhow::{bail, Result};
4
5use crate::{runtime::nodes::RuntimeNodeTree, structs::parameter_types::ParameterType};
6
7pub mod commands;
8pub mod nodes;
9
10// TODO: Variables
11
12pub fn recurse_runtime(entry: String, tab: &mut RuntimeNodeTree, args: Vec<ParameterType>) -> Result<()>{
13 let ( out_args, output_map ) = runtime(entry, tab, args)?;
14
15 let mut next_node_args: HashMap<String, Vec<ParameterType>> = HashMap::new();
16
17 for i in 0..out_args.len(){
18 if output_map.len() <= i { break; }
19 let links = &output_map[i];
20
21 for ( id, link_index, _ ) in links{
22 let link_index = link_index.clone() as usize;
23
24 if next_node_args.contains_key(id){
25 let args: &mut _ = next_node_args.get_mut(id).unwrap();
26 while args.len() < link_index{ args.push(ParameterType::None); }
27
28 args.push(out_args[i].clone());
29 } else{
30 let mut args = vec![ParameterType::None; link_index];
31 args.push(out_args[i].clone());
32
33 next_node_args.insert(id.clone(), args);
34 }
35 }
36 }
37
38 for i in 0..out_args.len(){
39 if let ParameterType::Flow(next) = out_args[i]{
40 if next{
41 let links = &output_map[i];
42
43 for ( id, _, _ ) in links{
44 let args = next_node_args.remove(id).unwrap();
45 recurse_runtime(id.clone(), tab, args)?;
46 }
47 }
48 }
49 }
50
51 Ok(())
52}
53
54pub fn runtime(entry: String, tab: &mut RuntimeNodeTree, mut args: Vec<ParameterType>) -> Result<(Vec<ParameterType>, Vec<Vec<(String, isize, isize)>>)> {
55 let node = tab.nodes.get_mut(&entry);
56 if node.is_none() { bail!("Cannot find node"); }
57
58 let node = node.unwrap();
59 let inputs = node.inputs();
60
61 let mut needed_input_nodes = HashMap::new();
62
63 for i in 0..inputs.len(){
64 if i >= args.len() || args[i] == ParameterType::None{
65 if let Some(input) = &inputs[i]{
66 if !needed_input_nodes.contains_key(&input.0){
67 needed_input_nodes.insert(input.0.clone(), vec![(input.1.clone(), i.clone())]);
68 } else{
69 needed_input_nodes.get_mut(&input.0).unwrap().push((input.1.clone(), i.clone()));
70 }
71 }
72 }
73 }
74
75 for ( id, needed ) in needed_input_nodes{
76 let (out_args, _) = runtime(id, tab, vec![]).unwrap();
77
78 for ( output, input ) in needed{
79 let arg = &out_args[output as usize];
80
81 if args.len() >= input{
82 while args.len() < input{ args.push(ParameterType::None); }
83 args.push(arg.clone());
84 } else{
85 args[input] = arg.clone();
86 }
87 }
88 }
89
90 let node = tab.nodes.get_mut(&entry); // TODO: Find a way to only do this lookup once
91 if node.is_none() { bail!("Cannot find node"); }
92
93 let node = node.unwrap();
94
95 let output = node.execute(args);
96 Ok((output, node.outputs()))
97}