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
/// Message specification parsing
use std::collections::HashMap;
use std::fs;
use std::path::Path;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use super::errors::{ParseError, ParseResult};
use crate::msg::types::{AnnotationValue, Annotations, Constant, Field, Type};
use crate::msg::validation::{
    COMMENT_DELIMITER, CONSTANT_SEPARATOR, OPTIONAL_ANNOTATION, is_valid_message_name,
    is_valid_package_name,
};

/// Message specification
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MessageSpecification {
    /// Package name
    pub pkg_name: String,
    /// Message name
    pub msg_name: String,
    /// List of fields
    pub fields: Vec<Field>,
    /// List of constants
    pub constants: Vec<Constant>,
    /// Annotations for the message
    pub annotations: Annotations,
}

impl MessageSpecification {
    /// Create a new empty message specification
    ///
    /// # Errors
    ///
    /// Returns [`ParseError::InvalidResourceName`] if the package name or message name are invalid.
    pub fn new(pkg_name: String, msg_name: String) -> ParseResult<Self> {
        if !is_valid_package_name(&pkg_name) {
            return Err(ParseError::InvalidResourceName {
                name: pkg_name,
                reason: "invalid package name pattern".to_string(),
            });
        }

        if !is_valid_message_name(&msg_name) {
            return Err(ParseError::InvalidResourceName {
                name: msg_name,
                reason: "invalid message name pattern".to_string(),
            });
        }

        Ok(MessageSpecification {
            pkg_name,
            msg_name,
            fields: Vec::new(),
            constants: Vec::new(),
            annotations: HashMap::new(),
        })
    }

    /// Add a field to the message
    pub fn add_field(&mut self, field: Field) {
        self.fields.push(field);
    }

    /// Add a constant to the message
    pub fn add_constant(&mut self, constant: Constant) {
        self.constants.push(constant);
    }

    /// Get field by name
    #[must_use]
    pub fn get_field(&self, name: &str) -> Option<&Field> {
        self.fields.iter().find(|f| f.name == name)
    }

    /// Get constant by name
    #[must_use]
    pub fn get_constant(&self, name: &str) -> Option<&Constant> {
        self.constants.iter().find(|c| c.name == name)
    }

    /// Check if message has any fields
    #[must_use]
    pub fn has_fields(&self) -> bool {
        !self.fields.is_empty()
    }

    /// Check if message has any constants
    #[must_use]
    pub fn has_constants(&self) -> bool {
        !self.constants.is_empty()
    }
}

impl std::fmt::Display for MessageSpecification {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "# {}/{}", self.pkg_name, self.msg_name)?;

        // Write constants first
        for constant in &self.constants {
            writeln!(f, "{constant}")?;
        }

        if !self.constants.is_empty() && !self.fields.is_empty() {
            writeln!(f)?; // Empty line between constants and fields
        }

        // Write fields
        for field in &self.fields {
            writeln!(f, "{field}")?;
        }

        Ok(())
    }
}

/// Parse a message file
///
/// # Errors
///
/// Returns [`ParseError`] if the file cannot be read or the message format is invalid.
pub fn parse_message_file<P: AsRef<Path>>(
    pkg_name: &str,
    interface_filename: P,
) -> ParseResult<MessageSpecification> {
    let path = interface_filename.as_ref();
    let basename =
        path.file_name()
            .and_then(|n| n.to_str())
            .ok_or_else(|| ParseError::InvalidField {
                reason: "invalid filename".to_string(),
            })?;

    let msg_name = basename
        .strip_suffix(".msg")
        .unwrap_or(basename)
        .to_string();

    let content = fs::read_to_string(path)?;
    parse_message_string(pkg_name, &msg_name, &content)
}

