-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.rs
More file actions
164 lines (149 loc) · 4.99 KB
/
Copy pathcompiler.rs
File metadata and controls
164 lines (149 loc) · 4.99 KB
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
//! Flow compiler for runtime execution plans.
use std::collections::HashMap;
use tucana::shared::{NodeFunction, node_value};
use crate::{
runtime::engine::model::{
CompiledArg, CompiledFlow, CompiledNode, CompiledParameter, NodeExecutionTarget,
},
types::errors::runtime_error::RuntimeError,
};
#[derive(Debug)]
pub enum CompileError {
DuplicateNodeId {
node_id: i64,
},
StartNodeMissing {
node_id: i64,
},
NextNodeMissing {
node_id: i64,
next_node_id: i64,
},
ParameterValueMissing {
node_id: i64,
parameter_index: usize,
},
}
impl CompileError {
pub fn as_runtime_error(&self) -> RuntimeError {
match self {
CompileError::DuplicateNodeId { node_id } => RuntimeError::new(
"T-CORE-000101",
"FlowCompileError",
format!("Duplicate node id in flow: {}", node_id),
),
CompileError::StartNodeMissing { node_id } => RuntimeError::new(
"T-CORE-000102",
"FlowCompileError",
format!("Start node not found in flow: {}", node_id),
),
CompileError::NextNodeMissing {
node_id,
next_node_id,
} => RuntimeError::new(
"T-CORE-000103",
"FlowCompileError",
format!(
"Node {} points to missing next node {}",
node_id, next_node_id
),
),
CompileError::ParameterValueMissing {
node_id,
parameter_index,
} => RuntimeError::new(
"T-CORE-000104",
"FlowCompileError",
format!(
"Node {} parameter {} does not contain a value",
node_id, parameter_index
),
),
}
}
}
pub fn compile_flow(
start_node_id: i64,
nodes: Vec<NodeFunction>,
) -> Result<CompiledFlow, CompileError> {
let mut node_idx_by_id = HashMap::with_capacity(nodes.len());
for (idx, node) in nodes.iter().enumerate() {
if node_idx_by_id.insert(node.database_id, idx).is_some() {
return Err(CompileError::DuplicateNodeId {
node_id: node.database_id,
});
}
}
let start_idx = match node_idx_by_id.get(&start_node_id).copied() {
Some(idx) => idx,
None => {
return Err(CompileError::StartNodeMissing {
node_id: start_node_id,
});
}
};
let mut compiled_nodes = Vec::with_capacity(nodes.len());
for node in nodes {
let next_idx = match node.next_node_id {
Some(next_id) => match node_idx_by_id.get(&next_id).copied() {
Some(idx) => Some(idx),
None => {
return Err(CompileError::NextNodeMissing {
node_id: node.database_id,
next_node_id: next_id,
});
}
},
None => None,
};
let execution_target = execution_target_for(&node);
let mut parameters = Vec::with_capacity(node.parameters.len());
for (parameter_index, parameter) in node.parameters.iter().enumerate() {
let Some(node_value) = parameter.value.as_ref() else {
return Err(CompileError::ParameterValueMissing {
node_id: node.database_id,
parameter_index,
});
};
let Some(value) = node_value.value.as_ref() else {
return Err(CompileError::ParameterValueMissing {
node_id: node.database_id,
parameter_index,
});
};
let arg = match value {
node_value::Value::LiteralValue(v) => CompiledArg::Literal(v.clone()),
node_value::Value::ReferenceValue(r) => CompiledArg::Reference(r.clone()),
node_value::Value::SubFlow(_sub_flow) => unimplemented!("Taurus needs to handle SubFlows (issue nr #184)"),
};
parameters.push(CompiledParameter {
runtime_parameter_id: parameter.runtime_parameter_id.clone(),
arg,
});
}
compiled_nodes.push(CompiledNode {
id: node.database_id,
handler_id: node.runtime_function_id,
execution_target,
next_idx,
parameters,
});
}
Ok(CompiledFlow {
start_idx,
nodes: compiled_nodes,
node_idx_by_id,
})
}
fn execution_target_for(node: &NodeFunction) -> NodeExecutionTarget {
if node.definition_source.is_empty()
|| node.definition_source == "taurus"
|| node.definition_source.starts_with("draco")
{
NodeExecutionTarget::Local
} else {
NodeExecutionTarget::Remote {
service: node.definition_source.clone(),
}
}
}