opengrep 1.1.0

Advanced AST-aware code search tool with tree-sitter parsing and AI integration capabilities
Documentation
//! XML output formatter.

use super::OutputFormatter;
use crate::config::OutputConfig;
use crate::search::SearchResult;
use anyhow::Result;
// use quick_xml::se::Serializer;
use serde::Serialize;
use std::io::Write;

/// Formats search results as XML.
#[derive(Default)]
pub struct XmlFormatter;

impl XmlFormatter {
    /// Creates a new `XmlFormatter`.
    pub fn new() -> Self {
        Self
    }
}

#[derive(Serialize)]
struct XmlResults<'a> {
    #[serde(rename = "result")]
    results: Vec<&'a SearchResult>,
}

impl OutputFormatter for XmlFormatter {
    fn format<W: Write>(
        &self,
        results: &[SearchResult],
        writer: &mut W,
        _config: &OutputConfig,
    ) -> Result<()> {
        let results_with_matches: Vec<_> = results
            .iter()
            .filter(|r| !r.matches.is_empty())
            .collect();
        let xml_results = XmlResults {
            results: results_with_matches,
        };

        let xml_string = quick_xml::se::to_string(&xml_results)?;
        write!(writer, "{}", xml_string)?;

        Ok(())
    }
}