ros2msg 0.5.3

A Rust parser for ROS2 message, service, action, and IDL files with 100% ROS2 Jazzy compatibility
Documentation
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
use ros2msg::generator::{FieldInfo, Generator, ParseCallbacks};
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Callback that generates #[ros2(...)] attributes for testing
struct Ros2AttributeCallbacks;

impl ParseCallbacks for Ros2AttributeCallbacks {
    fn add_field_attributes(&self, field_info: &FieldInfo) -> Vec<String> {
        let mut attrs = Vec::new();
        let mut ros2_parts = Vec::new();

        // Add type override if present
        if let Some(type_override) = field_info.ros2_type_override() {
            ros2_parts.push(format!("ros2_type = \"{}\"", type_override));
        }

        // Add capacity if present
        if let Some(capacity) = field_info.capacity() {
            ros2_parts.push(format!("capacity = {}", capacity));
        }

        // Add default value if present
        if let Some(default_value) = field_info.default_value() {
            ros2_parts.push(format!("default = \"{}\"", default_value));
        }

        if !ros2_parts.is_empty() {
            attrs.push(format!("#[ros2({})]", ros2_parts.join(", ")));
        }

        attrs
    }
}

/// Helper to create a temporary test message file
fn create_test_msg_file(dir: &TempDir, package: &str, name: &str, content: &str) -> PathBuf {
    let msg_dir = dir.path().join(package).join("msg");
    fs::create_dir_all(&msg_dir).unwrap();
    let file_path = msg_dir.join(format!("{}.msg", name));
    fs::write(&file_path, content).unwrap();
    file_path
}

/// Helper to create a temporary test service file
fn create_test_srv_file(dir: &TempDir, package: &str, name: &str, content: &str) -> PathBuf {
    let srv_dir = dir.path().join(package).join("srv");
    fs::create_dir_all(&srv_dir).unwrap();
    let file_path = srv_dir.join(format!("{}.srv", name));
    fs::write(&file_path, content).unwrap();
    file_path
}

#[test]
fn test_generator_basic_message() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "SimpleMessage",
        "int32 value\nstring name\n",
    );

    let result = Generator::new()
        .header("// Test generated code")
        .derive_debug(true)
        .derive_clone(true)
        .derive_partialeq(true)
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    // Check generated file exists
    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("simple_message.rs");
    assert!(generated_file.exists(), "Generated file not found");

    // Check file content
    let content = fs::read_to_string(&generated_file).unwrap();
    assert!(content.contains("// Test generated code"));
    assert!(content.contains("pub struct SimpleMessage"));
    assert!(content.contains("pub value: i32"));
    assert!(content.contains("pub name:") && content.contains("String"));
    assert!(
        content.contains("Debug") && content.contains("Clone") && content.contains("PartialEq")
    );
}

