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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
/// AST and parsing logic for floxide-macros
use syn::{
braced, bracketed,
parse::{Parse, ParseStream},
Generics, Ident, Result, Token, Type, Visibility,
};
#[derive(Debug, Clone)]
pub struct CompositeArm {
pub action_path: Ident,
pub variant: Ident,
pub binding: Option<Ident>,
pub is_wildcard: bool,
pub guard: Option<syn::Expr>,
pub succs: Vec<Ident>,
}
#[derive(Debug, Clone)]
pub enum EdgeKind {
Direct {
succs: Vec<Ident>,
on_failure: Option<Vec<Ident>>,
},
Composite(Vec<CompositeArm>),
}
#[derive(Debug, Clone)]
pub struct WorkflowDef {
pub vis: Visibility,
pub name: Ident,
pub generics: Generics,
pub fields: Vec<(Ident, Type, Option<Ident>)>,
pub start: Ident,
pub context: Type,
pub edges: Vec<(Ident, EdgeKind)>,
}
// Parsing logic for WorkflowDef (copy from floxide-macros/src/workflow.rs, adapted to use these types)
impl Parse for WorkflowDef {
fn parse(input: ParseStream) -> Result<Self> {
// parse optional visibility
let vis: Visibility = if input.peek(Token![pub]) {
input.parse()? // pub or pub(...) etc
} else {
Visibility::Inherited
};
// parse struct definition
input.parse::<Token![struct]>()?;
let name: Ident = input.parse()?;
let generics: Generics = input.parse()?;
// parse struct fields
let content;
braced!(content in input);
let mut fields = Vec::new();
while !content.is_empty() {
// Optional retry annotation: #[retry = policy]
let mut retry_policy: Option<Ident> = None;
if content.peek(Token![#]) {
content.parse::<Token![#]>()?;
let inner;
bracketed!(inner in content);
let attr_name: Ident = inner.parse()?;
if attr_name == "retry" {
inner.parse::<Token![=]>()?;
let pol: Ident = inner.parse()?;
retry_policy = Some(pol);
} else {
return Err(inner.error("unknown attribute, expected `retry`"));
}
}
// Field name and type
let fld: Ident = content.parse()?;
content.parse::<Token![:]>()?;
let ty: Type = content.parse()?;
if content.peek(Token![,]) {
content.parse::<Token![,]>()?;
}
fields.push((fld, ty, retry_policy));
}
// Now parse the rest in any order: start, context, edges
let mut start: Option<Ident> = None;
let mut context: Option<Type> = None;
let mut edges: Option<Vec<(Ident, EdgeKind)>> = None;
let mut seen = std::collections::HashSet::new();
while !input.is_empty() {
if input.peek(Ident) {
let fork = input.fork();
let kw = fork.parse::<Ident>()?;
match kw.to_string().as_str() {
"start" => {
if seen.contains("start") {
return Err(
input.error("Duplicate 'start' field in workflow definition.")
);
}
input.parse::<Ident>()?; // start
input.parse::<Token![=]>()?;
let s: Ident = input.parse()?;
input.parse::<Token![;]>()?;
start = Some(s);
seen.insert("start");
}
"context" => {
if seen.contains("context") {
return Err(
input.error("Duplicate 'context' field in workflow definition.")
);
}
input.parse::<Ident>()?; // context
input.parse::<Token![=]>()?;
let ty: Type = input.parse()?;
input.parse::<Token![;]>()?;
context = Some(ty);
seen.insert("context");
}
"edges" => {
if seen.contains("edges") {
return Err(
input.error("Duplicate 'edges' field in workflow definition.")
);
}
input.parse::<Ident>()?; // edges
let edges_content;
braced!(edges_content in input);
// Collect direct-success, direct-failure, and composite arms
let mut direct_success =
std::collections::HashMap::<Ident, Vec<Ident>>::new();
let mut direct_failure =
std::collections::HashMap::<Ident, Vec<Ident>>::new();
let mut composite_map =
std::collections::HashMap::<Ident, Vec<CompositeArm>>::new();
while !edges_content.is_empty() {
let src: Ident = edges_content.parse()?;
if edges_content.peek(Ident) {
// on_failure clause
let kw: Ident = edges_content.parse()?;
if kw == "on_failure" {
edges_content.parse::<Token![=>]>()?;
let nested;
braced!(nested in edges_content);
// expect bracketed fallback list
let succs_content;
bracketed!(succs_content in nested);
let fails: Vec<Ident> = succs_content
.parse_terminated(Ident::parse, Token![,])?
.into_iter()
.collect();
edges_content.parse::<Token![;]>()?;
direct_failure.insert(src.clone(), fails);
continue;
} else {
return Err(edges_content.error(
"Unexpected identifier. Expected `on_failure` or `=>`.",
));
}
}
// success or composite entry
edges_content.parse::<Token![=>]>()?;
if edges_content.peek(syn::token::Bracket) {
// direct edge: foo => [bar];
let succs_content;
bracketed!(succs_content in edges_content);
let succs: Vec<Ident> = succs_content
.parse_terminated(Ident::parse, Token![,])?
.into_iter()
.collect();
edges_content.parse::<Token![;]>()?;
direct_success.insert(src.clone(), succs);
} else if edges_content.peek(syn::token::Brace) {
// direct or composite edge: foo => { ... };
let nested;
braced!(nested in edges_content);
// Empty braces => direct edge with no successors
if nested.is_empty() {
edges_content.parse::<Token![;]>()?;
direct_success.insert(src.clone(), Vec::new());
} else if nested.peek(syn::token::Bracket) {
// legacy: foo => {[bar]};
let succs_content;
bracketed!(succs_content in nested);
let succs: Vec<Ident> = succs_content
.parse_terminated(Ident::parse, Token![,])?
.into_iter()
.collect();
edges_content.parse::<Token![;]>()?;
direct_success.insert(src.clone(), succs);
} else {
// composite arms
let mut arms = Vec::new();
while !nested.is_empty() {
let action_path: Ident = nested.parse()?;
nested.parse::<Token![::]>()?;
let variant: Ident = nested.parse()?;
let mut binding = None;
let mut is_wildcard = false;
if nested.peek(syn::token::Paren) {
let inner;
syn::parenthesized!(inner in nested);
if inner.peek(Token![_]) {
inner.parse::<Token![_]>()?;
is_wildcard = true;
} else if inner.peek(Ident) {
binding = Some(inner.parse()?);
} else {
return Err(inner.error(
"Expected identifier or _ in variant binding",
));
}
} else {
// No parens: always treat as wildcard for macro ergonomics
is_wildcard = true;
binding = None;
}
// Optional guard
let guard = if nested.peek(Token![if]) {
nested.parse::<Token![if]>()?;
Some(nested.parse()?)
} else {
None
};
nested.parse::<Token![=>]>()?;
let succs_content;
bracketed!(succs_content in nested);
let succs: Vec<Ident> = succs_content
.parse_terminated(Ident::parse, Token![,])?
.into_iter()
.collect();
nested.parse::<Token![;]>()?;
let arm = CompositeArm {
action_path: action_path.clone(),
variant: variant.clone(),
binding: binding.clone(),
is_wildcard,
guard: guard.clone(),
succs: succs.clone(),
};
// Debug log removed: parsed arm
arms.push(arm);
}
edges_content.parse::<Token![;]>()?;
// After parsing all arms for a composite edge, print them for debugging
// Debug log removed: parsed composite arms for source
composite_map.insert(src.clone(), arms);
}
} else {
return Err(edges_content
.error("Expected [ or { after => in edge definition"));
}
}
// Merge into final edges vector
let mut edges_vec = Vec::new();
// direct-success entries
for (src, succs) in direct_success.into_iter() {
let failure = direct_failure.remove(&src);
edges_vec.push((
src,
EdgeKind::Direct {
succs,
on_failure: failure,
},
));
}
// direct-failure-only entries
for (src, fails) in direct_failure.into_iter() {
edges_vec.push((
src,
EdgeKind::Direct {
succs: Vec::new(),
on_failure: Some(fails),
},
));
}
// composite entries
for (src, arms) in composite_map.into_iter() {
edges_vec.push((src, EdgeKind::Composite(arms)));
}
edges = Some(edges_vec);
seen.insert("edges");
}
other => {
return Err(input.error(format!(
"Unexpected identifier '{}'. Expected one of: start, context, edges.",
other
)));
}
}
} else {
return Err(input.error("Unexpected token in workflow definition. Expected 'start', 'context', or 'edges'."));
}
}
// Check required fields
let start = start
.ok_or_else(|| input.error("Missing required 'start' field in workflow definition."))?;
let context = context.unwrap_or_else(|| syn::parse_quote! { () });
let edges = edges
.ok_or_else(|| input.error("Missing required 'edges' field in workflow definition."))?;
Ok(WorkflowDef {
vis,
name,
generics,
fields,
start,
context,
edges,
})
}
}