cu29_derive/
lib.rs

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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use std::fs::read_to_string;
use syn::meta::parser;
use syn::Fields::{Named, Unnamed};
use syn::{
    parse_macro_input, parse_quote, parse_str, Field, Fields, ItemImpl, ItemStruct, LitStr, Type,
    TypeTuple,
};

use crate::utils::config_id_to_enum;
use cu29_runtime::config::read_configuration;
use cu29_runtime::config::CuConfig;
use cu29_runtime::curuntime::{
    compute_runtime_plan, find_task_type_for_id, CuExecutionLoop, CuExecutionUnit, CuTaskType,
};

#[cfg(feature = "macro_debug")]
use format::{highlight_rust_code, rustfmt_generated_code};
use proc_macro2::Ident;

mod format;
mod utils;

// TODO: this needs to be determined when the runtime is sizing itself.
const DEFAULT_CLNB: usize = 10;

#[inline]
fn int2sliceindex(i: u32) -> syn::Index {
    syn::Index::from(i as usize)
}

/// Generates the CopperList content type from a config.
/// gen_cumsgs!("path/to/config.toml")
/// It will create a new type called CuMsgs you can pass to the log reader for decoding:
#[proc_macro]
pub fn gen_cumsgs(config_path_lit: TokenStream) -> TokenStream {
    let config = parse_macro_input!(config_path_lit as LitStr).value();
    #[cfg(feature = "macro_debug")]
    eprintln!("[gen culist support with {:?}]", config);
    let cuconfig = read_config(&config);
    let runtime_plan: CuExecutionLoop =
        compute_runtime_plan(&cuconfig).expect("Could not compute runtime plan");

    // Give a name compatible with a struct to match the task ids to their output in the CuMsgs tuple.
    let all_tasks_member_ids: Vec<String> = cuconfig
        .get_all_nodes()
        .iter()
        .map(|(_, node)| utils::config_id_to_struct_member(node.get_id().as_str()))
        .collect();

    // All accesses are linear on the culist but the id of the tasks is random (determined by the Ron declaration order).
    // This records the task ids in call order.
    let taskid_order: Vec<usize> = runtime_plan
        .steps
        .iter()
        .filter_map(|unit| match unit {
            CuExecutionUnit::Step(step) => Some(step.node_id as usize),
            _ => None,
        })
        .collect();

    #[cfg(feature = "macro_debug")]
    eprintln!(
        "[The CuMsgs matching tasks ids are {:?}]",
        taskid_order
            .iter()
            .map(|i| all_tasks_member_ids[*i].clone())
            .collect::<Vec<_>>()
    );

    let support = gen_culist_support(&runtime_plan, &taskid_order, &all_tasks_member_ids);

    let with_uses = quote! {
        mod cumsgs {
            use cu29::bincode::Encode as _Encode;
            use cu29::bincode::enc::Encoder as _Encoder;
            use cu29::bincode::error::EncodeError as _EncodeError;
            use cu29::bincode::Decode as _Decode;
            use cu29::bincode::de::Decoder as _Decoder;
            use cu29::bincode::error::DecodeError as _DecodeError;
            use cu29::copperlist::CopperList as _CopperList;
            use cu29::cutask::CuMsgMetadata as _CuMsgMetadata;
            use cu29::cutask::CuMsg as _CuMsg;
            #support
        }
        use cumsgs::CuMsgs;
    };
    with_uses.into()
}

/// Build the inner support of the copper list.
fn gen_culist_support(
    runtime_plan: &CuExecutionLoop,
    taskid_call_order: &[usize],
    all_tasks_as_struct_member_name: &Vec<String>,
) -> proc_macro2::TokenStream {
    #[cfg(feature = "macro_debug")]
    eprintln!("[Extract msgs types]");
    let all_msgs_types_in_culist_order = extract_msg_types(runtime_plan);

    let culist_size = all_msgs_types_in_culist_order.len();
    let task_indices: Vec<_> = taskid_call_order
        .iter()
        .map(|i| syn::Index::from(*i))
        .collect();

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the copperlist tuple]");
    let msgs_types_tuple: TypeTuple = build_culist_tuple(&all_msgs_types_in_culist_order);

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the copperlist tuple bincode support]");
    let msgs_types_tuple_encode = build_culist_tuple_encode(&all_msgs_types_in_culist_order);
    let msgs_types_tuple_decode = build_culist_tuple_decode(&all_msgs_types_in_culist_order);

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the copperlist tuple debug support]");
    let msgs_types_tuple_debug = build_culist_tuple_debug(&all_msgs_types_in_culist_order);

    let collect_metadata_function = quote! {
        pub fn collect_metadata<'a>(culist: &'a CuList) -> [&'a _CuMsgMetadata; #culist_size] {
            [#( &culist.msgs.0.#task_indices.metadata, )*]
        }
    };

    let methods = itertools::multizip((all_tasks_as_struct_member_name, taskid_call_order)).map(
        |(name, output_position)| {
            let fn_name = format_ident!("get_{}_output", name);
            let payload_type = all_msgs_types_in_culist_order[*output_position].clone();
            let index = syn::Index::from(*output_position);
            quote! {
                pub fn #fn_name(&self) -> &_CuMsg<#payload_type> {
                    &self.0.#index
                }
            }
        },
    );

    // This generates a way to get the metadata of every single message of a culist at low cost
    quote! {
        #collect_metadata_function

        pub struct CuMsgs(#msgs_types_tuple);
        pub type CuList = _CopperList<CuMsgs>;

        impl CuMsgs {
            #(#methods)*

            fn get_tuple(&self) -> &#msgs_types_tuple {
                &self.0
            }

            fn get_tuple_mut(&mut self) -> &mut #msgs_types_tuple {
                &mut self.0
            }
        }

        // Adds the bincode support for the copper list tuple
        #msgs_types_tuple_encode
        #msgs_types_tuple_decode

        // Adds the debug support
        #msgs_types_tuple_debug
    }
}