#[test]
fn test_generator_service_file() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let srv_file = create_test_srv_file(
        &temp_dir,
        "test_msgs",
        "AddTwoInts",
        "int64 a\nint64 b\n---\nint64 sum\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .derive_clone(true)
        .include(srv_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    // Check generated service file
    let generated_file = output_dir
        .join("test_msgs")
        .join("srv")
        .join("add_two_ints.rs");
    assert!(generated_file.exists());

    let content = fs::read_to_string(&generated_file).unwrap();
    assert!(content.contains("AddTwoInts_Request") || content.contains("AddTwoIntsRequest"));
    assert!(content.contains("AddTwoInts_Response") || content.contains("AddTwoIntsResponse"));
    assert!(content.contains("pub a: i64"));
    assert!(content.contains("pub b: i64"));
    assert!(content.contains("pub sum: i64"));
}

#[test]
fn test_generator_with_all_derives() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file =
        create_test_msg_file(&temp_dir, "test_msgs", "FullDerives", "int32 x\nint32 y\n");

    let result = Generator::new()
        .derive_debug(true)
        .derive_clone(true)
        .derive_copy(true)
        .derive_default(true)
        .derive_partialeq(true)
        .derive_eq(true)
        .derive_partialord(true)
        .derive_ord(true)
        .derive_hash(true)
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("full_derives.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    assert!(content.contains("Debug"));
    assert!(content.contains("Clone"));
    assert!(content.contains("Copy"));
    assert!(content.contains("Default"));
    assert!(content.contains("PartialEq"));
    assert!(content.contains("Eq"));
    assert!(content.contains("PartialOrd"));
    assert!(content.contains("Ord"));
    assert!(content.contains("Hash"));
}

#[test]
fn test_generator_with_raw_lines() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(&temp_dir, "test_msgs", "WithRawLines", "int32 value\n");

    let result = Generator::new()
        .derive_debug(true)
        .raw_line("#![allow(clippy::all)]")
        .raw_line("use std::fmt;")
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("with_raw_lines.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    assert!(content.contains("#![allow(clippy::all)]"));
    assert!(content.contains("use std::fmt;"));
}

#[test]
fn test_generator_module_structure() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    // Create multiple messages
    let msg1 = create_test_msg_file(&temp_dir, "test_msgs", "Message1", "int32 a\n");
    let msg2 = create_test_msg_file(&temp_dir, "test_msgs", "Message2", "int32 b\n");
    let srv1 = create_test_srv_file(
        &temp_dir,
        "test_msgs",
        "Service1",
        "int32 req\n---\nint32 resp\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .include(msg1.to_str().unwrap())
        .include(msg2.to_str().unwrap())
        .include(srv1.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    // Check module structure
    let package_mod = output_dir.join("test_msgs").join("mod.rs");
    assert!(package_mod.exists());
    let package_mod_content = fs::read_to_string(&package_mod).unwrap();
    assert!(package_mod_content.contains("pub mod msg;"));
    assert!(package_mod_content.contains("pub mod srv;"));

    let msg_mod = output_dir.join("test_msgs").join("msg").join("mod.rs");
    assert!(msg_mod.exists());
    let msg_mod_content = fs::read_to_string(&msg_mod).unwrap();
    assert!(msg_mod_content.contains("pub mod message1;"));
    assert!(msg_mod_content.contains("pub mod message2;"));

    let srv_mod = output_dir.join("test_msgs").join("srv").join("mod.rs");
    assert!(srv_mod.exists());
    let srv_mod_content = fs::read_to_string(&srv_mod).unwrap();
    assert!(srv_mod_content.contains("pub mod service1;"));
}

#[test]
fn test_generator_with_constants() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "WithConstants",
        "int32 MAX_VALUE = 100\nstring DEFAULT_NAME = \"test\"\nint32 value\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("with_constants.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    assert!(content.contains("pub const MAX_VALUE: i32 = 100"));
    assert!(content.contains("pub const DEFAULT_NAME: &str = \"test\""));
}

#[test]
fn test_generator_emit_rerun_if_changed() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(&temp_dir, "test_msgs", "TestMsg", "int32 value\n");

    // emit_rerun_if_changed is only relevant in build.rs context
    // but we can verify it doesn't break the API
    let result = Generator::new()
        .derive_debug(true)
        .emit_rerun_if_changed(true)
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());
}

#[test]
fn test_generator_multiple_packages() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg1 = create_test_msg_file(&temp_dir, "package_a", "MsgA", "int32 a\n");
    let msg2 = create_test_msg_file(&temp_dir, "package_b", "MsgB", "int32 b\n");

    let result = Generator::new()
        .derive_debug(true)
        .include(msg1.to_str().unwrap())
        .include(msg2.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    // Check both packages were generated
    assert!(
        output_dir
            .join("package_a")
            .join("msg")
            .join("msg_a.rs")
            .exists()
    );
    assert!(
        output_dir
            .join("package_b")
            .join("msg")
            .join("msg_b.rs")
            .exists()
    );

    // Check root mod.rs includes both packages
    let root_mod = output_dir.join("mod.rs");
    let root_content = fs::read_to_string(&root_mod).unwrap();
    assert!(root_content.contains("pub mod package_a;"));
    assert!(root_content.contains("pub mod package_b;"));
}

#[test]
fn test_generator_error_no_output_dir() {
    let temp_dir = TempDir::new().unwrap();
    let msg_file = create_test_msg_file(&temp_dir, "test_msgs", "Test", "int32 value\n");

    let result = Generator::new()
        .include(msg_file.to_str().unwrap())
        // Missing output_dir
        .generate();

    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("Output directory is required")
    );
}

#[test]
fn test_generator_error_no_input_files() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let result = Generator::new()
        .output_dir(output_dir.to_str().unwrap())
        // Missing include files
        .generate();

    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("No input files provided")
    );
}

#[test]
fn test_generator_with_arrays() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "WithArrays",
        "int32[5] fixed_array\nint32[] dynamic_array\nint32[<=10] bounded_array\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("with_arrays.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    assert!(content.contains("[i32; 5]")); // fixed array
    assert!(content.contains("Vec<i32>")); // dynamic and bounded arrays
}

