1use crate::common::diagnostic::Diagnostic;
4use crate::syntax::lexer::Mode;
5use indexmap::IndexSet;
6use smol_str::SmolStr;
7use std::collections::{HashMap, HashSet};
8use std::sync::Arc;
9use temple_dsl::{ExprUnit, Functions, InputRead, Span, TemplateUnit, Value};
10
11pub(crate) const HASH_LEN: usize = 32;
13
14#[derive(Debug, Clone)]
18pub struct Flow {
19 pub(crate) name: SmolStr,
20 pub(crate) edition: u32,
21 pub(crate) annotations: Vec<Annotation>,
22 pub(crate) entries: Vec<NodeId>,
23 pub(crate) events: IndexSet<SmolStr>,
24 pub(crate) params: Vec<Param>,
25 pub(crate) lets: Vec<Let>,
26 pub(crate) helpers: Vec<Helper>,
27 pub(crate) functions: Functions,
28 pub(crate) nodes: Vec<Node>,
29 pub(crate) node_ids: HashMap<SmolStr, NodeId>,
30 pub(crate) param_positions: HashMap<SmolStr, usize>,
32 pub(crate) asks: IndexSet<SmolStr>,
34 pub(crate) hash: [u8; HASH_LEN],
35 pub(crate) warnings: Vec<Diagnostic>,
36 pub(crate) source: Arc<str>,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub(crate) struct NodeId(pub(crate) u32);
43
44impl NodeId {
45 pub(crate) fn index(self) -> usize {
46 self.0 as usize
47 }
48}
49
50#[derive(Debug, Clone, PartialEq)]
52pub struct Annotation {
53 pub name: String,
55 pub value: Value,
57}
58
59#[derive(Debug, Clone, PartialEq)]
61pub struct Param {
62 pub name: String,
64 pub default: Value,
66 pub annotations: Vec<Annotation>,
68}
69
70#[derive(Debug, Clone)]
72pub(crate) struct Let {
73 pub name: SmolStr,
74 pub expression: ExprUnit,
75}
76
77#[derive(Debug, Clone)]
79pub(crate) struct Helper {
80 pub name: SmolStr,
81 pub params: Vec<SmolStr>,
82 pub body: ExprUnit,
83}
84
85#[derive(Debug, Clone)]
87pub(crate) struct Node {
88 pub name: SmolStr,
89 pub params: Vec<SmolStr>,
90 pub annotations: Vec<Annotation>,
91 pub lets: Vec<Let>,
92 pub branches: Vec<Branch>,
93 pub resumes_ask: bool,
95 pub is_entry: bool,
96 pub span: Span,
97}
98
99#[derive(Debug, Clone)]
101pub(crate) struct Branch {
102 pub guard: Option<ExprUnit>,
104 pub outcome: Outcome,
105 pub needs_event: bool,
108 pub span: Span,
109}
110
111#[derive(Debug, Clone)]
113pub(crate) enum Outcome {
114 Jump {
115 node: NodeId,
116 args: Vec<ExprUnit>,
117 },
118 Value {
119 template: TemplateUnit,
120 wake: Option<ExprUnit>,
121 },
122 Ask {
123 name: SmolStr,
124 payload: Option<ExprUnit>,
125 node: NodeId,
126 args: Vec<ExprUnit>,
127 },
128 Ignore,
129}
130
131impl Outcome {
132 fn units(&self) -> impl Iterator<Item = UnitRef<'_>> {
134 let (payload, template, args, wake) = match self {
135 Outcome::Jump { args, .. } => (None, None, args.as_slice(), None),
136 Outcome::Value { template, wake } => {
137 (None, Some(template), [].as_slice(), wake.as_ref())
138 }
139 Outcome::Ask { payload, args, .. } => (payload.as_ref(), None, args.as_slice(), None),
140 Outcome::Ignore => (None, None, [].as_slice(), None),
141 };
142 payload
143 .map(UnitRef::Expression)
144 .into_iter()
145 .chain(template.map(UnitRef::Template))
146 .chain(args.iter().map(UnitRef::Expression))
147 .chain(wake.map(UnitRef::Expression))
148 }
149}
150
151impl Node {
152 fn units(&self) -> impl Iterator<Item = UnitRef<'_>> {
153 let lets = self
154 .lets
155 .iter()
156 .map(|binding| UnitRef::Expression(&binding.expression));
157 let branches = self.branches.iter().flat_map(|branch| {
158 let guard = branch.guard.iter().map(UnitRef::Expression);
159 guard.chain(branch.outcome.units())
160 });
161 lets.chain(branches)
162 }
163
164 pub(crate) fn unit_spans(&self) -> impl Iterator<Item = (Span, Mode)> + '_ {
167 self.units().map(|unit| match unit {
168 UnitRef::Expression(unit) => (unit.span(), Mode::Expression),
169 UnitRef::Template(unit) => (unit.span(), Mode::Output),
170 })
171 }
172
173 pub(crate) fn reads(&self) -> Vec<InputRead> {
174 self.units().flat_map(|unit| unit.reads()).collect()
175 }
176}
177
178enum UnitRef<'a> {
180 Expression(&'a ExprUnit),
181 Template(&'a TemplateUnit),
182}
183
184impl UnitRef<'_> {
185 fn reads(&self) -> Vec<InputRead> {
186 match self {
187 UnitRef::Expression(unit) => unit.reads(),
188 UnitRef::Template(unit) => unit.reads(),
189 }
190 }
191}
192
193#[derive(Debug, Clone, PartialEq)]
195pub struct NodeInfo<'a> {
196 pub name: &'a str,
198 pub params: Vec<&'a str>,
200 pub annotations: &'a [Annotation],
202 pub is_entry: bool,
204 pub resumes_ask: bool,
206 pub jumps_to: Vec<&'a str>,
208 pub asks: Vec<AskInfo<'a>>,
210 pub branch_count: usize,
212}
213
214#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216pub struct AskInfo<'a> {
217 pub name: &'a str,
219 pub node: &'a str,
221}
222
223impl Flow {
224 pub fn name(&self) -> &str {
226 &self.name
227 }
228
229 pub fn edition(&self) -> u32 {
231 self.edition
232 }
233
234 pub fn hash(&self) -> String {
237 hex(&self.hash)
238 }
239
240 pub fn entries(&self) -> impl Iterator<Item = &str> {
242 self.entries.iter().map(|&id| self.node_name(id))
243 }
244
245 pub fn events(&self) -> impl Iterator<Item = &str> {
247 self.events.iter().map(SmolStr::as_str)
248 }
249
250 pub fn params(&self) -> &[Param] {
252 &self.params
253 }
254
255 pub fn asks(&self) -> impl Iterator<Item = &str> {
258 self.asks.iter().map(SmolStr::as_str)
259 }
260
261 pub fn annotations(&self) -> &[Annotation] {
263 &self.annotations
264 }
265
266 pub fn warnings(&self) -> &[Diagnostic] {
268 &self.warnings
269 }
270
271 pub fn nodes(&self) -> impl Iterator<Item = NodeInfo<'_>> {
273 self.nodes.iter().map(|node| self.info(node))
274 }
275
276 pub fn node(&self, name: &str) -> Option<NodeInfo<'_>> {
278 self.node_by_name(name)
279 .map(|id| self.info(&self.nodes[id.index()]))
280 }
281
282 fn info<'a>(&'a self, node: &'a Node) -> NodeInfo<'a> {
283 let mut jumps_to = Vec::new();
284 let mut seen = HashSet::new();
285 let mut asks = Vec::new();
286 for branch in &node.branches {
287 match &branch.outcome {
288 Outcome::Jump { node: target, .. } => {
289 if seen.insert(*target) {
290 jumps_to.push(self.node_name(*target));
291 }
292 }
293 Outcome::Ask {
294 name, node: target, ..
295 } => asks.push(AskInfo {
296 name: name.as_str(),
297 node: self.node_name(*target),
298 }),
299 _ => {}
300 }
301 }
302 NodeInfo {
303 name: &node.name,
304 params: node.params.iter().map(SmolStr::as_str).collect(),
305 annotations: &node.annotations,
306 is_entry: node.is_entry,
307 resumes_ask: node.resumes_ask,
308 jumps_to,
309 asks,
310 branch_count: node.branches.len(),
311 }
312 }
313
314 pub(crate) fn node_by_name(&self, name: &str) -> Option<NodeId> {
315 self.node_ids.get(name).copied()
316 }
317
318 pub(crate) fn node_name(&self, id: NodeId) -> &str {
319 &self.nodes[id.index()].name
320 }
321
322 pub fn source(&self) -> &str {
325 &self.source
326 }
327}
328
329pub(crate) fn hex(bytes: &[u8]) -> String {
331 const DIGITS: &[u8; 16] = b"0123456789abcdef";
332 let mut out = String::with_capacity(2 * bytes.len());
333 for &byte in bytes {
334 out.push(char::from(DIGITS[usize::from(byte >> 4)]));
335 out.push(char::from(DIGITS[usize::from(byte & 0xf)]));
336 }
337 out
338}