fn gen_sim_support(runtime_plan: &CuExecutionLoop) -> proc_macro2::TokenStream {
    #[cfg(feature = "macro_debug")]
    eprintln!("[Sim: Build SimEnum]");
    let plan_enum: Vec<proc_macro2::TokenStream> = runtime_plan
        .steps
        .iter()
        .map(|unit| match unit {
            CuExecutionUnit::Step(step) => {
                let enum_entry_name = config_id_to_enum(step.node.get_id().as_str());
                let enum_ident = Ident::new(&enum_entry_name, proc_macro2::Span::call_site());
                let inputs: Vec<Type> = step
                    .input_msg_indices_types
                    .iter()
                    .map(|(_, t)| parse_str::<Type>(format!("_CuMsg<{t}>").as_str()).unwrap())
                    .collect();
                let output: Option<Type> = step
                    .output_msg_index_type
                    .as_ref()
                    .map(|(_, t)| parse_str::<Type>(format!("_CuMsg<{t}>").as_str()).unwrap());
                let no_output = parse_str::<Type>("_CuMsg<()>").unwrap();
                let output = output.as_ref().unwrap_or(&no_output);
                quote! {
                    #enum_ident(cu29::simulation::CuTaskCallbackState<'cl, (#(&'cl #inputs),*), &'cl mut #output>)
                }
            }
            CuExecutionUnit::Loop(_) => {
                todo!("Needs to be implemented")
            }
        })
        .collect();
    quote! {
        pub enum SimStep<'cl> {
            #(#plan_enum),*
        }
    }
}

