|
| 1 | +use crate::context::Context; |
| 2 | +use crate::context::signal::Signal; |
| 3 | +use crate::error::RuntimeError; |
| 4 | +use crate::registry::FunctionStore; |
| 5 | +use std::collections::HashMap; |
| 6 | +use tucana::shared::{NodeFunction, NodeParameter}; |
| 7 | + |
| 8 | +pub struct Executor<'a> { |
| 9 | + functions: &'a FunctionStore, |
| 10 | + nodes: HashMap<i64, NodeFunction>, |
| 11 | + context: Context, |
| 12 | +} |
| 13 | + |
| 14 | +type HandleNodeParameterFn = fn(&mut Executor, node_parameter: &NodeParameter) -> Signal; |
| 15 | + |
| 16 | +impl<'a> Executor<'a> { |
| 17 | + pub fn new( |
| 18 | + functions: &'a FunctionStore, |
| 19 | + nodes: HashMap<i64, NodeFunction>, |
| 20 | + context: Context, |
| 21 | + ) -> Self { |
| 22 | + Executor { |
| 23 | + functions, |
| 24 | + nodes, |
| 25 | + context, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn execute(&mut self, starting_node_id: i64) -> Signal { |
| 30 | + let mut current_node_id = starting_node_id; |
| 31 | + |
| 32 | + loop { |
| 33 | + let node = match self.nodes.get(¤t_node_id) { |
| 34 | + None => { |
| 35 | + return Signal::Failure(RuntimeError::simple_str( |
| 36 | + "NodeNotFound", |
| 37 | + "The node with the id was not found", |
| 38 | + )); |
| 39 | + } |
| 40 | + Some(n) => n.clone(), |
| 41 | + }; |
| 42 | + |
| 43 | + let mut parameters = Vec::new(); |
| 44 | + |
| 45 | + for node_parameter in &node.parameters { |
| 46 | + match Executor::handle_node_parameter(self, node_parameter) { |
| 47 | + Signal::Success(value) | Signal::Return(value) => { |
| 48 | + parameters.push(value.clone()) |
| 49 | + } |
| 50 | + Signal::Failure(error) => return Signal::Failure(error), |
| 51 | + Signal::Stop => return Signal::Stop, |
| 52 | + Signal::Respond(value) => return Signal::Respond(value), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let execution_result = match self.functions.get(node.runtime_function_id.as_str()) { |
| 57 | + Some(handler) => handler(¶meters, &mut self.context), |
| 58 | + None => { |
| 59 | + return Signal::Failure(RuntimeError::simple_str( |
| 60 | + "FunctionNotFound", |
| 61 | + "The function was not found", |
| 62 | + )); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + match execution_result { |
| 67 | + Signal::Success(value) => { |
| 68 | + if let Some(next_node_id) = node.next_node_id { |
| 69 | + current_node_id = next_node_id; |
| 70 | + continue; |
| 71 | + } else { |
| 72 | + return Signal::Success(value); |
| 73 | + } |
| 74 | + } |
| 75 | + Signal::Failure(runtime_error) => return Signal::Failure(runtime_error), |
| 76 | + Signal::Return(value) => return Signal::Return(value), |
| 77 | + Signal::Respond(value) => return Signal::Respond(value), |
| 78 | + Signal::Stop => return Signal::Stop, |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn handle_node_parameter(&mut self, node_parameter: &NodeParameter) -> Signal { |
| 84 | + let node_value = match &node_parameter.value { |
| 85 | + Some(v) => v, |
| 86 | + None => { |
| 87 | + return Signal::Failure(RuntimeError::simple_str( |
| 88 | + "NodeValueNotFound", |
| 89 | + "An error occurred while executing a flow!", |
| 90 | + )); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + let value = match &node_value.value { |
| 95 | + Some(v) => v, |
| 96 | + None => { |
| 97 | + return Signal::Failure(RuntimeError::simple_str( |
| 98 | + "NodeValueNotFound", |
| 99 | + "An error occurred while executing a flow!", |
| 100 | + )); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + match value { |
| 105 | + tucana::shared::node_value::Value::LiteralValue(value) => { |
| 106 | + Signal::Success(value.clone()) |
| 107 | + } |
| 108 | + tucana::shared::node_value::Value::ReferenceValue(_reference_value) => { |
| 109 | + todo!("implement reference values!") |
| 110 | + } |
| 111 | + tucana::shared::node_value::Value::NodeFunctionId(id) => Executor::execute(self, *id), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments