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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! # Graph Macros
//!
//! Macros for declarative graph construction with minimal syntax.
//!
//! This module provides macros that allow you to define graphs using a concise,
//! declarative syntax, reducing boilerplate and improving readability.
/// Declarative macro for constructing graphs with minimal syntax.
///
/// The `graph!` macro allows you to define graphs using a concise, declarative syntax
/// that reduces boilerplate and improves readability compared to the traditional Graph API.
///
/// # Syntax
///
/// ```rust,no_run
/// use streamweave::graph;
///
/// let graph = graph! {
/// node1: Node1::new("node1"),
/// node2: Node2::new("node2"),
/// // Optional connections section
/// ; node1.out => node2.in
/// };
/// ```
///
/// # Node Definitions
///
/// Nodes are defined using the pattern `name: NodeInstance`, where:
/// - `name` is an identifier that will be used to reference the node in connections
/// - `NodeInstance` is an expression that evaluates to a node instance
/// - Multiple nodes are separated by commas
///
/// # Connections
///
/// Connections are optional and specified after a semicolon. Supported patterns:
///
/// ## Node-to-Node Connections
///
/// ```rust,no_run
/// # use streamweave::graph;
/// graph! {
/// source: SourceNode::new(),
/// sink: SinkNode::new(),
/// ; source.out => sink.in
/// };
/// ```
///
/// All ports must be explicitly named. No default ports or sequential shortcuts.
///
/// ## Graph Inputs
///
/// ### With Initial Value
///
/// ```rust,no_run
/// # use streamweave::graph;
/// graph! {
/// node: Node::new(),
/// graph.config: 42i32 => node.in
/// };
/// ```
///
/// The value is sent via a channel when the graph is built.
///
/// ### Without Value (Runtime Connection)
///
/// ```rust,no_run
/// # use streamweave::graph;
/// graph! {
/// node: Node::new(),
/// ; graph.input => node.in
/// };
/// ```
///
/// External channels must be connected at runtime.
///
/// ## Graph Outputs
///
/// ```rust,no_run
/// # use streamweave::graph;
/// graph! {
/// node: Node::new(),
/// node.out => graph.output
/// };
/// ```
///
/// # Restrictions
///
/// - **Reserved Node Name**: Node names cannot be `"graph"` - this name is reserved for graph I/O namespace
/// - **Unique Node Names**: All node names must be unique within the graph
/// - **Fan-Out Not Supported**: Each output port can only connect to one input port. Attempting to connect
/// the same source port to multiple targets will panic at build time.
/// - **Fan-In Allowed**: Multiple sources can connect to the same target port (fan-in is supported)
/// - **Explicit Ports Required**: All connections must explicitly specify port names on both source and target sides
///
/// # Examples
///
/// ## Simple Linear Pipeline
///
/// ```rust,no_run
/// # use streamweave::graph;
/// let graph = graph! {
/// producer: ProducerNode::new(),
/// transform: TransformNode::new(),
/// sink: SinkNode::new(),
/// producer.out => transform.in,
/// transform.out => sink.in
/// };
/// ```
///
/// ## Graph I/O with Values
///
/// ```rust,no_run
/// # use streamweave::graph;
/// let graph = graph! {
/// transform: TransformNode::new(),
/// graph.config: 100i32 => transform.in,
/// transform.out => graph.result
/// };
/// ```
///
/// ## Fan-In Pattern
///
/// ```rust,no_run
/// # use streamweave::graph;
/// let graph = graph! {
/// source1: SourceNode::new(),
/// source2: SourceNode::new(),
/// merge: MergeNode::new(),
/// ; source1.out => merge.in1,
/// source2.out => merge.in2
/// };
/// ```
///
/// # Error Handling
///
/// The macro will panic at build time if:
/// - A node is named `"graph"` (compile-time error via `validate_node_name!`)
/// - Fan-out is detected (runtime panic in `GraphBuilder::connect`)
/// - Invalid ports or nodes are referenced (errors from `Graph::add_edge`)
///
/// # See Also
///
/// - [`GraphBuilder`](crate::graph_builder::GraphBuilder) - Fluent API alternative
/// - [`Graph`](crate::graph::Graph) - Core graph type
/// - Examples in `examples/` directory
/// Helper macro to parse items (nodes and connections mixed).
///
/// This macro recursively processes items one at a time, distinguishing between
/// node definitions (`name: expr`) and connection patterns (`source.port => target.port`).
///
/// This macro is used internally by the `graph!` macro and should not be called directly.
/// Parses connection patterns and generates GraphBuilder method calls.
///
/// This macro is used internally by the `graph!` macro to parse connection syntax
/// and generate the appropriate builder method calls.
///
/// # Supported Patterns
///
/// ## Node-to-Node Connections
///
/// `source_node.source_port => target_node.target_port`
///
/// ```rust,no_run
/// # use streamweave::graph_builder::GraphBuilder;
/// # use streamweave::parse_connections;
/// let mut builder = GraphBuilder::new("test");
/// builder = parse_connections!(builder, node1.out => node2.in);
/// ```
///
/// ## Graph Inputs
///
/// With value: `graph.input_name: value => node.port`
///
/// ```rust,no_run
/// # use streamweave::graph_builder::GraphBuilder;
/// # use streamweave::parse_connections;
/// let mut builder = GraphBuilder::new("test");
/// builder = parse_connections!(builder, graph.config: 42i32 => node.in);
/// ```
///
/// Without value: `graph.input_name => node.port`
///
/// ```rust,no_run
/// # use streamweave::graph_builder::GraphBuilder;
/// # use streamweave::parse_connections;
/// let mut builder = GraphBuilder::new("test");
/// builder = parse_connections!(builder, graph.input => node.in);
/// ```
///
/// ## Graph Outputs
///
/// `node.port => graph.output_name`
///
/// ```rust,no_run
/// # use streamweave::graph_builder::GraphBuilder;
/// # use streamweave::parse_connections;
/// let mut builder = GraphBuilder::new("test");
/// builder = parse_connections!(builder, node.out => graph.result);
/// ```
///
/// # Multiple Connections
///
/// Multiple connections can be specified, separated by commas:
///
/// ```rust,no_run
/// # use streamweave::graph_builder::GraphBuilder;
/// # use streamweave::parse_connections;
/// let mut builder = GraphBuilder::new("test");
/// builder = parse_connections!(
/// builder,
/// node1.out => node2.in,
/// node2.out => node3.in,
/// graph.input => node1.in,
/// node3.out => graph.output
/// );
/// ```
///
/// # Restrictions
///
/// - **Fan-out Not Supported**: Same source port cannot connect to multiple targets
/// - **Fan-in Allowed**: Multiple sources can connect to the same target port
/// - **Rule Ordering**: Graph I/O patterns must be matched before node-to-node patterns
/// (handled internally by macro rule ordering)
///
/// # Usage
///
/// This macro is typically used internally by `graph!`, but can be used directly
/// for programmatic graph construction.
/// Validates that a node name is not "graph".
///
/// This macro is used internally by the `graph!` macro to ensure that no node
/// is named "graph", which is reserved for the graph I/O namespace.
///
/// # Usage
///
/// This macro is typically used internally by `graph!`, but can be used directly:
///
/// ```rust,compile_fail
/// # use streamweave::validate_node_name;
/// validate_node_name!("graph"); // This will cause a compile error
/// ```
///
/// # Errors
///
/// If the node name is "graph", this macro will emit a `compile_error!` with
/// a helpful message explaining that "graph" is reserved for graph I/O namespace.