/// Adds #[copper_runtime(config = "path", sim_mode = false/true)] to your application struct to generate the runtime.
/// if sim_mode is ommited, it is set to false.
/// This will add a "runtime" field to your struct and implement the "new" and "run" methods.
#[proc_macro_attribute]
pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
    #[cfg(feature = "macro_debug")]
    eprintln!("[entry]");
    let mut item_struct = parse_macro_input!(input as ItemStruct);
    let mut config_file: Option<LitStr> = None;
    let mut sim_mode = false;

    // Custom parser for the attribute arguments
    let attribute_config_parser = parser(|meta| {
        if meta.path.is_ident("config") {
            config_file = Some(meta.value()?.parse()?);
            Ok(())
        } else if meta.path.is_ident("sim_mode") {
            // Check if `sim_mode` has an explicit value (true/false)
            if meta.input.peek(syn::Token![=]) {
                meta.input.parse::<syn::Token![=]>()?;
                let value: syn::LitBool = meta.input.parse()?;
                sim_mode = value.value();
                Ok(())
            } else {
                // If no value is provided, default to true
                sim_mode = true;
                Ok(())
            }
        } else {
            Err(meta.error("unsupported property"))
        }
    });

    #[cfg(feature = "macro_debug")]
    eprintln!("[parse]");
    // Parse the provided args with the custom parser
    parse_macro_input!(args with attribute_config_parser);

    // Check if the config file was provided
    let config_file = config_file
        .expect("Expected config file attribute like #[CopperRuntime(config = \"path\")]")
        .value();

    let copper_config = read_config(&config_file);
    let copper_config_content = read_to_string(config_full_path(config_file.as_str())).expect(
        "Could not read the config file (should not happen because we just succeeded just before).",
    );

    #[cfg(feature = "macro_debug")]
    eprintln!("[runtime plan]");
    let runtime_plan: CuExecutionLoop =
        compute_runtime_plan(&copper_config).expect("Could not compute runtime plan");
    #[cfg(feature = "macro_debug")]
    eprintln!("{:?}", runtime_plan);

    #[cfg(feature = "macro_debug")]
    eprintln!("[extract tasks ids & types]");
    let (all_tasks_ids, all_tasks_cutype, all_tasks_types_names, all_tasks_types) =
        extract_tasks_types(&copper_config);

    let all_sim_tasks_types: Vec<Type> = all_tasks_ids
        .iter()
        .zip(&all_tasks_cutype)
        .zip(&all_tasks_types)
        .map(|((task_id, cutype), stype)| match cutype {
            CuTaskType::Source => {
                let msg_type = copper_config
                    .get_node_output_msg_type(task_id.as_str())
                    .unwrap_or_else(|| panic!("CuSrcTask {task_id} should have an outgoing connection with a valid output msg type"));
                let sim_task_name = format!("cu29::simulation::CuSimSrcTask<{msg_type}>");
                parse_str(sim_task_name.as_str()).unwrap_or_else(|_| panic!("Could not build the placeholder for simulation: {sim_task_name}"))
            }
            CuTaskType::Regular => stype.clone(),
            CuTaskType::Sink => {
                let msg_type = copper_config
                    .get_node_input_msg_type(task_id.as_str())
                    .unwrap_or_else(|| panic!("CuSinkTask {task_id} should have an incoming connection with a valid input msg type"));
                let sim_task_name = format!("cu29::simulation::CuSimSinkTask<{msg_type}>");
                parse_str(sim_task_name.as_str()).unwrap_or_else(|_| panic!("Could not build the placeholder for simulation: {sim_task_name}"))
            }
        })
        .collect();

    #[cfg(feature = "macro_debug")]
    eprintln!("[build task tuples]");
    // Build the tuple of all those types
    // note the extraneous , at the end is to make the tuple work even if this is only one element
    let task_types_tuple: TypeTuple = parse_quote! {
        (#(#all_tasks_types),*,)
    };

    let task_types_tuple_sim: TypeTuple = parse_quote! {
        (#(#all_sim_tasks_types),*,)
    };

    #[cfg(feature = "macro_debug")]
    eprintln!("[build monitor type]");
    let monitor_type = if let Some(monitor_config) = copper_config.get_monitor_config() {
        let monitor_type = parse_str::<Type>(monitor_config.get_type())
            .expect("Could not transform the monitor type name into a Rust type.");
        quote! { #monitor_type }
    } else {
        quote! { _NoMonitor }
    };

    #[cfg(feature = "macro_debug")]
    eprintln!("[build runtime field]");
    // add that to a new field
    let runtime_field: Field = if sim_mode {
        parse_quote! {
            copper_runtime: _CuRuntime<CuSimTasks, CuMsgs, #monitor_type, #DEFAULT_CLNB>
        }
    } else {
        parse_quote! {
            copper_runtime: _CuRuntime<CuTasks, CuMsgs, #monitor_type, #DEFAULT_CLNB>
        }
    };

    let name = &item_struct.ident;

    #[cfg(feature = "macro_debug")]
    eprintln!("[match struct anonymity]");
    match &mut item_struct.fields {
        Named(fields_named) => {
            fields_named.named.push(runtime_field);
        }
        Unnamed(fields_unnamed) => {
            fields_unnamed.unnamed.push(runtime_field);
        }
        Fields::Unit => {
            panic!("This struct is a unit struct, it should have named or unnamed fields. use struct Something {{}} and not struct Something;")
        }
    };

    #[cfg(feature = "macro_debug")]
    eprintln!("[gen instances]");

    let task_sim_instances_init_code = all_sim_tasks_types.iter().enumerate().map(|(index, ty)| {
        let additional_error_info = format!(
            "Failed to get create instance for {}, instance index {}.",
            all_tasks_types_names[index], index
        );

        quote! {
        <#ty>::new(all_instances_configs[#index]).map_err(|e| e.add_cause(#additional_error_info))?
        }
    }).collect::<Vec<_>>();

    // Generate the code to create instances of the nodes
    // It maps the types to their index
    let (task_instances_init_code,
        start_calls,
        stop_calls,
        preprocess_calls,
        postprocess_calls): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = itertools::multiunzip(all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, ty)| {
            let task_index = int2sliceindex(index as u32);
            let task_enum_name = config_id_to_enum(&all_tasks_ids[index]);
            let enum_name = Ident::new(&task_enum_name, proc_macro2::Span::call_site());
            let additional_error_info = format!(
                "Failed to get create instance for {}, instance index {}.",
                all_tasks_types_names[index], index
            );
            (
                quote! {
                    #ty::new(all_instances_configs[#index]).map_err(|e| e.add_cause(#additional_error_info))?
                },
                {
                    let monitoring_action = quote! {
                        let decision = self.copper_runtime.monitor.process_error(#index, _CuTaskState::Start, &error);
                        match decision {
                            _Decision::Abort => {
                                debug!("Start: ABORT decision from monitoring. Task '{}' errored out \
                                during start. Aborting all the other starts.", TASKS_IDS[#index]);
                                return Ok(());

                            }
                            _Decision::Ignore => {
                                debug!("Start: IGNORE decision from monitoring. Task '{}' errored out \
                                during start. The runtime will continue.", TASKS_IDS[#index]);
                            }
                            _Decision::Shutdown => {
                                debug!("Start: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                during start. The runtime cannot continue.", TASKS_IDS[#index]);
                                return Err(_CuError::new_with_cause("Task errored out during start.", error));
                            }
                        }
                    };

                    let call_sim_callback = if sim_mode {
                        quote! {
                            // Ask the sim if this task should be executed or overridden by the sim.
                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Start));

                            let doit = if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                let error: _CuError = reason.into();
                                #monitoring_action
                                false
                           }
                           else {
                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                           };
                        }
                    } else {
                        quote! {
                            let doit = true;  // in normal mode always execute the steps in the runtime.
                        }
                    };


                    quote! {
                        #call_sim_callback
                        if doit {
                            let task = &mut self.copper_runtime.tasks.#task_index;
                            if let Err(error) = task.start(&self.copper_runtime.clock) {
                                #monitoring_action
                            }
                        }
                    }
                },
                {
                    let monitoring_action = quote! {
                                let decision = self.copper_runtime.monitor.process_error(#index, _CuTaskState::Stop, &error);
                                match decision {
                                    _Decision::Abort => {
                                        debug!("Stop: ABORT decision from monitoring. Task '{}' errored out \
                                    during stop. Aborting all the other starts.", TASKS_IDS[#index]);
                                        return Ok(());

                                    }
                                    _Decision::Ignore => {
                                        debug!("Stop: IGNORE decision from monitoring. Task '{}' errored out \
                                    during stop. The runtime will continue.", TASKS_IDS[#index]);
                                    }
                                    _Decision::Shutdown => {
                                        debug!("Stop: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                    during stop. The runtime cannot continue.", TASKS_IDS[#index]);
                                        return Err(_CuError::new_with_cause("Task errored out during stop.", error));
                                    }
                                }
                        };
                    let call_sim_callback = if sim_mode {
                        quote! {
                            // Ask the sim if this task should be executed or overridden by the sim.
                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Stop));

                            let doit = if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                let error: _CuError = reason.into();
                                #monitoring_action
                                false
                           }
                           else {
                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                           };
                        }
                    } else {
                        quote! {
                            let doit = true;  // in normal mode always execute the steps in the runtime.
                        }
                    };
                    quote! {
                        #call_sim_callback
                        if doit {
                            let task = &mut self.copper_runtime.tasks.#task_index;
                            if let Err(error) = task.stop(&self.copper_runtime.clock) {
                                #monitoring_action
                            }
                        }
                    }
                },
                {
                    let monitoring_action = quote! {
                        let decision = self.copper_runtime.monitor.process_error(#index, _CuTaskState::Preprocess, &error);
                        match decision {
                            _Decision::Abort => {
                                debug!("Preprocess: ABORT decision from monitoring. Task '{}' errored out \
                                during preprocess. Aborting all the other starts.", TASKS_IDS[#index]);
                                return Ok(());

                            }
                            _Decision::Ignore => {
                                debug!("Preprocess: IGNORE decision from monitoring. Task '{}' errored out \
                                during preprocess. The runtime will continue.", TASKS_IDS[#index]);
                            }
                            _Decision::Shutdown => {
                                debug!("Preprocess: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                during preprocess. The runtime cannot continue.", TASKS_IDS[#index]);
                                return Err(_CuError::new_with_cause("Task errored out during preprocess.", error));
                            }
                        }
                    };
                    let call_sim_callback = if sim_mode {
                        quote! {
                            // Ask the sim if this task should be executed or overridden by the sim.
                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Preprocess));

                            let doit = if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                let error: _CuError = reason.into();
                                #monitoring_action
                                false
                            } else {
                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                            };
                        }
                    } else {
                        quote! {
                            let doit = true;  // in normal mode always execute the steps in the runtime.
                        }
                    };
                    quote! {
                        #call_sim_callback
                        if doit {
                            let task = &mut self.copper_runtime.tasks.#task_index;
                            if let Err(error) = task.preprocess(&self.copper_runtime.clock) {
                                #monitoring_action
                            }
                        }
                    }
                },
                {
                    let monitoring_action = quote! {
                        let decision = self.copper_runtime.monitor.process_error(#index, _CuTaskState::Postprocess, &error);
                        match decision {
                            _Decision::Abort => {
                                debug!("Postprocess: ABORT decision from monitoring. Task '{}' errored out \
                                during postprocess. Aborting all the other starts.", TASKS_IDS[#index]);
                                return Ok(());

                            }
                            _Decision::Ignore => {
                                debug!("Postprocess: IGNORE decision from monitoring. Task '{}' errored out \
                                during postprocess. The runtime will continue.", TASKS_IDS[#index]);
                            }
                            _Decision::Shutdown => {
                                debug!("Postprocess: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                during postprocess. The runtime cannot continue.", TASKS_IDS[#index]);
                                return Err(_CuError::new_with_cause("Task errored out during postprocess.", error));
                            }
                        }
                    };
                    let call_sim_callback = if sim_mode {
                        quote! {
                            // Ask the sim if this task should be executed or overridden by the sim.
                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Postprocess));

                            let doit = if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                let error: _CuError = reason.into();
                                #monitoring_action
                                false
                            } else {
                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                            };
                        }
                    } else {
                        quote! {
                            let doit = true;  // in normal mode always execute the steps in the runtime.
                        }
                    };
                    quote! {
                        #call_sim_callback
                        if doit {
                            let task = &mut self.copper_runtime.tasks.#task_index;
                            if let Err(error) = task.postprocess(&self.copper_runtime.clock) {
                                #monitoring_action
                            }
                        }
                    }
                }
            )
        })
    );

    // All accesses are linear on the culist but the id of the tasks is random (determined by the Ron declaration order).
    // This records the task ids in call order.
    let mut taskid_call_order: Vec<usize> = Vec::new();

    let runtime_plan_code: Vec<proc_macro2::TokenStream> = runtime_plan.steps
        .iter()
        .map(|unit| {
            match unit {
                CuExecutionUnit::Step(step) => {
                    #[cfg(feature = "macro_debug")]
                    eprintln!(
                        "{} -> {} as {:?}. task_id: {} Input={:?}, Output={:?}",
                        step.node.get_id(),
                        step.node.get_type(),
                        step.task_type,
                        step.node_id,
                        step.input_msg_indices_types,
                        step.output_msg_index_type
                    );

                    let node_index = int2sliceindex(step.node_id);
                    let task_instance = quote! { self.copper_runtime.tasks.#node_index };
                    let comment_str = format!(
                        "/// {} ({:?}) Id:{} I:{:?} O:{:?}",
                        step.node.get_id(),
                        step.task_type,
                        step.node_id,
                        step.input_msg_indices_types,
                        step.output_msg_index_type
                    );
                    let comment_tokens: proc_macro2::TokenStream = parse_str(&comment_str).unwrap();
                    let tid = step.node_id as usize;
                    taskid_call_order.push(tid);

                    let task_enum_name = config_id_to_enum(&all_tasks_ids[tid]);
                    let enum_name = Ident::new(&task_enum_name, proc_macro2::Span::call_site());

                    let process_call = match step.task_type {
                        CuTaskType::Source => {
                            if let Some((index, _)) = &step.output_msg_index_type {
                                let output_culist_index = int2sliceindex(*index);

                                let monitoring_action = quote! {
                                    let decision = self.copper_runtime.monitor.process_error(#tid, _CuTaskState::Process, &error);
                                    match decision {
                                        _Decision::Abort => {
                                            debug!("Process: ABORT decision from monitoring. Task '{}' errored out \
                                            during process. Skipping the processing of CL {}.", TASKS_IDS[#tid], id);
                                            self.copper_runtime.monitor.process_copperlist(&collect_metadata(&culist))?;
                                            self.copper_runtime.end_of_processing(id);
                                            return Ok(()); // this returns early from the one iteration call.

                                        }
                                        _Decision::Ignore => {
                                            debug!("Process: IGNORE decision from monitoring. Task '{}' errored out \
                                            during process. The runtime will continue with a forced empty message.", TASKS_IDS[#tid]);
                                            cumsg_output.clear_payload();
                                        }
                                        _Decision::Shutdown => {
                                            debug!("Process: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                            during process. The runtime cannot continue.", TASKS_IDS[#tid]);
                                            return Err(_CuError::new_with_cause("Task errored out during process.", error));
                                        }
                                    }
                                };
                                let call_sim_callback = if sim_mode {
                                    quote! {
                                        let doit = {
                                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Process((), cumsg_output)));
                                            if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                                let error: _CuError = reason.into();
                                                #monitoring_action
                                                false
                                            } else {
                                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                                            }
                                        };
                                     }
                                } else {
                                    quote! {
                                        let  doit = true;  // in normal mode always execute the steps in the runtime.
                                   }
                                };

                                quote! {
                                    {
                                        #comment_tokens
                                        {
                                            let cumsg_output = &mut msgs.#output_culist_index;
                                            #call_sim_callback
                                            cumsg_output.metadata.process_time.start = self.copper_runtime.clock.now().into();
                                            let maybe_error = if doit {
                                                #task_instance.process(&self.copper_runtime.clock, cumsg_output)
                                            } else {
                                                Ok(())
                                            };
                                            cumsg_output.metadata.process_time.end = self.copper_runtime.clock.now().into();
                                            if let Err(error) = maybe_error {
                                                #monitoring_action
                                            }
                                        }
                                    }
                                }
                            } else {
                                panic!("Source task should have an output message index.");
                            }
                        }
                        CuTaskType::Sink => {
                            // collect the indices
                            let indices = step.input_msg_indices_types.iter().map(|(index, _)| int2sliceindex(*index));
                            if let Some((output_index, _)) = &step.output_msg_index_type {
                                let output_culist_index = int2sliceindex(*output_index);

                                let monitoring_action = quote! {
                                    let decision = self.copper_runtime.monitor.process_error(#tid, _CuTaskState::Process, &error);
                                    match decision {
                                        _Decision::Abort => {
                                            debug!("Process: ABORT decision from monitoring. Task '{}' errored out \
                                            during process. Skipping the processing of CL {}.", TASKS_IDS[#tid], id);
                                            self.copper_runtime.monitor.process_copperlist(&collect_metadata(&culist))?;
                                            self.copper_runtime.end_of_processing(id);
                                            return Ok(()); // this returns early from the one iteration call.

                                        }
                                        _Decision::Ignore => {
                                            debug!("Process: IGNORE decision from monitoring. Task '{}' errored out \
                                            during process. The runtime will continue with a forced empty message.", TASKS_IDS[#tid]);
                                            cumsg_output.clear_payload();
                                        }
                                        _Decision::Shutdown => {
                                            debug!("Process: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                            during process. The runtime cannot continue.", TASKS_IDS[#tid]);
                                            return Err(_CuError::new_with_cause("Task errored out during process.", error));
                                        }
                                    }
                                };

                                let call_sim_callback = if sim_mode {
                                    quote! {
                                        let doit = {
                                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Process(cumsg_input, cumsg_output)));

                                            if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                                let error: _CuError = reason.into();
                                                #monitoring_action
                                                false
                                            } else {
                                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                                            }
                                        };
                                     }
                                } else {
                                    quote! {
                                        let doit = true;  // in normal mode always execute the steps in the runtime.
                                   }
                                };
                                quote! {
                                    {
                                        #comment_tokens
                                        let cumsg_input = (#(&msgs.#indices),*);
                                        // This is the virtual output for the sink
                                        let cumsg_output = &mut msgs.#output_culist_index;
                                        #call_sim_callback
                                        cumsg_output.metadata.process_time.start = self.copper_runtime.clock.now().into();
                                        let maybe_error = if doit {#task_instance.process(&self.copper_runtime.clock, cumsg_input)} else {Ok(())};
                                        cumsg_output.metadata.process_time.end = self.copper_runtime.clock.now().into();
                                        if let Err(error) = maybe_error {
                                            #monitoring_action
                                        }
                                    }
                                }
                            } else {
                                panic!("Sink tasks should have a virtual output message index.");
                            }
                        }
                        CuTaskType::Regular => {
                            let indices = step.input_msg_indices_types.iter().map(|(index, _)| int2sliceindex(*index));
                            if let Some((output_index, _)) = &step.output_msg_index_type {
                                let output_culist_index = int2sliceindex(*output_index);

                                let monitoring_action = quote! {
                                    let decision = self.copper_runtime.monitor.process_error(#tid, _CuTaskState::Process, &error);
                                    match decision {
                                        _Decision::Abort => {
                                            debug!("Process: ABORT decision from monitoring. Task '{}' errored out \
                                            during process. Skipping the processing of CL {}.", TASKS_IDS[#tid], id);
                                            self.copper_runtime.monitor.process_copperlist(&collect_metadata(&culist))?;
                                            self.copper_runtime.end_of_processing(id);
                                            return Ok(()); // this returns early from the one iteration call.

                                        }
                                        _Decision::Ignore => {
                                            debug!("Process: IGNORE decision from monitoring. Task '{}' errored out \
                                            during process. The runtime will continue with a forced empty message.", TASKS_IDS[#tid]);
                                            cumsg_output.clear_payload();
                                        }
                                        _Decision::Shutdown => {
                                            debug!("Process: SHUTDOWN decision from monitoring. Task '{}' errored out \
                                            during process. The runtime cannot continue.", TASKS_IDS[#tid]);
                                            return Err(_CuError::new_with_cause("Task errored out during process.", error));
                                        }
                                    }
                                };

                                let call_sim_callback = if sim_mode {
                                    quote! {
                                        let doit = {
                                            let ovr = sim_callback(SimStep::#enum_name(cu29::simulation::CuTaskCallbackState::Process(cumsg_input, cumsg_output)));

                                            if let cu29::simulation::SimOverride::Errored(reason) = ovr  {
                                                let error: _CuError = reason.into();
                                                #monitoring_action
                                                false
                                            }
                                            else {
                                                ovr == cu29::simulation::SimOverride::ExecuteByRuntime
                                            }
                                        };
                                     }
                                } else {
                                    quote! {
                                        let doit = true;  // in normal mode always execute the steps in the runtime.
                                   }
                                };
                                quote! {
                                    {
                                        #comment_tokens
                                        let cumsg_input = (#(&msgs.#indices),*);
                                        let cumsg_output = &mut msgs.#output_culist_index;
                                        #call_sim_callback
                                        cumsg_output.metadata.process_time.start = self.copper_runtime.clock.now().into();
                                        let maybe_error = if doit {#task_instance.process(&self.copper_runtime.clock, cumsg_input, cumsg_output)} else {Ok(())};
                                        cumsg_output.metadata.process_time.end = self.copper_runtime.clock.now().into();
                                        if let Err(error) = maybe_error {
                                            #monitoring_action
                                        }
                                    }
                                }
                            } else {
                                panic!("Regular task should have an output message index.");
                            }
                        }
                    };

                    process_call
                }
                CuExecutionUnit::Loop(_) => todo!("Needs to be implemented"),
            }
        }).collect();
    #[cfg(feature = "macro_debug")]
    eprintln!("[Culist access order:  {:?}]", taskid_call_order);

    // Give a name compatible with a struct to match the task ids to their output in the CuMsgs tuple.
    let all_tasks_member_ids: Vec<String> = all_tasks_ids
        .iter()
        .map(|name| utils::config_id_to_struct_member(name.as_str()))
        .collect();

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the copperlist support]");
    let culist_support: proc_macro2::TokenStream =
        gen_culist_support(&runtime_plan, &taskid_call_order, &all_tasks_member_ids);

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the sim support]");
    let sim_support: proc_macro2::TokenStream = gen_sim_support(&runtime_plan);

    let (new, run_one_iteration, start_all_tasks, stop_all_tasks, run) = if sim_mode {
        (
            quote! {
                pub fn new<F>(clock:_RobotClock, unified_logger: _Arc<_Mutex<_UnifiedLoggerWrite>>, config_override: Option<_CuConfig>, sim_callback: &mut F) -> _CuResult<Self>
                where F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
            quote! {
                pub fn run_one_iteration<F>(&mut self, sim_callback: &mut F) -> _CuResult<()>
                where F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
            quote! {
                pub fn start_all_tasks<F>(&mut self, sim_callback: &mut F) -> _CuResult<()>
                where F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
            quote! {
                pub fn stop_all_tasks<F>(&mut self, sim_callback: &mut F) -> _CuResult<()>
                where F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
            quote! {
                pub fn run<F>(&mut self, sim_callback: &mut F) -> _CuResult<()>
                where F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
        )
    } else {
        (
            quote! {
                pub fn new(clock:_RobotClock, unified_logger: _Arc<_Mutex<_UnifiedLoggerWrite>>, config_override: Option<_CuConfig>) -> _CuResult<Self>
            },
            quote! {
                pub fn run_one_iteration(&mut self) -> _CuResult<()>
            },
            quote! {
                pub fn start_all_tasks(&mut self) -> _CuResult<()>
            },
            quote! {
                pub fn stop_all_tasks(&mut self) -> _CuResult<()>
            },
            quote! {
                pub fn run(&mut self) -> _CuResult<()>
            },
        )
    };

    let sim_callback_arg = if sim_mode {
        Some(quote!(sim_callback))
    } else {
        None
    };

    let sim_callback_on_new_calls = all_tasks_ids.iter().enumerate().map(|(i, id)| {
        let enum_name = config_id_to_enum(id);
        let enum_ident = Ident::new(&enum_name, proc_macro2::Span::call_site());
        quote! {
            // the answer is ignored, we have to instantiate the tasks anyway.
            sim_callback(SimStep::#enum_ident(cu29::simulation::CuTaskCallbackState::New(all_instances_configs[#i].cloned())));
        }
    });

    let sim_callback_on_new = if sim_mode {
        Some(quote! {
            let all_instances_configs: Vec<Option<&_ComponentConfig>> = config
                .get_all_nodes()
                .iter()
                .map(|(_, node)| node.get_instance_config())
                .collect();
            #(#sim_callback_on_new_calls)*
        })
    } else {
        None
    };

    #[cfg(feature = "macro_debug")]
    eprintln!("[build the run method]");
    let run_method = quote! {

        #run_one_iteration {
            #(#preprocess_calls)*
            {
                let mut culist: &mut _ = &mut self.copper_runtime.copper_lists_manager.create().expect("Ran out of space for copper lists"); // FIXME: error handling.
                let id = culist.id;
                culist.change_state(cu29::copperlist::CopperListState::Processing);
                {
                    let msgs = &mut culist.msgs.0;
                    #(#runtime_plan_code)*
                } // drop(msgs);

                {
                    // End of CL monitoring
                    let md = collect_metadata(&culist);
                    let e2e = md.last().unwrap().process_time.end.unwrap() - md.first().unwrap().process_time.start.unwrap();
                    let e2en: u64 = e2e.into();
                } // drop(md);

                self.copper_runtime.monitor.process_copperlist(&collect_metadata(&culist))?;
                self.copper_runtime.end_of_processing(id);

           }// drop(culist); avoids a double mutable borrow
           #(#postprocess_calls)*
           Ok(())
        }

        #start_all_tasks {
            #(#start_calls)*
            self.copper_runtime.monitor.start(&self.copper_runtime.clock)?;
            Ok(())
        }

        #stop_all_tasks {
            #(#stop_calls)*
            self.copper_runtime.monitor.stop(&self.copper_runtime.clock)?;
            Ok(())
        }

        #run {
            self.start_all_tasks(#sim_callback_arg)?;
            let error = loop {
                let error = self.run_one_iteration(#sim_callback_arg);
                if error.is_err() {
                    break error;
                }
            };
            debug!("A task errored out: {}", &error);
            self.stop_all_tasks(#sim_callback_arg)?;
            error
        }
    };

    let tasks_type = if sim_mode {
        quote!(CuSimTasks)
    } else {
        quote!(CuTasks)
    };

    let tasks_instanciator = if sim_mode {
        quote!(tasks_instanciator_sim)
    } else {
        quote!(tasks_instanciator)
    };

    let runtime_impl = quote! {
        impl #name {

            #new {
                let config_filename = #config_file;
                let config = if config_override.is_some() {
                    let overriden_config = config_override.unwrap();
                    debug!("CuConfig: Overriden programmatically: {}", &overriden_config.serialize_ron());
                    overriden_config
                } else if std::path::Path::new(config_filename).exists() {
                    debug!("CuConfig: Reading configuration from file: {}", config_filename);
                    _read_configuration(config_filename)?
                } else {
                    let original_config = Self::get_original_config();
                    debug!("CuConfig: Using the original configuration the project was compiled with: {}", &original_config);
                    _read_configuration_str(original_config)?
                };

                // For simple cases we can say the section is just a bunch of Copper Lists.
                // But we can now have allocations outside of it so we can override it from the config.
                let mut default_section_size = std::mem::size_of::<CuList>() * 64;
                // Check if there is a logging configuration with section_size_mib
                if let Some(section_size_mib) = config.logging.as_ref().and_then(|l| l.section_size_mib) {
                    // Convert MiB to bytes
                    default_section_size = section_size_mib as usize * 1024usize * 1024usize;
                }
                let copperlist_stream = _stream_write::<CuList>(
                    unified_logger.clone(),
                    _UnifiedLogType::CopperList,
                    default_section_size,
                    // the 2 sizes are not directly related as we encode the CuList but we can
                    // assume the encoded size is close or lower than the non encoded one
                    // This is to be sure we have the size of at least a Culist and some.
                );

                let runtime = Ok(#name {
                    copper_runtime: _CuRuntime::<#tasks_type, CuMsgs, #monitor_type, #DEFAULT_CLNB>::new(
                        clock,
                        &config,
                        #tasks_instanciator,
                        monitor_instanciator,
                        copperlist_stream)?,
                });

                #sim_callback_on_new

                runtime
            }

            pub fn get_original_config() -> String {
                #copper_config_content.to_string()
            }

            #run_method
        }
    };

    let builder_name = format_ident!("{}Builder", name);
    let (
        builder_struct,
        builder_new,
        builder_impl,
        builder_sim_callback_method,
        builder_build_sim_callback_arg,
    ) = if sim_mode {
        (
            quote! {
                pub struct #builder_name <'a, F> {
                    clock: Option<_RobotClock>,
                    unified_logger: Option<_Arc<_Mutex<_UnifiedLoggerWrite>>>,
                    config_override: Option<_CuConfig>,
                    sim_callback: Option<&'a mut F>
                }
            },
            quote! {
                pub fn new() -> Self {
                    Self {
                        clock: None,
                        unified_logger: None,
                        config_override: None,
                        sim_callback: None,
                    }
                }
            },
            quote! {
                impl<'a, F> #builder_name <'a, F>
                where
                    F: FnMut(SimStep) -> cu29::simulation::SimOverride,
            },
            Some(quote! {
                fn with_sim_callback(mut self, sim_callback: &'a mut F) -> Self
                {
                    self.sim_callback = Some(sim_callback);
                    self
                }
            }),
            Some(quote! {
                self.sim_callback
                    .ok_or(_CuError::from("Sim callback missing from builder"))?,
            }),
        )
    } else {
        (
            quote! {
                pub struct #builder_name {
                    clock: Option<_RobotClock>,
                    unified_logger: Option<_Arc<_Mutex<_UnifiedLoggerWrite>>>,
                    config_override: Option<_CuConfig>,
                }
            },
            quote! {
                pub fn new() -> Self {
                    Self {
                        clock: None,
                        unified_logger: None,
                        config_override: None,
                    }
                }
            },
            quote! {
                impl #builder_name
            },
            None,
            None,
        )
    };

    let builder = quote! {
        #builder_struct

        #builder_impl
        {
            #builder_new

            pub fn with_clock(mut self, clock: _RobotClock) -> Self {
                self.clock = Some(clock);
                self
            }

            pub fn with_unified_logger(mut self, unified_logger: _Arc<_Mutex<_UnifiedLoggerWrite>>) -> Self {
                self.unified_logger = Some(unified_logger);
                self
            }

            pub fn with_context(mut self, copper_ctx: &_CopperContext) -> Self {
                self.clock = Some(copper_ctx.clock.clone());
                self.unified_logger = Some(copper_ctx.unified_logger.clone());
                self
            }

            pub fn with_config(mut self, config_override: _CuConfig) -> Self {
                    self.config_override = Some(config_override);
                    self
            }

            #builder_sim_callback_method

            pub fn build(self) -> _CuResult<#name> {
                #name::new(
                    self.clock
                        .ok_or(_CuError::from("Clock missing from builder"))?,
                    self.unified_logger
                        .ok_or(_CuError::from("Unified logger missing from builder"))?,
                    self.config_override,
                    #builder_build_sim_callback_arg
                )
            }
        }
    };

    #[cfg(feature = "macro_debug")]
    eprintln!("[build result]");
    // Convert the modified struct back into a TokenStream
    let result = quote! {
        // import everything with an _ to avoid clashes with the user's code
        use cu29::bincode::Encode as _Encode;
        use cu29::bincode::enc::Encoder as _Encoder;
        use cu29::bincode::error::EncodeError as _EncodeError;
        use cu29::bincode::Decode as _Decode;
        use cu29::bincode::de::Decoder as _Decoder;
        use cu29::bincode::error::DecodeError as _DecodeError;
        use cu29::clock::RobotClock as _RobotClock;
        use cu29::clock::OptionCuTime as _OptionCuTime;
        use cu29::clock::ClockProvider as _ClockProvider;
        use cu29::config::CuConfig as _CuConfig;
        use cu29::config::ComponentConfig as _ComponentConfig;
        use cu29::config::MonitorConfig as _MonitorConfig;
        use cu29::config::read_configuration as _read_configuration;
        use cu29::config::read_configuration_str as _read_configuration_str;
        use cu29::curuntime::CuRuntime as _CuRuntime;
        use cu29::curuntime::CopperContext as _CopperContext;
        use cu29::CuResult as _CuResult;
        use cu29::CuError as _CuError;
        use cu29::cutask::CuSrcTask as _CuSrcTask;
        use cu29::cutask::CuSinkTask as _CuSinkTask;
        use cu29::cutask::CuTask as _CuTask;
        use cu29::cutask::CuMsg as _CuMsg;
        use cu29::cutask::CuMsgMetadata as _CuMsgMetadata;
        use cu29::copperlist::CopperList as _CopperList;
        use cu29::monitoring::CuMonitor as _CuMonitor; // Trait import.
        use cu29::monitoring::NoMonitor as _NoMonitor;
        use cu29::monitoring::CuTaskState as _CuTaskState;
        use cu29::monitoring::Decision as _Decision;
        use cu29::prelude::stream_write as _stream_write;
        use cu29::prelude::UnifiedLoggerWrite as _UnifiedLoggerWrite;
        use cu29::prelude::UnifiedLogType as _UnifiedLogType;
        use std::sync::Arc as _Arc;
        use std::sync::Mutex as _Mutex;

        // This is the heart of everything.
        // CuTasks is the list of all the tasks types.
        // CuList is a CopperList with the list of all the messages types as msgs.
        pub type CuTasks = #task_types_tuple;

        // This is the variation with stubs for the sources and sinks in simulation mode.
        pub type CuSimTasks = #task_types_tuple_sim;

        const TASKS_IDS: &'static [&'static str] = &[#( #all_tasks_ids ),*];

        #culist_support

        #sim_support

        fn tasks_instanciator(all_instances_configs: Vec<Option<&_ComponentConfig>>) -> _CuResult<CuTasks> {
            Ok(( #(#task_instances_init_code),*, ))
        }

        fn tasks_instanciator_sim(all_instances_configs: Vec<Option<&_ComponentConfig>>) -> _CuResult<CuSimTasks> {
            Ok(( #(#task_sim_instances_init_code),*, ))
        }

        fn monitor_instanciator(config: &_CuConfig) -> #monitor_type {
            #monitor_type::new(config, TASKS_IDS).expect("Failed to create the given monitor.")
        }

        pub #item_struct

        #runtime_impl

        #builder
    };
    let tokens: TokenStream = result.into();

    // Print and format the generated code using rustfmt
    #[cfg(feature = "macro_debug")]
    {
        let formatted_code = rustfmt_generated_code(tokens.to_string());
        eprintln!("\n     ===    Gen. Runtime ===\n");
        eprintln!("{}", highlight_rust_code(formatted_code));
        eprintln!("\n     === === === === === ===\n");
    }

    tokens
}

fn read_config(config_file: &str) -> CuConfig {
    let filename = config_full_path(config_file);

    read_configuration(filename.as_str())
        .unwrap_or_else(|_| panic!("Failed to read configuration file: {filename}"))
}

fn config_full_path(config_file: &str) -> String {
    let mut config_full_path = utils::caller_crate_root();
    config_full_path.push(config_file);
    let filename = config_full_path
        .as_os_str()
        .to_str()
        .expect("Could not interpret the config file name");
    filename.to_string()
}

/// Extract all the tasks types in their index order and their ids.
fn extract_tasks_types(
    copper_config: &CuConfig,
) -> (Vec<String>, Vec<CuTaskType>, Vec<String>, Vec<Type>) {
    let all_id_nodes = copper_config.get_all_nodes();

    // Get all the tasks Ids
    let all_tasks_ids: Vec<String> = all_id_nodes
        .iter()
        .map(|(_, node)| node.get_id().to_string())
        .collect();

    let all_task_cutype: Vec<CuTaskType> = all_id_nodes
        .iter()
        .map(|(id, _)| find_task_type_for_id(&copper_config.graph, *id))
        .collect();

    // Collect all the type names used by our configs.
    let all_types_names: Vec<String> = all_id_nodes
        .iter()
        .map(|(_, node)| node.get_type().to_string())
        .collect();

    // Transform them as Rust types
    let all_types: Vec<Type> = all_types_names
        .iter()
        .map(|name| {
            parse_str(name)
                .unwrap_or_else(|_| panic!("Could not transform {name} into a Task Rust type."))
        })
        .collect();
    (all_tasks_ids, all_task_cutype, all_types_names, all_types)
}

fn extract_msg_types(runtime_plan: &CuExecutionLoop) -> Vec<Type> {
    runtime_plan
        .steps
        .iter()
        .filter_map(|unit| match unit {
            CuExecutionUnit::Step(step) => {
                if let Some((_, output_msg_type)) = &step.output_msg_index_type {
                    Some(
                        parse_str::<Type>(output_msg_type.as_str()).unwrap_or_else(|_| {
                            panic!(
                                "Could not transform {output_msg_type} into a message Rust type."
                            )
                        }),
                    )
                } else {
                    None
                }
            }
            CuExecutionUnit::Loop(_) => todo!("Needs to be implemented"),
        })
        .collect()
}

/// Builds the tuple of the CuList as a tuple off all the messages types.
fn build_culist_tuple(all_msgs_types_in_culist_order: &[Type]) -> TypeTuple {
    if all_msgs_types_in_culist_order.is_empty() {
        parse_quote! {()}
    } else {
        parse_quote! { (#(_CuMsg<#all_msgs_types_in_culist_order>),*,)}
    }
}

/// This is the bincode encoding part of the CuMsgs
fn build_culist_tuple_encode(all_msgs_types_in_culist_order: &[Type]) -> ItemImpl {
    let indices: Vec<usize> = (0..all_msgs_types_in_culist_order.len()).collect();

    // Generate the `self.#i.encode(encoder)?` for each tuple index, including `()` types
    let encode_fields: Vec<_> = indices
        .iter()
        .map(|i| {
            let idx = syn::Index::from(*i);
            quote! { self.0.#idx.encode(encoder)?; }
        })
        .collect();

    parse_quote! {
        impl _Encode for CuMsgs {
            fn encode<E: _Encoder>(&self, encoder: &mut E) -> Result<(), _EncodeError> {
                #(#encode_fields)*
                Ok(())
            }
        }
    }
}

/// This is the bincode decoding part of the CuMsgs
fn build_culist_tuple_decode(all_msgs_types_in_culist_order: &[Type]) -> ItemImpl {
    let indices: Vec<usize> = (0..all_msgs_types_in_culist_order.len()).collect();

    // Generate the `_CuMsg::<T>::decode(decoder)?` for each tuple index
    let decode_fields: Vec<_> = indices
        .iter()
        .map(|i| {
            let t = &all_msgs_types_in_culist_order[*i];
            quote! { _CuMsg::<#t>::decode(decoder)? }
        })
        .collect();

    parse_quote! {
        impl _Decode for CuMsgs {
            fn decode<D: _Decoder>(decoder: &mut D) -> Result<Self, _DecodeError> {
                Ok(CuMsgs ((
                    #(#decode_fields),*
                )))
            }
        }
    }
}

fn build_culist_tuple_debug(all_msgs_types_in_culist_order: &[Type]) -> ItemImpl {
    let indices: Vec<usize> = (0..all_msgs_types_in_culist_order.len()).collect();

    let debug_fields: Vec<_> = indices
        .iter()
        .map(|i| {
            let idx = syn::Index::from(*i);
            quote! { .field(&self.0.#idx) }
        })
        .collect();

    parse_quote! {
        impl std::fmt::Debug for CuMsgs {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_tuple("CuMsgs")
                    #(#debug_fields)*
                    .finish()
            }
        }
    }
}