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
//! Integration tests comparing IDL conversion against ROS2 Jazzy official IDL files
//!
//! These tests parse ROS2 .msg/.srv/.action files from the Jazzy installation,
//! convert them to IDL using our converter, then parse both our generated IDL
//! and the official IDL to compare the parsed structures.

use ros2msg::idl::parse_idl_string;
use ros2msg::idl_adapter::{action_to_idl, message_to_idl, service_to_idl};
use ros2msg::{parse_action_file, parse_message_file, parse_service_file};
use std::fs;
use std::path::{Path, PathBuf};

const ROS2_JAZZY_SHARE: &str = "/opt/ros/jazzy/share";

/// Compare two parsed IDL structures
fn compare_parsed_idl(
    generated_idl_text: &str,
    official_idl_text: &str,
    context: &str,
) -> Result<bool, Box<dyn std::error::Error>> {
    // Parse both IDLs
    let generated_idl = parse_idl_string(generated_idl_text)?;
    let official_idl = parse_idl_string(official_idl_text)?;

    // Compare the parsed structures
    if generated_idl != official_idl {
        eprintln!("\n=== IDL Structure Mismatch for {} ===", context);
        eprintln!("\n--- Generated IDL Text ---");
        eprintln!("{}", generated_idl_text);
        eprintln!("\n--- Official IDL Text ---");
        eprintln!("{}", official_idl_text);
        eprintln!("\n--- Generated Parsed Structure ---");
        eprintln!("{:#?}", generated_idl);
        eprintln!("\n--- Official Parsed Structure ---");
        eprintln!("{:#?}", official_idl);
        eprintln!("\n=== End Mismatch ===\n");
        Ok(false)
    } else {
        Ok(true)
    }
}

/// Find all files with a given extension in a package directory
fn find_files(package_dir: &Path, extension: &str) -> Vec<PathBuf> {
    let mut files = Vec::new();

    if let Ok(entries) = fs::read_dir(package_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some(extension) {
                files.push(path);
            }
        }
    }

    files
}

/// Test message conversion for a specific package
fn test_package_messages(package_name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let package_path = Path::new(ROS2_JAZZY_SHARE).join(package_name);
    if !package_path.exists() {
        eprintln!("Skipping {} - package not found", package_name);
        return Ok(());
    }

    let msg_dir = package_path.join("msg");
    if !msg_dir.exists() {
        return Ok(()); // No messages in this package
    }

    let msg_files = find_files(&msg_dir, "msg");
    let mut tested = 0;
    let mut passed = 0;

    for msg_file in msg_files {
        let msg_name = msg_file.file_stem().unwrap().to_str().unwrap();
        let idl_file = msg_file.with_extension("idl");

        if !idl_file.exists() {
            eprintln!(
                "Warning: IDL file not found for {}/{}",
                package_name, msg_name
            );
            continue;
        }

        // Parse the message file
        let msg_spec = match parse_message_file(package_name, &msg_file) {
            Ok(spec) => spec,
            Err(e) => {
                eprintln!("Failed to parse {}/{}: {}", package_name, msg_name, e);
                continue;
            }
        };

        // Convert to IDL
        let input_file = format!("msg/{}.msg", msg_name);
        let generated_idl = message_to_idl(&msg_spec, package_name, &input_file);

        // Read official IDL
        let official_idl = fs::read_to_string(&idl_file)?;

        // Compare
        tested += 1;
        match compare_parsed_idl(
            &generated_idl,
            &official_idl,
            &format!("{}/{}", package_name, msg_name),
        ) {
            Ok(true) => passed += 1,
            Ok(false) => {
                eprintln!("FAILED: {}/{}", package_name, msg_name);
            }
            Err(e) => {
                eprintln!("ERROR parsing IDL for {}/{}: {}", package_name, msg_name, e);
            }
        }
    }

    if tested > 0 {
        println!("{}: {}/{} messages passed", package_name, passed, tested);
    }

    assert_eq!(
        passed, tested,
        "{}: Some message conversions don't match official IDL",
        package_name
    );
    Ok(())
}

