use crate::sros2::LintViolation;
use roxmltree::Document;
pub fn lint_launch_xml(doc: &Document<'_>) -> Vec<LintViolation> {
let mut violations = Vec::new();
for node in doc.descendants().filter(|n| n.has_tag_name("node")) {
let has_respawn =
node.attribute("respawn") == Some("true") || node.attribute("respawn") == Some("True");
if !has_respawn {
let node_name = node.attribute("name").unwrap_or("unnamed_node");
violations.push(LintViolation {
message: format!("Launch XML Safety Violation: Node '{}' is missing `respawn=\"true\"`. If this node crashes during autonomous operation, it will not restart, potentially causing a critical system failure.", node_name),
range: node.range(),
});
}
}
violations
}
#[cfg(test)]
mod tests {
use super::*;
use roxmltree::Document;
#[test]
fn test_launch_xml_missing_respawn() {
let xml =
"<launch><node name=\"controller\" pkg=\"my_pkg\" exec=\"control_node\"/></launch>";
let doc = Document::parse(xml).unwrap();
let violations = lint_launch_xml(&doc);
assert_eq!(violations.len(), 1);
assert!(violations[0].message.contains("missing `respawn=\"true\"`"));
}
#[test]
fn test_launch_xml_valid_respawn() {
let xml = "<launch><node name=\"controller\" pkg=\"my_pkg\" exec=\"control_node\" respawn=\"true\"/></launch>";
let doc = Document::parse(xml).unwrap();
let violations = lint_launch_xml(&doc);
assert_eq!(violations.len(), 0);
}
}