#[test]
fn test_generator_ctypes_prefix() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    // Create an IDL file with actual IDL 'char' type (not .msg 'char' which is uint8)
    // IDL 'char' is a signed 8-bit character type that maps to c_char
    let idl_dir = temp_dir.path().join("test_msgs").join("msg");
    fs::create_dir_all(&idl_dir).unwrap();
    let idl_file = idl_dir.join("WithChar.idl");
    fs::write(
        &idl_file,
        r#"module test_msgs {
  module msg {
    struct WithChar {
      char c;
    };
  };
};
"#,
    )
    .unwrap();

    let result = Generator::new()
        .derive_debug(true)
        .ctypes_prefix("libc")
        .include(idl_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("with_char.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    assert!(
        content.contains("libc::c_char"),
        "Expected libc::c_char but got:\n{}",
        content
    );
}

#[test]
fn test_generator_bounded_string() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "BoundedString",
        "string<=255 name\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .parse_callbacks(Box::new(Ros2AttributeCallbacks))
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("bounded_string.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    // Should have ros2 capacity attribute for bounded string
    assert!(
        content.contains("#[ros2(capacity = 255)]"),
        "Missing ros2 capacity attribute. Generated content:\n{}",
        content
    );
    assert!(content.contains("pub name: ::std::string::String"));
}

#[test]
fn test_generator_bounded_wstring() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "BoundedWString",
        "wstring<=128 text\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .parse_callbacks(Box::new(Ros2AttributeCallbacks))
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("bounded_w_string.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    // Should have combined ros2 attribute with type and capacity
    assert!(
        content.contains("#[ros2(ros2_type = \"wstring\", capacity = 128)]"),
        "Missing combined ros2 attribute. Generated content:\n{}",
        content
    );
    assert!(content.contains("pub text: ::std::string::String"));
}

#[test]
fn test_generator_bounded_sequence() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "BoundedSequence",
        "int32[<=100] values\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .parse_callbacks(Box::new(Ros2AttributeCallbacks))
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("bounded_sequence.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    // Should have ros2 capacity attribute for bounded sequence
    assert!(
        content.contains("#[ros2(capacity = 100)]"),
        "Missing ros2 capacity attribute. Generated content:\n{}",
        content
    );
    assert!(content.contains("pub values: Vec<i32>"));
}