/// Test service conversion for a specific package
fn test_package_services(package_name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let package_path = Path::new(ROS2_JAZZY_SHARE).join(package_name);
    if !package_path.exists() {
        eprintln!("Skipping {} - package not found", package_name);
        return Ok(());
    }

    let srv_dir = package_path.join("srv");
    if !srv_dir.exists() {
        return Ok(()); // No services in this package
    }

    let srv_files = find_files(&srv_dir, "srv");
    let mut tested = 0;
    let mut passed = 0;

    for srv_file in srv_files {
        let srv_name = srv_file.file_stem().unwrap().to_str().unwrap();
        let idl_file = srv_file.with_extension("idl");

        if !idl_file.exists() {
            eprintln!(
                "Warning: IDL file not found for {}/{}",
                package_name, srv_name
            );
            continue;
        }

        // Parse the service file
        let srv_spec = match parse_service_file(package_name, &srv_file) {
            Ok(spec) => spec,
            Err(e) => {
                eprintln!("Failed to parse {}/{}: {}", package_name, srv_name, e);
                continue;
            }
        };

        // Convert to IDL
        let input_file = format!("srv/{}.srv", srv_name);
        let generated_idl = service_to_idl(&srv_spec, package_name, &input_file);

        // Read official IDL
        let official_idl = fs::read_to_string(&idl_file)?;

        // Compare
        tested += 1;
        match compare_parsed_idl(
            &generated_idl,
            &official_idl,
            &format!("{}/{}", package_name, srv_name),
        ) {
            Ok(true) => passed += 1,
            Ok(false) => {
                eprintln!("FAILED: {}/{}", package_name, srv_name);
            }
            Err(e) => {
                eprintln!("ERROR parsing IDL for {}/{}: {}", package_name, srv_name, e);
            }
        }
    }

    if tested > 0 {
        println!("{}: {}/{} services passed", package_name, passed, tested);
    }

    assert_eq!(
        passed, tested,
        "{}: Some service conversions don't match official IDL",
        package_name
    );
    Ok(())
}

/// Test action conversion for a specific package
fn test_package_actions(package_name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let package_path = Path::new(ROS2_JAZZY_SHARE).join(package_name);
    if !package_path.exists() {
        eprintln!("Skipping {} - package not found", package_name);
        return Ok(());
    }

    let action_dir = package_path.join("action");
    if !action_dir.exists() {
        return Ok(()); // No actions in this package
    }

    let action_files = find_files(&action_dir, "action");
    let mut tested = 0;
    let mut passed = 0;

    for action_file in action_files {
        let action_name = action_file.file_stem().unwrap().to_str().unwrap();
        let idl_file = action_file.with_extension("idl");

        if !idl_file.exists() {
            eprintln!(
                "Warning: IDL file not found for {}/{}",
                package_name, action_name
            );
            continue;
        }

        // Parse the action file
        let action_spec = match parse_action_file(package_name, &action_file) {
            Ok(spec) => spec,
            Err(e) => {
                eprintln!("Failed to parse {}/{}: {}", package_name, action_name, e);
                continue;
            }
        };

        // Convert to IDL
        let input_file = format!("action/{}.action", action_name);
        let generated_idl = action_to_idl(&action_spec, package_name, &input_file);

        // Read official IDL
        let official_idl = fs::read_to_string(&idl_file)?;

        // Compare
        tested += 1;
        match compare_parsed_idl(
            &generated_idl,
            &official_idl,
            &format!("{}/{}", package_name, action_name),
        ) {
            Ok(true) => passed += 1,
            Ok(false) => {
                eprintln!("FAILED: {}/{}", package_name, action_name);
            }
            Err(e) => {
                eprintln!(
                    "ERROR parsing IDL for {}/{}: {}",
                    package_name, action_name, e
                );
            }
        }
    }

    if tested > 0 {
        println!("{}: {}/{} actions passed", package_name, passed, tested);
    }

    assert_eq!(
        passed, tested,
        "{}: Some action conversions don't match official IDL",
        package_name
    );
    Ok(())
}