/// Parse a message from string content
///
/// # Errors
///
/// Returns [`ParseError`] if the message format is invalid.
pub fn parse_message_string(
    pkg_name: &str,
    msg_name: &str,
    message_string: &str,
) -> ParseResult<MessageSpecification> {
    let mut spec = MessageSpecification::new(pkg_name.to_string(), msg_name.to_string())?;

    // Replace tabs with spaces for consistent parsing
    let normalized_content = message_string.replace('\t', " ");

    // Extract file-level comments and content
    let (file_level_comments, content_lines) = extract_file_level_comments(&normalized_content);

    // Set file-level comments as message annotations
    if !file_level_comments.is_empty() {
        spec.annotations.insert(
            "comment".to_string(),
            AnnotationValue::StringList(file_level_comments),
        );
    }

    // Parse content lines
    let mut current_comments = Vec::<String>::new();
    let mut is_optional = false;

    for (line_num, line) in content_lines.iter().enumerate() {
        let line = line.trim_end();

        // Skip empty lines
        if line.trim().is_empty() {
            continue;
        }

        // Handle comments
        let (line_content, comment) = extract_line_comment(line);

        if let Some(comment_text) = comment {
            if line_content.trim().is_empty() {
                // This is a comment-only line - collect for next element
                current_comments.push(comment_text);
                continue;
            }
            // Line has both content and comment, collect the comment
            current_comments.push(comment_text);
        }

        let line_content = line_content.trim();
        if line_content.is_empty() {
            continue;
        }

        // Check for optional annotation
        if line_content == OPTIONAL_ANNOTATION {
            is_optional = true;
            continue;
        }

        // Parse the line as field or constant
        match parse_line_content(line_content, pkg_name, line_num + 1) {
            Ok(LineContent::Field(mut field)) => {
                // Add collected comments
                if !current_comments.is_empty() {
                    field.annotations.insert(
                        "comment".to_string(),
                        AnnotationValue::StringList(current_comments.clone()),
                    );
                    current_comments.clear();
                }

                // Add optional annotation if present
                if is_optional {
                    field
                        .annotations
                        .insert("optional".to_string(), AnnotationValue::Bool(true));
                    is_optional = false;
                }

                spec.add_field(field);
            }
            Ok(LineContent::Constant(mut constant)) => {
                // Add collected comments
                if !current_comments.is_empty() {
                    constant.annotations.insert(
                        "comment".to_string(),
                        AnnotationValue::StringList(current_comments.clone()),
                    );
                    current_comments.clear();
                }

                spec.add_constant(constant);
            }
            Err(e) => {
                return Err(ParseError::LineParseError {
                    line: line_num + 1,
                    message: format!("Error parsing line '{line_content}': {e}"),
                });
            }
        }
    }

    // Process comments for all elements
    process_comments(&mut spec);

    Ok(spec)
}

/// Content parsed from a line
enum LineContent {
    Field(Field),
    Constant(Constant),
}

/// Extract file-level comments from the beginning of the message
fn extract_file_level_comments(message_string: &str) -> (Vec<String>, Vec<String>) {
    let lines: Vec<String> = message_string
        .lines()
        .map(std::string::ToString::to_string)
        .collect();

    let mut file_level_comments = Vec::new();
    let mut first_content_index = 0;

    // Extract comments at the very top, until we hit a blank line or non-comment content
    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();

        if trimmed.is_empty() {
            // Blank line marks end of file-level comments
            first_content_index = i + 1;
            break;
        } else if trimmed.starts_with(COMMENT_DELIMITER) {
            // This is a file-level comment
            if let Some(comment_text) = trimmed.strip_prefix(COMMENT_DELIMITER) {
                file_level_comments.push(comment_text.trim_start().to_string());
            }
        } else {
            // First non-comment, non-blank line - no file-level comments if we haven't seen a blank
            first_content_index = i;
            break;
        }
    }

    let content_lines = lines[first_content_index..].to_vec();

    (file_level_comments, content_lines)
}

/// Extract comment from a line, returning (content, comment)
fn extract_line_comment(line: &str) -> (String, Option<String>) {
    if let Some(comment_index) = line.find(COMMENT_DELIMITER) {
        let content = line[..comment_index].to_string();
        let comment = line[comment_index + 1..].trim_start().to_string();
        (content, Some(comment))
    } else {
        (line.to_string(), None)
    }
}

/// Parse a single line of content (field or constant definition)
fn parse_line_content(line: &str, pkg_name: &str, _line_num: usize) -> ParseResult<LineContent> {
    // Check if this is a constant (contains '=' but not as part of '<=' array bounds)
    if line.contains(CONSTANT_SEPARATOR) && !is_array_bound_syntax(line) {
        parse_constant_line(line)
    } else {
        parse_field_line(line, pkg_name)
    }
}

/// Check if line contains array bound syntax (<=) which should not be confused with constants
fn is_array_bound_syntax(line: &str) -> bool {
    // Check for array bounds in brackets
    if line.contains("<=") && (line.contains('[') || line.contains(']')) {
        return true;
    }

    // Check for string bounds (e.g., "string<=50")
    if line.contains("<=") && (line.contains("string") || line.contains("wstring")) {
        return true;
    }

    false
}

/// Parse a constant definition line
fn parse_constant_line(line: &str) -> ParseResult<LineContent> {
    let parts: Vec<&str> = line.splitn(2, CONSTANT_SEPARATOR).collect();
    if parts.len() != 2 {
        return Err(ParseError::InvalidConstant {
            reason: "constant must have format: TYPE NAME=VALUE".to_string(),
        });
    }

    let left_part = parts[0].trim();
    let value_part = parts[1].trim();

    // Parse type and name from left part
    let type_name_parts: Vec<&str> = left_part.split_whitespace().collect();
    if type_name_parts.len() != 2 {
        return Err(ParseError::InvalidConstant {
            reason: "constant must have format: TYPE NAME=VALUE".to_string(),
        });
    }

    let type_name = type_name_parts[0];
    let const_name = type_name_parts[1];

    let constant = Constant::new(type_name, const_name, value_part)?;
    Ok(LineContent::Constant(constant))
}

