use super::OutputFormatter;
use crate::config::OutputConfig;
use crate::search::SearchResult;
use anyhow::Result;
use serde::Serialize;
use std::io::Write;
#[derive(Default)]
pub struct XmlFormatter;
impl 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(())
}
}