use std::collections::BTreeSet;
use std::io::Write;
use crate::check::Misspelling;
pub fn write_text<W: Write>(out: &mut W, miss: &[Misspelling]) -> std::io::Result<()> {
for m in miss {
let sugs = m
.suggestions
.as_ref()
.map(|s| s.join(", "))
.unwrap_or_default();
if sugs.is_empty() {
writeln!(out, "{}:{}:{} {}", m.path.display(), m.line, m.col, m.word)?;
} else {
writeln!(
out,
"{}:{}:{} {} -> {}",
m.path.display(),
m.line,
m.col,
m.word,
sugs
)?;
}
}
Ok(())
}
pub fn write_json<W: Write>(out: &mut W, miss: &[Misspelling]) -> anyhow::Result<()> {
let rows: Vec<serde_json::Value> = miss
.iter()
.map(|m| {
let mut row = serde_json::json!({
"file": m.path.display().to_string(),
"line": m.line,
"column": m.col,
"byte_offset": m.byte_offset,
"word": m.word,
"compound": m.compound.as_ref().map(|(w, _)| w),
});
if let Some(sugs) = &m.suggestions {
row["suggestions"] = serde_json::json!(sugs);
} else {
row["suggestions"] = serde_json::json!(Vec::<String>::new());
}
row
})
.collect();
let json = serde_json::to_string_pretty(&rows)?;
out.write_all(json.as_bytes())?;
writeln!(out)?;
Ok(())
}
pub fn write_words<W: Write>(out: &mut W, miss: &[Misspelling]) -> std::io::Result<()> {
let unique: BTreeSet<&str> = miss.iter().map(|m| m.word.as_str()).collect();
for w in unique {
writeln!(out, "{w}")?;
}
Ok(())
}
pub fn summary(miss: &[Misspelling]) -> String {
let files: BTreeSet<&std::path::Path> = miss.iter().map(|m| m.path.as_path()).collect();
let word = if miss.len() == 1 {
"misspelling"
} else {
"misspellings"
};
format!(
"{} {word} across {} file{}",
miss.len(),
files.len(),
if files.len() == 1 { "" } else { "s" }
)
}