#[test]
fn test_generator_bounded_string_sequence() {
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let msg_file = create_test_msg_file(
        &temp_dir,
        "test_msgs",
        "BoundedStringSequence",
        "string<=50[<=10] names\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .parse_callbacks(Box::new(Ros2AttributeCallbacks))
        .include(msg_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("test_msgs")
        .join("msg")
        .join("bounded_string_sequence.rs");
    let content = fs::read_to_string(&generated_file).unwrap();

    // Should have ros2 capacity attribute for the sequence (not the inner string capacity)
    // The sequence capacity is what matters for the field type
    assert!(
        content.contains("#[ros2(capacity = 10)]"),
        "Missing ros2 capacity attribute for sequence. Generated content:\n{}",
        content
    );
    assert!(content.contains("pub names: Vec<::std::string::String>"));
}

#[test]
fn test_generator_list_nodes_service() {
    // Test the ListNodes.srv pattern: empty request, response with arrays
    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let srv_file = create_test_srv_file(
        &temp_dir,
        "composition_interfaces",
        "ListNodes",
        "---\nstring[] full_node_names\nuint64[] unique_ids\n",
    );

    let result = Generator::new()
        .derive_debug(true)
        .include(srv_file.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("list_nodes.rs");
    assert!(generated_file.exists(), "Generated file not found");

    let content = fs::read_to_string(&generated_file).unwrap();
    println!("=== Generated Rust Code ===\n{}\n=== End ===", content);

    // Must have both request and response structs
    assert!(
        content.contains("pub struct ListNodes_Request"),
        "Missing ListNodes_Request struct. Content:\n{}",
        content
    );
    assert!(
        content.contains("pub struct ListNodes_Response"),
        "Missing ListNodes_Response struct. Content:\n{}",
        content
    );

    // Request should have dummy member (empty struct)
    assert!(
        content.contains("structure_needs_at_least_one_member"),
        "Empty request should have dummy member. Content:\n{}",
        content
    );

    // Response should have the actual fields
    assert!(
        content.contains("full_node_names"),
        "Missing full_node_names field. Content:\n{}",
        content
    );
    assert!(
        content.contains("unique_ids"),
        "Missing unique_ids field. Content:\n{}",
        content
    );
}

#[test]
fn test_generator_real_list_nodes_service() {
    // Test with actual ROS2 ListNodes.srv from filesystem
    use std::path::Path;
    let srv_path = Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/ListNodes.srv");
    if !srv_path.exists() {
        eprintln!("Skipping test - ListNodes.srv not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let result = Generator::new()
        .derive_debug(true)
        .include(srv_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("list_nodes.rs");
    assert!(
        generated_file.exists(),
        "Generated file not found at {:?}",
        generated_file
    );

    let content = fs::read_to_string(&generated_file).unwrap();
    println!("=== Generated from real file ===\n{}\n=== End ===", content);

    // Must have both request and response structs
    assert!(
        content.contains("pub struct ListNodes_Request"),
        "Missing ListNodes_Request struct"
    );
    assert!(
        content.contains("pub struct ListNodes_Response"),
        "Missing ListNodes_Response struct"
    );
}

#[test]
fn test_generator_multiple_files_same_package() {
    // Test with multiple .srv files from same package to check for file overwriting
    use std::path::Path;
    let list_nodes_path =
        Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/ListNodes.srv");
    let load_node_path = Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/LoadNode.srv");
    let unload_node_path =
        Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/UnloadNode.srv");

    if !list_nodes_path.exists() {
        eprintln!("Skipping test - files not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let ament_paths = vec![PathBuf::from("/opt/ros/jazzy/share")];

    // Include all three services like oxidros-build does
    let result = Generator::new()
        .header("// Auto-generated")
        .derive_debug(true)
        .include(list_nodes_path.to_str().unwrap())
        .include(load_node_path.to_str().unwrap())
        .include(unload_node_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .allowlist_recursively(true)
        .package_search_paths(ament_paths)
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    // Check ListNodes
    let list_nodes_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("list_nodes.rs");
    assert!(list_nodes_file.exists(), "ListNodes file not found");

    let list_nodes_content = fs::read_to_string(&list_nodes_file).unwrap();
    println!("=== ListNodes ===\n{}\n", list_nodes_content);
    assert!(
        list_nodes_content.contains("pub struct ListNodes_Request"),
        "Missing ListNodes_Request"
    );
    assert!(
        list_nodes_content.contains("pub struct ListNodes_Response"),
        "Missing ListNodes_Response"
    );

    // Check LoadNode
    let load_node_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("load_node.rs");
    assert!(load_node_file.exists(), "LoadNode file not found");

    let load_node_content = fs::read_to_string(&load_node_file).unwrap();
    println!("=== LoadNode ===\n{}\n", load_node_content);
    assert!(
        load_node_content.contains("pub struct LoadNode_Request"),
        "Missing LoadNode_Request"
    );
    assert!(
        load_node_content.contains("pub struct LoadNode_Response"),
        "Missing LoadNode_Response"
    );
}

/// Callback that generates ros2 attributes similar to oxidros-build's RosCallbacks
struct FullRos2Callbacks;

impl ParseCallbacks for FullRos2Callbacks {
    fn add_attributes(&self, info: &ros2msg::generator::ItemInfo) -> Vec<String> {
        let package = info.package();
        let interface_type = match info.interface_kind() {
            ros2msg::generator::InterfaceKind::Message => "msg",
            ros2msg::generator::InterfaceKind::Service => "srv",
            ros2msg::generator::InterfaceKind::Action => "action",
        };
        vec![
            format!("#[ros2(package = \"{}\", interface_type = \"{}\")]", package, interface_type),
            "#[cfg_attr(not(feature = \"rcl\"), derive(ros2_types::serde::Serialize, ros2_types::serde::Deserialize))]".to_string(),
            "#[cfg_attr(not(feature = \"rcl\"), serde(crate = \"ros2_types::serde\"))]".to_string(),
        ]
    }

    fn add_derives(&self, _info: &ros2msg::generator::ItemInfo) -> Vec<String> {
        vec![
            "ros2_types::Ros2Msg".to_string(),
            "ros2_types::TypeDescription".to_string(),
        ]
    }
}

#[test]
fn test_generator_with_full_ros2_callbacks() {
    // Test with callbacks similar to oxidros-build's RosCallbacks
    use std::path::Path;
    let srv_path = Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/ListNodes.srv");
    if !srv_path.exists() {
        eprintln!("Skipping test - ListNodes.srv not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");
    let ament_paths = vec![PathBuf::from("/opt/ros/jazzy/share")];

    let result = Generator::new()
        .header("// Auto-generated")
        .derive_debug(true)
        .parse_callbacks(Box::new(FullRos2Callbacks))
        .include(srv_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .emit_rerun_if_changed(true)
        .allowlist_recursively(true)
        .package_search_paths(ament_paths)
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("list_nodes.rs");
    assert!(generated_file.exists(), "Generated file not found");

    let content = fs::read_to_string(&generated_file).unwrap();
    println!("=== With FullRos2Callbacks ===\n{}\n=== End ===", content);

    // Must have both request and response structs
    assert!(
        content.contains("pub struct ListNodes_Request"),
        "Missing ListNodes_Request. Content:\n{}",
        content
    );
    assert!(
        content.contains("pub struct ListNodes_Response"),
        "Missing ListNodes_Response. Content:\n{}",
        content
    );
}

#[test]
fn test_generator_all_composition_services_with_callbacks() {
    // Test with ALL composition_interfaces services to find file overwriting issues
    use std::path::Path;
    let list_nodes_path =
        Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/ListNodes.srv");
    let load_node_path = Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/LoadNode.srv");
    let unload_node_path =
        Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/UnloadNode.srv");

    if !list_nodes_path.exists() {
        eprintln!("Skipping test - files not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");
    let ament_paths = vec![PathBuf::from("/opt/ros/jazzy/share")];

    let result = Generator::new()
        .header("// Auto-generated")
        .derive_debug(true)
        .parse_callbacks(Box::new(FullRos2Callbacks))
        .include(list_nodes_path.to_str().unwrap())
        .include(load_node_path.to_str().unwrap())
        .include(unload_node_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .emit_rerun_if_changed(true)
        .allowlist_recursively(true)
        .package_search_paths(ament_paths)
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    // Check each service file
    for (name, req_name, resp_name) in [
        ("list_nodes.rs", "ListNodes_Request", "ListNodes_Response"),
        ("load_node.rs", "LoadNode_Request", "LoadNode_Response"),
        (
            "unload_node.rs",
            "UnloadNode_Request",
            "UnloadNode_Response",
        ),
    ] {
        let file_path = output_dir
            .join("composition_interfaces")
            .join("srv")
            .join(name);
        assert!(file_path.exists(), "File not found: {}", name);

        let content = fs::read_to_string(&file_path).unwrap();
        println!("=== {} ===\n{}\n", name, content);

        assert!(
            content.contains(&format!("pub struct {}", req_name)),
            "Missing {} in {}. Content:\n{}",
            req_name,
            name,
            content
        );
        assert!(
            content.contains(&format!("pub struct {}", resp_name)),
            "Missing {} in {}. Content:\n{}",
            resp_name,
            name,
            content
        );
    }
}

#[test]
fn test_generator_with_allowlist_recursively() {
    // Test with allowlist_recursively enabled, simulating how oxidros-build works
    use std::path::Path;
    let srv_path = Path::new("/opt/ros/jazzy/share/composition_interfaces/srv/ListNodes.srv");
    if !srv_path.exists() {
        eprintln!("Skipping test - ListNodes.srv not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let ament_paths = vec![PathBuf::from("/opt/ros/jazzy/share")];

    let result = Generator::new()
        .header("// Auto-generated")
        .derive_debug(true)
        .include(srv_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .allowlist_recursively(true)
        .package_search_paths(ament_paths)
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("composition_interfaces")
        .join("srv")
        .join("list_nodes.rs");
    assert!(
        generated_file.exists(),
        "Generated file not found at {:?}",
        generated_file
    );

    let content = fs::read_to_string(&generated_file).unwrap();
    println!(
        "=== With allowlist_recursively ===\n{}\n=== End ===",
        content
    );

    // Must have both request and response structs
    assert!(
        content.contains("pub struct ListNodes_Request"),
        "Missing ListNodes_Request struct"
    );
    assert!(
        content.contains("pub struct ListNodes_Response"),
        "Missing ListNodes_Response struct"
    );
}

#[test]
fn test_generator_idl_parameter_value() {
    // Test generating from ParameterValue.idl file
    use std::path::Path;
    let idl_path = Path::new("/opt/ros/jazzy/share/rcl_interfaces/msg/ParameterValue.idl");
    if !idl_path.exists() {
        eprintln!("Skipping test - ParameterValue.idl not found");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    let output_dir = temp_dir.path().join("generated");

    let result = Generator::new()
        .derive_debug(true)
        .include(idl_path.to_str().unwrap())
        .output_dir(output_dir.to_str().unwrap())
        .generate();

    assert!(result.is_ok(), "Failed to generate: {:?}", result.err());

    let generated_file = output_dir
        .join("rcl_interfaces")
        .join("msg")
        .join("parameter_value.rs");
    assert!(
        generated_file.exists(),
        "Generated file not found at {:?}",
        generated_file
    );

    let content = fs::read_to_string(&generated_file).unwrap();
    println!(
        "=== Generated from ParameterValue.idl ===\n{}\n=== End ===",
        content
    );

    // Must have the ParameterValue struct
    assert!(
        content.contains("pub struct ParameterValue"),
        "Missing ParameterValue struct. Content:\n{}",
        content
    );
}