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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use ;
/*macro_rules! count_tts {
() => { 0 };
($odd:tt $($a:tt $b:tt)*) => { (count_tts!($($a)*) << 1) | 1 };
($($a:tt $even:tt)*) => { count_tts!($($a)*) << 1 };
}*/
/// Creates a stateless, data-transforming processor struct.
///
/// This macro generates a struct that implements the `Processor<T>` trait,
/// taking data of a specific type, processing it with a given function,
/// and returning data of the same type.
///
/// # Parameters
/// - `$struct_name`: The name for the new processor struct.
/// - `$type`: The type of data the processor will operate on.
/// - `$process_fn`: A function or closure that takes a value of `$type` and returns a new value of `$type`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::processor;
/// use type_flow_traits::Processor;
///
/// fn to_uppercase(s: String) -> String {
/// s.to_uppercase()
/// }
///
/// processor!(UppercaseProcessor, String, to_uppercase);
///
/// let processed = UppercaseProcessor::process("hello".to_string());
/// assert_eq!(processed, "HELLO");
/// ```
;
}
/// Creates a stateless pipeline of processors.
///
/// This macro generates a struct that chains multiple `Processor` implementations
/// together into a single pipeline. The data flows through each processor in the
/// order they are specified.
///
/// # Parameters
/// - `$struct_name`: The name for the new pipeline struct.
/// - `$type`: The type of data the pipeline will operate on.
/// - `$($processor: ty),+`: A comma-separated list of processor types that implement `Processor<$type>`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{processor, processor_pipeline};
/// use type_flow_traits::Processor;
///
/// processor!(AddOne, i32, |x| x + 1);
/// processor!(MulTwo, i32, |x| x * 2);
///
/// processor_pipeline!(MyPipeline, i32, AddOne, MulTwo);
///
/// let result = MyPipeline::process(10);
/// assert_eq!(result, 22); // (10 + 1) * 2
/// ```
}
/// Creates a generic, stateless processor pipeline.
///
/// This macro generates a struct that is generic over its processors.
/// It chains them together, and the pipeline itself implements `Processor`.
/// This allows for creating flexible pipelines where the exact processors can
/// be specified at a later time.
///
/// # Parameters
/// - `$struct_name`: The name for the new generic pipeline struct.
/// - `$processor_type`: The data type the pipeline operates on.
/// - `$($type_param:ident),+`: A comma-separated list of generic type parameter identifiers.
/// Each will be constrained to implement `Processor<$processor_type>`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{processor, type_flow_processor_pipeline};
/// use type_flow_traits::Processor;
///
/// processor!(AddTen, i32, |x| x + 10);
/// processor!(DivTwo, i32, |x| x / 2);
///
/// type_flow_processor_pipeline!(GenericPipeline, i32, P1, P2);
///
/// let result = GenericPipeline::<AddTen, DivTwo>::process(20);
/// assert_eq!(result, 15); // (20 + 10) / 2
/// ```
/// Creates a stateless, in-place processor that can return an error.
///
/// This macro generates a struct that implements the `InPlaceProcessor<T, E>` trait.
/// It processes data by mutating it directly and can fail with a specified error type.
///
/// # Parameters
/// - `$struct_name`: The name for the new processor struct.
/// - `$type`: The type of data to be processed (will be passed as `&mut`).
/// - `$error_type`: The error type for the `Result`.
/// - `$process_fn`: A function or closure that takes `&mut $type` and returns a `Result<(), $error_type>`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::inplace_processor;
/// use type_flow_traits::InPlaceProcessor;
///
/// #[derive(Debug)]
/// struct MyError;
///
/// fn increment(data: &mut i32) -> Result<(), MyError> {
/// if *data > 100 {
/// return Err(MyError);
/// }
/// *data += 1;
/// Ok(())
/// }
///
/// inplace_processor!(IncrementProcessor, i32, MyError, increment);
///
/// let mut value = 5;
/// IncrementProcessor::process(&mut value).unwrap();
/// assert_eq!(value, 6);
/// ```
;
}
/// Creates a stateless pipeline of in-place processors.
///
/// This macro generates a struct that chains multiple `InPlaceProcessor` implementations.
/// Data is mutated by each processor in the pipeline sequence. If any processor returns an error,
/// the pipeline stops and forwards the error.
///
/// # Parameters
/// - `$struct_name`: The name for the new pipeline struct.
/// - `$type`: The type of data the pipeline will operate on.
/// - `$error_type`: The common error type for the processors.
/// - `$($processor: ty),+`: A comma-separated list of types that implement `InPlaceProcessor<$type, $error_type>`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{inplace_processor, inplace_processor_pipeline};
/// use type_flow_traits::InPlaceProcessor;
/// #[derive(Debug)]
/// struct MyError;
///
/// inplace_processor!(Add, i32, MyError, |x: &mut i32| { *x += 1; Ok(()) });
/// inplace_processor!(Mul, i32, MyError, |x: &mut i32| { *x *= 2; Ok(()) });
///
/// inplace_processor_pipeline!(MyInPlacePipeline, i32, MyError, Add, Mul);
///
/// let mut value = 10;
/// MyInPlacePipeline::process(&mut value).unwrap();
/// assert_eq!(value, 22); // (10 + 1) * 2
/// ```
}
/// Creates a generic, stateless, in-place processor pipeline.
///
/// This macro generates a struct that is generic over its `InPlaceProcessor`s.
/// This allows for creating flexible in-place pipelines where the exact processors can
/// be specified later.
///
/// # Parameters
/// - `$struct_name`: The name for the new generic pipeline struct.
/// - `$processor_type`: The data type the pipeline operates on.
/// - `$processor_error_type`: The common error type.
/// - `$($type_param:ident),+`: A comma-separated list of generic type parameter identifiers,
/// each constrained to implement `InPlaceProcessor`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{inplace_processor, type_flow_inplace_processor_pipeline};
/// use type_flow_traits::InPlaceProcessor;
/// #[derive(Debug)]
/// struct MyError;
///
/// inplace_processor!(AddTen, i32, MyError, |x: &mut i32| { *x += 10; Ok(()) });
/// inplace_processor!(DivTwo, i32, MyError, |x: &mut i32| { *x /= 2; Ok(()) });
///
/// type_flow_inplace_processor_pipeline!(GenericInPlacePipeline, i32, MyError, P1, P2);
///
/// let mut value = 20;
/// GenericInPlacePipeline::<AddTen, DivTwo>::process(&mut value).unwrap();
/// assert_eq!(value, 15); // (20 + 10) / 2
/// ```
/// Creates a stateless processor that transforms data from an input type to an output type.
///
/// This macro generates a struct implementing the `TransformProcessor<I, O>` trait.
///
/// # Parameters
/// - `$struct_name`: The name for the new processor struct.
/// - `$input_type`: The type of the input data.
/// - `$output_type`: The type of the output data.
/// - `$process_fn`: A function or closure that takes `$input_type` and returns `$output_type`.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{transform_processor};
/// use type_flow_traits::TransformProcessor;
///
/// // This processor transforms an i32 to a String.
/// transform_processor!(IntToString, i32, String, |x: i32| x.to_string());
///
/// let result = IntToString::process(123);
/// assert_eq!(result, "123");
/// ```
;
}
/// Creates a stateful processor that maintains its own internal state.
///
/// This macro generates a struct that implements `StatefulProcessor<T>`. The processing logic
/// can read and modify the internal state during its operation.
///
/// Two variants are available:
/// 1. Takes a state type and requires the state to be provided via a `new()` constructor.
/// 2. Takes a state type and an initial value expression, providing a `new()` constructor with no arguments.
///
/// # Arms
///
/// ## 1. With `new(state)` constructor
/// - `$struct_name`: Name of the processor struct.
/// - `$state_type`: Type of the internal state.
/// - `$type`: Type of the data to be processed.
/// - `$process_fn`: A closure `|state, data| -> new_data`.
///
/// ## 2. With `new()` constructor and initial state
/// - `$struct_name`: Name of the processor struct.
/// - `$state_type`: Type of the internal state.
/// - `$state_value`: An expression that evaluates to the initial state.
/// - `$type`: Type of the data to be processed.
/// - `$process_fn`: A closure `|state, data| -> new_data`.
///
/// # Examples
///
/// ### 1. With `new(state)`
/// ```
/// use type_flow_macros::stateful_processor;
/// use type_flow_traits::StatefulProcessor;
///
/// // A processor that adds a configurable number to the input.
/// stateful_processor!(AddN, i32, i32, |state: &mut i32, data: i32| {
/// data + *state
/// });
///
/// let mut processor = AddN::new(5); // State is 5
/// assert_eq!(processor.process(10), 15);
/// assert_eq!(processor.process(20), 25); // State remains 5
/// ```
///
/// ### 2. With initial state value
/// ```
/// use type_flow_macros::stateful_processor;
/// use type_flow_traits::StatefulProcessor;
/// // A processor that accumulates values.
/// stateful_processor!(Accumulator, i32, 0, i32, |state: &mut i32, data: i32| {
/// *state += data;
/// *state // returns the new accumulated value
/// });
///
/// let mut processor = Accumulator::new();
/// assert_eq!(processor.process(5), 5);
/// assert_eq!(processor.process(10), 15);
/// assert_eq!(processor.process(-3), 12);
/// ```
};
// Version with direct state value
=>
}
};
}
/// Creates a pipeline of stateful processors.
///
/// This macro generates a struct that holds multiple stateful processors and
/// processes data through them in sequence. Each processor in the pipeline
/// maintains its own state.
///
/// # Arms
///
/// ## 1. With named fields for processors
/// - `$pipeline_name`: Name for the pipeline struct.
/// - `$data_type`: The type of data the pipeline operates on.
/// - `$($processor_field:ident: $processor_type:ty),+`: A comma-separated list of `field: Type` pairs for the processors.
///
/// ## 2. With auto-generated fields (delegates to a proc-macro)
/// - `$pipeline_name`: Name for the pipeline struct.
/// - `$data_type`: The type of data the pipeline operates on.
/// - `$($processor_type:ty),+`: A comma-separated list of processor types.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{stateful_processor, stateful_processor_pipeline};
/// use type_flow_traits::StatefulProcessor;
///
/// // A processor that adds its call count to the data
/// stateful_processor!(CallCountAdder, i32, 0, i32, |state: &mut i32, data: i32| {
/// *state += 1;
/// data + *state
/// });
///
/// // A processor that multiplies data by its call count
/// stateful_processor!(CallCountMultiplier, i32, 0, i32, |state: &mut i32, data: i32| {
/// *state += 1;
/// data * *state
/// });
///
/// // Arm 1: Named fields
/// stateful_processor_pipeline!(MyStatefulPipeline, i32, adder: CallCountAdder, multiplier: CallCountMultiplier);
///
/// let mut pipeline = MyStatefulPipeline::new(CallCountAdder::new(), CallCountMultiplier::new());
/// // First run: process(10) -> adder(state=1) -> 11 -> multiplier(state=1) -> 11
/// assert_eq!(pipeline.process(10), 11);
/// // Second run: process(10) -> adder(state=2) -> 12 -> multiplier(state=2) -> 24
/// assert_eq!(pipeline.process(10), 24);
///
/// // Arm 2: Auto-generated fields
/// stateful_processor_pipeline!(MyAnonPipeline, i32, CallCountMultiplier, CallCountAdder);
/// let mut anon_pipeline = MyAnonPipeline::new(CallCountMultiplier::new(), CallCountAdder::new());
/// assert_eq!(anon_pipeline.process(5), 6); // (5+1)*1
/// assert_eq!(anon_pipeline.process(5), 12); // (5+2)*2
/// ```
};
=> ;
}
/// Creates a stateful, in-place processor that can return an error.
///
/// This macro generates a struct that implements `InPlaceStatefulProcessor<T, E>`,
/// combining state management with in-place mutation and error handling.
///
/// # Arms
///
/// ## 1. With `new(state)` constructor
/// - `$struct_name`: Name of the processor struct.
/// - `$state_type`: Type of the internal state.
/// - `$type`: Type of data to process.
/// - `$error_type`: Error type for the `Result`.
/// - `$process_fn`: A closure `|state, data| -> Result<(), E>`.
///
/// ## 2. With `new()` constructor and initial state
/// - `$struct_name`: Name of the processor struct.
/// - `$state_type`: Type of the internal state.
/// - `$state_value`: An expression for the initial state.
/// - `$type`: Type of data to process.
/// - `$error_type`: Error type for the `Result`.
/// - `$process_fn`: A closure `|state, data| -> Result<(), E>`.
///
/// # Examples
///
/// ### 1. With `new(state)`
/// ```
/// use type_flow_macros::inplace_stateful_processor;
/// use type_flow_traits::InPlaceStatefulProcessor;
/// #[derive(Debug)]
/// struct MyError;
///
/// // Adds a configured value, fails if result exceeds a limit from state.
/// inplace_stateful_processor!(AddNLimited, (i32, i32), i32, MyError, |state: &mut (i32, i32), data: &mut i32| {
/// *data += state.0;
/// if *data > state.1 { Err(MyError) } else { Ok(()) }
/// });
///
/// let mut processor = AddNLimited::new((5, 100)); // Add 5, limit 100
/// let mut value = 10;
/// processor.process(&mut value).unwrap();
/// assert_eq!(value, 15);
/// ```
///
/// ### 2. With initial state
/// ```
/// use type_flow_macros::inplace_stateful_processor;
/// use type_flow_traits::InPlaceStatefulProcessor;
///
/// // Accumulates values in-place.
/// inplace_stateful_processor!(Accumulator, i32, 0, i32, (), |state: &mut i32, data: &mut i32| {
/// *state += *data;
/// *data = *state;
/// Ok(())
/// });
///
/// let mut processor = Accumulator::new();
/// let (mut val1, mut val2) = (5, 10);
/// processor.process(&mut val1).unwrap();
/// assert_eq!(val1, 5);
/// processor.process(&mut val2).unwrap();
/// assert_eq!(val2, 15); // 5 (previous state) + 10
/// ```
};
=>
}
};
}
/// Creates a pipeline of stateful, in-place processors.
///
/// Generates a struct that holds multiple `InPlaceStatefulProcessor`s and
/// processes data through them sequentially. Each processor can mutate the data
/// and its own state. The pipeline stops if any processor returns an error.
///
/// # Arms
///
/// ## 1. With named fields
/// - `$pipeline_name`: Name for the pipeline struct.
/// - `$data_type`: The type of data for the pipeline.
/// - `$error_type`: The common error type.
/// - `$($processor_field:ident: $processor_type:ty),+`: A list of `field: Type` pairs.
///
/// ## 2. With auto-generated fields (delegates to a proc-macro)
/// - `$pipeline_name`: Name for the pipeline struct.
/// - `$data_type`: The type of data for the pipeline.
/// - `$error_type`: The common error type.
/// - `$($processor_type:ty),+`: A list of processor types.
///
/// # Examples
///
/// ```
/// use type_flow_macros::{inplace_stateful_processor, inplace_stateful_processor_pipeline};
/// use type_flow_traits::InPlaceStatefulProcessor;
/// use type_flow_proc_macros::inplace_stateful_processor_pipeline_with_index;
/// #[derive(Debug)]
/// struct MyError;
///
/// // Processor that adds its call count to data.
/// inplace_stateful_processor!(CounterAdd, i32, 0, i32, MyError, |state: &mut i32, data: &mut i32| {
/// *state += 1;
/// *data += *state;
/// Ok(())
/// });
///
/// // Processor that multiplies data by its call count.
/// inplace_stateful_processor!(CounterMul, i32, 0, i32, MyError, |state: &mut i32, data: &mut i32| {
/// *state += 1;
/// *data *= *state;
/// Ok(())
/// });
///
/// // Arm 1: Named fields
/// inplace_stateful_processor_pipeline!(MyPipeline, i32, MyError, adder: CounterAdd, multiplier: CounterMul);
///
/// let mut pipeline = MyPipeline::new(CounterAdd::new(), CounterMul::new());
/// let mut value = 10;
///
/// // First run: value=10 -> adder(s=1) -> 11 -> multiplier(s=1) -> 11
/// pipeline.process(&mut value).unwrap();
/// assert_eq!(value, 11);
///
/// // Second run: value=11 -> adder(s=2) -> 13 -> multiplier(s=2) -> 26
/// pipeline.process(&mut value).unwrap();
/// assert_eq!(value, 26);
/// ```
};
=> ;
}
/// Creates a generic, stateful, in-place processor pipeline with type-level permutation capabilities.
///
/// This powerful macro generates a pipeline struct that is generic over its
/// `InPlaceStatefulProcessor`s. It automatically implements traits that allow the
/// order of processors to be rearranged at compile time through type manipulation.
///
/// # Supported Operations
///
/// The generated pipeline supports the following type-level transformations through trait implementations:
///
/// - `Reverse`: Reverses the order of processors in the pipeline
/// - `ShiftLeft`: Rotates all processors one position left
/// - `ShiftRight`: Rotates all processors one position right
/// - `SwapStartEnd`: Exchanges the first and last processors
/// - `SwapArbitraryProcessors<I, J>`: Swaps processors at any two positions
///
/// # Macro Parameters
///
/// ## Variant 1: With named fields
/// - `$struct_name`: Name for the generated pipeline struct
/// - `$processor_type`: Type of data flowing through the pipeline
/// - `$processor_error_type`: Common error type used by all processors
/// - `$($field_name:ident : $type_param:ident),+`: Field names and corresponding generic type parameters
///
/// ## Variant 2: With processor count
/// - `$struct_name`: Name for the generated pipeline struct
/// - `$processor_type`: Type of data flowing through the pipeline
/// - `$processor_error_type`: Common error type used by all processors
/// - `$number_of_processors`: A literal integer specifying how many processors the pipeline should support
///
/// # Examples
///
/// ```
/// use type_flow_macros::{
/// type_flow_inplace_stateful_processor_pipeline,
/// inplace_stateful_processor
/// };
/// use type_flow_proc_macros::implement_processor_swapping;
/// use type_flow_traits::{InPlaceStatefulProcessor,
/// Reverse, ShiftLeft};
/// #[derive(Debug)]
/// struct MyError;
///
/// // These processors have a state type of `()` and don't use it.
/// inplace_stateful_processor!(AddOne, (), i32, MyError, |_, d: &mut i32| { *d += 1; Ok(()) });
/// inplace_stateful_processor!(MulTwo, (), i32, MyError, |_, d: &mut i32| { *d *= 2; Ok(()) });
/// inplace_stateful_processor!(SubThree, (), i32, MyError, |_, d: &mut i32| { *d -= 3; Ok(()) });
///
/// // Using the variant of the macro that creates field names from type names.
/// type_flow_inplace_stateful_processor_pipeline!(
/// MyPipeline, i32, MyError, a: A, b: B, c: C
/// );
///
/// // Original pipeline
/// let mut pipeline = MyPipeline::new(AddOne::new(()), MulTwo::new(()), SubThree::new(()));
/// let mut value = 10;
/// pipeline.process(&mut value).unwrap();
/// assert_eq!(value, 19); // (10 + 1) * 2 - 3 = 19
///
/// // Reversed pipeline. Note: .reverse() consumes the pipeline and returns a new type.
/// let mut reversed_pipeline = MyPipeline::new(AddOne::new(()), MulTwo::new(()), SubThree::new(()))
/// .reverse();
/// let mut value_rev = 10;
/// reversed_pipeline.process(&mut value_rev).unwrap();
/// assert_eq!(value_rev, 15); // (10 - 3) * 2 + 1 = 15
///
/// // Shifted pipeline
/// let mut shifted_pipeline = MyPipeline::new(AddOne::new(()), MulTwo::new(()), SubThree::new(()))
/// .shift_left();
/// let mut value_shift = 10;
/// shifted_pipeline.process(&mut value_shift).unwrap();
/// assert_eq!(value_shift, 18); // (10 * 2 - 3) + 1 = 18
/// ```