rosfix 0.2.0

High-Performance Multi-Language Static Verification and Active Remediation Engine for ROS 2
Documentation
use crate::sros2::LintViolation;
use std::path::Path;

/// Represents a single code remediation patch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemediationFix {
    pub file_path: String,
    pub start_byte: usize,
    pub end_byte: usize,
    pub original_snippet: String,
    pub replacement_snippet: String,
    pub description: String,
}

/// Generates AST-aware remediation patches for detected violations.
pub fn generate_fix(file_path: &str, violation: &LintViolation, content: &str) -> Option<RemediationFix> {
    let filename = Path::new(file_path)
        .file_name()
        .and_then(|f| f.to_str())
        .unwrap_or("");

    let start = violation.range.start;
    let end = violation.range.end;

    if start > content.len() || end > content.len() || start >= end {
        return None;
    }

    let original_snippet = content[start..end].to_string();

    // 1. C++ Hardcoded BEST_EFFORT Remediation
    if filename.ends_with(".cpp") || filename.ends_with(".hpp") || filename.ends_with(".cc") {
        if original_snippet == "best_effort" || original_snippet == "BEST_EFFORT" {
            return Some(RemediationFix {
                file_path: file_path.to_string(),
                start_byte: start,
                end_byte: end,
                original_snippet: original_snippet.clone(),
                replacement_snippet: "reliable".to_string(),
                description: "Refactored hardcoded BEST_EFFORT QoS override to safety-compliant RELIABLE parameter profile.".to_string(),
            });
        }
    }

    // 2. Python Launch Script Local Loopback Binding Remediation
    if filename.ends_with(".py") {
        if original_snippet.contains("127.0.0.1") || original_snippet.contains("localhost") {
            return Some(RemediationFix {
                file_path: file_path.to_string(),
                start_byte: start,
                end_byte: end,
                original_snippet: original_snippet.clone(),
                replacement_snippet: "LaunchConfiguration('network_interface', default='0.0.0.0')".to_string(),
                description: "Replaced hardcoded loopback IP with dynamic LaunchConfiguration network interface argument.".to_string(),
            });
        }
    }

    // 3. SROS2 Governance RTPS Protection Remediation
    if filename == "governance.xml" {
        if content.contains("<rtps_protection_kind>NONE</rtps_protection_kind>") {
            let start_tag = content.find("<rtps_protection_kind>NONE</rtps_protection_kind>")?;
            let end_tag = start_tag + "<rtps_protection_kind>NONE</rtps_protection_kind>".len();
            return Some(RemediationFix {
                file_path: file_path.to_string(),
                start_byte: start_tag,
                end_byte: end_tag,
                original_snippet: "<rtps_protection_kind>NONE</rtps_protection_kind>".to_string(),
                replacement_snippet: "<rtps_protection_kind>ENCRYPT</rtps_protection_kind>".to_string(),
                description: "Upgraded insecure RTPS protection from NONE to ENCRYPT.".to_string(),
            });
        }
    }

    // 4. SROS2 Permissions Wildcard Remediation
    if filename == "permissions.xml" {
        if original_snippet == "*" {
            return Some(RemediationFix {
                file_path: file_path.to_string(),
                start_byte: start,
                end_byte: end,
                original_snippet: "*".to_string(),
                replacement_snippet: "ros2_node_subject".to_string(),
                description: "Replaced overly permissive wildcard subject with explicit node identity subject constraint.".to_string(),
            });
        }
    }

    None
}

/// Applies remediation fixes to a file on disk or generates a unified patch string.
pub fn apply_remediation(_file_path: &str, content: &str, fixes: &[RemediationFix]) -> Result<String, String> {

    if fixes.is_empty() {
        return Ok(content.to_string());
    }

    let mut sorted_fixes = fixes.to_vec();
    // Sort in reverse byte order to preserve indices during string replacement
    sorted_fixes.sort_by(|a, b| b.start_byte.cmp(&a.start_byte));

    let mut updated_content = content.to_string();
    for fix in sorted_fixes {
        if fix.start_byte <= updated_content.len() && fix.end_byte <= updated_content.len() {
            updated_content.replace_range(fix.start_byte..fix.end_byte, &fix.replacement_snippet);
        }
    }

    Ok(updated_content)
}

/// Generates a standard unified diff string (.patch format) for code review.
pub fn generate_unified_diff(file_path: &str, original: &str, modified: &str) -> String {
    if original == modified {
        return String::new();
    }

    format!(
        "--- a/{}\n+++ b/{}\n@@ -1 +1 @@\n- {}\n+ {}",
        file_path,
        file_path,
        original.trim(),
        modified.trim()
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sros2::LintViolation;

    #[test]
    fn test_cpp_remediation_generation() {
        let content = "auto qos = rclcpp::QoS(10).best_effort();";
        let violation = LintViolation {
            message: "Hardcoded BEST_EFFORT".to_string(),
            range: 27..38,
        };

        let fix = generate_fix("node.cpp", &violation, content).expect("Fix should be generated");
        assert_eq!(fix.replacement_snippet, "reliable");
        let updated = apply_remediation("node.cpp", content, &[fix]).unwrap();
        assert_eq!(updated, "auto qos = rclcpp::QoS(10).reliable();");
    }

    #[test]
    fn test_python_remediation_generation() {
        let content = "node_ip = '127.0.0.1'";
        let violation = LintViolation {
            message: "Hardcoded IP".to_string(),
            range: 10..21,
        };

        let fix = generate_fix("launch.py", &violation, content).expect("Fix should be generated");
        let updated = apply_remediation("launch.py", content, &[fix]).unwrap();
        assert!(updated.contains("LaunchConfiguration('network_interface'"));
    }
}