/// Parse a field definition line
fn parse_field_line(line: &str, pkg_name: &str) -> ParseResult<LineContent> {
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 2 {
        return Err(ParseError::InvalidField {
            reason: "field must have at least type and name".to_string(),
        });
    }

    let type_string = parts[0];
    let field_name = parts[1];

    // Check for default value
    let default_value = if parts.len() > 2 {
        Some(parts[2..].join(" "))
    } else {
        None
    };

    let field_type = Type::new(type_string, Some(pkg_name))?;
    let field = Field::new(field_type, field_name, default_value.as_deref())?;

    Ok(LineContent::Field(field))
}

/// Process comments to extract special annotations like units
fn process_comments(spec: &mut MessageSpecification) {
    // Process message-level comments
    process_element_comments(&mut spec.annotations);

    // Process field comments
    for field in &mut spec.fields {
        process_element_comments(&mut field.annotations);
    }

    // Process constant comments
    for constant in &mut spec.constants {
        process_element_comments(&mut constant.annotations);
    }
}

/// Process comments for a single element to extract special annotations
fn process_element_comments(annotations: &mut Annotations) {
    if let Some(AnnotationValue::StringList(comments)) = annotations.get("comment").cloned() {
        // Look for unit annotations in brackets
        let comment_text = comments.join("\n");

        let mut processed_comments = if let Some(unit) = extract_unit_from_comment(&comment_text) {
            annotations.insert("unit".to_string(), AnnotationValue::String(unit.clone()));

            // Remove unit from comments
            comments
                .into_iter()
                .map(|line| remove_unit_from_line(&line, &unit))
                .collect()
        } else {
            comments
        };

        // Remove empty lines and update comments
        processed_comments.retain(|line| !line.trim().is_empty());

        if processed_comments.is_empty() {
            annotations.remove("comment");
        } else {
            annotations.insert(
                "comment".to_string(),
                AnnotationValue::StringList(processed_comments),
            );
        }
    }
}

/// Extract unit annotation from comment text
fn extract_unit_from_comment(comment: &str) -> Option<String> {
    // Look for [unit] pattern that doesn't contain commas
    let re = regex::Regex::new(r"\[([^,\]]+)\]").ok()?;
    let captures = re.captures(comment)?;
    captures.get(1).map(|m| m.as_str().trim().to_string())
}

/// Remove unit annotation from a comment line
fn remove_unit_from_line(line: &str, unit: &str) -> String {
    let pattern = format!("[{unit}]");
    line.replace(&pattern, "").trim().to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::msg::validation::PrimitiveValue;

    #[test]
    fn test_parse_simple_message() {
        let content = r"
# This is a test message
int32 x
int32 y
string name
";

        let spec = parse_message_string("test_msgs", "TestMessage", content).unwrap();
        assert_eq!(spec.pkg_name, "test_msgs");
        assert_eq!(spec.msg_name, "TestMessage");
        assert_eq!(spec.fields.len(), 3);
        assert_eq!(spec.fields[0].name, "x");
        assert_eq!(spec.fields[1].name, "y");
        assert_eq!(spec.fields[2].name, "name");
    }

    #[test]
    fn test_parse_message_with_constants() {
        let content = r#"
# Constants
int32 MAX_VALUE=100
string DEFAULT_NAME="test"

# Fields
int32 value
string name
"#;

        let spec = parse_message_string("test_msgs", "TestMessage", content).unwrap();
        assert_eq!(spec.constants.len(), 2);
        assert_eq!(spec.fields.len(), 2);

        let max_const = spec.get_constant("MAX_VALUE").unwrap();
        assert_eq!(max_const.value, PrimitiveValue::Int32(100));
    }

    #[test]
    fn test_parse_message_with_arrays() {
        let content = r"
int32[] dynamic_array
int32[5] fixed_array
int32[<=10] bounded_array
";

        let spec = parse_message_string("test_msgs", "TestMessage", content).unwrap();
        assert_eq!(spec.fields.len(), 3);

        assert!(spec.fields[0].field_type.is_dynamic_array());
        assert_eq!(spec.fields[1].field_type.array_size, Some(5));
        assert!(spec.fields[2].field_type.is_bounded_array());
    }

    #[test]
    fn test_parse_message_with_comments() {
        let content = r"
# File level comment
# Second line

int32 x  # X coordinate
int32 y  # Y coordinate
";

        let spec = parse_message_string("test_msgs", "TestMessage", content).unwrap();

        // Should have file-level comments
        if let Some(AnnotationValue::StringList(comments)) = spec.annotations.get("comment") {
            assert!(comments.contains(&"File level comment".to_string()));
        }

        // Fields should have comments
        assert!(spec.fields[0].annotations.contains_key("comment"));
        assert!(spec.fields[1].annotations.contains_key("comment"));
    }

    #[test]
    fn test_parse_message_with_optional_fields() {
        let content = r"
int32 required_field
@optional
int32 optional_field
";

        let spec = parse_message_string("test_msgs", "TestMessage", content).unwrap();
        assert_eq!(spec.fields.len(), 2);

        // First field should not be optional
        assert!(!spec.fields[0].annotations.contains_key("optional"));

        // Second field should be optional
        if let Some(AnnotationValue::Bool(is_optional)) = spec.fields[1].annotations.get("optional")
        {
            assert!(is_optional);
        }
    }
}