1use std::{collections::HashMap, fmt::Display};
2
3use harper_core::linting::{FlatConfig, LintKind};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Default, Serialize, Deserialize)]
7pub struct Summary {
8 pub lint_counts: HashMap<LintKind, u32>,
9 pub total_applied: u32,
10 pub final_config: FlatConfig,
11 pub misspelled: HashMap<String, u32>,
13}
14
15impl Summary {
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 pub fn inc_lint_count(&mut self, kind: LintKind) {
22 self.lint_counts
23 .entry(kind)
24 .and_modify(|counter| *counter += 1)
25 .or_insert(1);
26 self.total_applied += 1;
27 }
28
29 pub fn inc_misspelled_count(&mut self, word: impl AsRef<str>) {
31 if let Some(counter) = self.misspelled.get_mut(word.as_ref()) {
32 *counter += 1
33 } else {
34 self.misspelled.insert(word.as_ref().to_owned(), 1);
35 }
36 }
37
38 pub fn get_count(&self, kind: LintKind) -> u32 {
40 self.lint_counts.get(&kind).copied().unwrap_or(0)
41 }
42}
43
44impl Display for Summary {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 writeln!(f, "`LintKind` counts")?;
47 writeln!(f, "=================")?;
48
49 for (kind, count) in &self.lint_counts {
50 writeln!(f, "{kind}\t{count}")?;
51 }
52
53 writeln!(f, "Misspelling counts")?;
54 writeln!(f, "=================")?;
55
56 let mut misspelled: Vec<_> = self
57 .misspelled
58 .iter()
59 .map(|(a, b)| (a.clone(), *b))
60 .collect();
61
62 misspelled.sort_by_key(|(_a, b)| u32::MAX - b);
63
64 for (kind, count) in &misspelled {
65 writeln!(f, "{kind}\t{count}")?;
66 }
67
68 Ok(())
69 }
70}