-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.rs
More file actions
235 lines (219 loc) · 7.73 KB
/
Copy pathcompiler.rs
File metadata and controls
235 lines (219 loc) · 7.73 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
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
//! Flow compiler for runtime execution plans.
use std::collections::HashMap;
use tucana::shared::{NodeFunction, node_value, sub_flow};
use crate::{
runtime::engine::model::{
CompiledArg, CompiledFlow, CompiledNode, CompiledParameter, CompiledThunk,
NodeExecutionTarget,
},
types::errors::runtime_error::RuntimeError,
};
#[derive(Debug)]
pub enum CompileError {
NodeIdMissing {
node_index: usize,
},
DuplicateNodeId {
node_id: i64,
},
StartNodeMissing {
node_id: i64,
},
NextNodeMissing {
node_id: i64,
next_node_id: i64,
},
ParameterValueMissing {
node_id: i64,
parameter_index: usize,
},
SubFlowExecutionReferenceMissing {
node_id: i64,
parameter_index: usize,
},
EmptyRemoteService {
node_id: i64,
definition_source: String,
},
}
impl CompileError {
pub fn as_runtime_error(&self) -> RuntimeError {
match self {
CompileError::NodeIdMissing { node_index } => RuntimeError::new(
"T-CORE-000100",
"FlowCompileError",
format!("Node at index {} is missing database id", node_index),
),
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
),
),
CompileError::SubFlowExecutionReferenceMissing {
node_id,
parameter_index,
} => RuntimeError::new(
"T-CORE-000105",
"FlowCompileError",
format!(
"Node {} parameter {} sub_flow is missing execution reference",
node_id, parameter_index
),
),
CompileError::EmptyRemoteService {
node_id,
definition_source,
} => RuntimeError::new(
"T-CORE-000106",
"FlowCompileError",
format!(
"Node {} definition_source '{}' does not contain a remote service name",
node_id, definition_source
),
),
}
}
}
pub fn compile_flow(
project_id: i64,
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() {
let Some(node_id) = node.database_id else {
return Err(CompileError::NodeIdMissing { node_index: idx });
};
if node_idx_by_id.insert(node_id, idx).is_some() {
return Err(CompileError::DuplicateNodeId { node_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 node_id = node
.database_id
.expect("compiler validates node database ids before compilation");
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,
next_node_id: next_id,
});
}
},
None => None,
};
let execution_target = execution_target_for(node_id, &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,
parameter_index,
});
};
let Some(value) = node_value.value.as_ref() else {
return Err(CompileError::ParameterValueMissing {
node_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) => {
match sub_flow.execution_reference.as_ref() {
Some(sub_flow::ExecutionReference::StartingNodeId(node_id)) => {
CompiledArg::Deferred(CompiledThunk::Node(*node_id))
}
Some(sub_flow::ExecutionReference::FunctionIdentifier(identifier)) => {
CompiledArg::Deferred(CompiledThunk::Function {
identifier: identifier.clone(),
parameter_index: parameter_index as i64,
settings: sub_flow.settings.clone(),
})
}
None => {
return Err(CompileError::SubFlowExecutionReferenceMissing {
node_id,
parameter_index,
});
}
}
}
};
parameters.push(CompiledParameter {
runtime_parameter_id: parameter.runtime_parameter_id.clone(),
arg,
});
}
compiled_nodes.push(CompiledNode {
id: node_id,
handler_id: node.runtime_function_id,
execution_target,
next_idx,
parameters,
});
}
Ok(CompiledFlow {
project_id,
start_idx,
nodes: compiled_nodes,
node_idx_by_id,
})
}
fn execution_target_for(
node_id: i64,
node: &NodeFunction,
) -> Result<NodeExecutionTarget, CompileError> {
match node.definition_source.as_deref() {
None | Some("") | Some("taurus") => Ok(NodeExecutionTarget::Local),
Some(source) if source.starts_with("draco") => Ok(NodeExecutionTarget::Local),
Some(service) => match service.strip_prefix("action.").unwrap_or(service) {
"" => Err(CompileError::EmptyRemoteService {
node_id,
definition_source: service.to_string(),
}),
service => Ok(NodeExecutionTarget::Remote {
service: service.to_string(),
}),
},
}
}