// Tests for common ROS2 packages

#[test]
#[ignore] // Run with: cargo test --test jazzy_integration_tests -- --ignored
fn test_std_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("std_msgs")
}

#[test]
#[ignore]
fn test_std_srvs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_services("std_srvs")
}

#[test]
#[ignore]
fn test_geometry_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("geometry_msgs")
}

#[test]
#[ignore]
fn test_sensor_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("sensor_msgs")
}

#[test]
#[ignore]
fn test_nav_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("nav_msgs")
}

#[test]
#[ignore]
fn test_trajectory_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("trajectory_msgs")
}

#[test]
#[ignore]
fn test_action_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("action_msgs")
}

#[test]
#[ignore]
fn test_example_interfaces_msgs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("example_interfaces")
}

#[test]
#[ignore]
fn test_example_interfaces_srvs() -> Result<(), Box<dyn std::error::Error>> {
    test_package_services("example_interfaces")
}

#[test]
#[ignore]
fn test_example_interfaces_actions() -> Result<(), Box<dyn std::error::Error>> {
    test_package_actions("example_interfaces")
}

#[test]
#[ignore]
fn test_builtin_interfaces() -> Result<(), Box<dyn std::error::Error>> {
    test_package_messages("builtin_interfaces")
}

/// Comprehensive test that tests all packages at once
#[test]
#[ignore] // Run with: cargo test test_all_jazzy_packages -- --ignored --nocapture
fn test_all_jazzy_packages() -> Result<(), Box<dyn std::error::Error>> {
    let packages = vec![
        "std_msgs",
        "std_srvs",
        "geometry_msgs",
        "sensor_msgs",
        "nav_msgs",
        "trajectory_msgs",
        "action_msgs",
        "example_interfaces",
        "builtin_interfaces",
        "diagnostic_msgs",
        "shape_msgs",
        "stereo_msgs",
        "visualization_msgs",
    ];

    println!("\n=== Testing IDL Conversion Against ROS2 Jazzy ===\n");

    for package in packages {
        // Test messages
        test_package_messages(package)?;

        // Test services
        test_package_services(package)?;

        // Test actions
        test_package_actions(package)?;
    }

    println!("\n=== All tests passed! ===");

    Ok(())
}

/// Test a single specific message for debugging
#[test]
#[ignore]
fn test_single_message_empty() -> Result<(), Box<dyn std::error::Error>> {
    let msg_file = Path::new(ROS2_JAZZY_SHARE).join("std_msgs/msg/Empty.msg");
    let idl_file = Path::new(ROS2_JAZZY_SHARE).join("std_msgs/msg/Empty.idl");

    let msg_spec = parse_message_file("std_msgs", &msg_file)?;
    let generated_idl = message_to_idl(&msg_spec, "std_msgs", "msg/Empty.msg");
    let official_idl = fs::read_to_string(&idl_file)?;

    assert!(
        compare_parsed_idl(&generated_idl, &official_idl, "std_msgs/Empty")?,
        "Empty message IDL structures don't match"
    );

    Ok(())
}

/// Test a single specific message with fields
#[test]
#[ignore]
fn test_single_message_header() -> Result<(), Box<dyn std::error::Error>> {
    let msg_file = Path::new(ROS2_JAZZY_SHARE).join("std_msgs/msg/Header.msg");
    let idl_file = Path::new(ROS2_JAZZY_SHARE).join("std_msgs/msg/Header.idl");

    let msg_spec = parse_message_file("std_msgs", &msg_file)?;
    let generated_idl = message_to_idl(&msg_spec, "std_msgs", "msg/Header.msg");
    let official_idl = fs::read_to_string(&idl_file)?;

    assert!(
        compare_parsed_idl(&generated_idl, &official_idl, "std_msgs/Header")?,
        "Header message IDL structures don't match"
    );

    Ok(())
}