Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(cli): ability to summarize `stats.txt` files
Elijah Potter223d165f23b2
diff --git a/Cargo.lock b/Cargo.lockindex d40ac8039..f083bc164 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -626,6 +626,7 @@ dependencies = ["harper-comments","harper-core","harper-literate-haskell",+ "harper-stats","harper-typst","serde_json",]diff --git a/harper-cli/Cargo.toml b/harper-cli/Cargo.tomlindex 05528322f..880f6d245 100644--- a/harper-cli/Cargo.toml+++ b/harper-cli/Cargo.toml@@ -14,6 +14,7 @@ harper-literate-haskell = { path = "../harper-literate-haskell", version = "0.16harper-core = { path = "../harper-core", version = "0.16.0" }harper-comments = { path = "../harper-comments", version = "0.16.0" }harper-typst = { path = "../harper-typst", version = "0.16.0" }+harper-stats = { path = "../harper-stats", version = "0.16.0" }serde_json = "1.0.135"[features]diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rsindex d306b0218..fa9e043b0 100644--- a/harper-cli/src/main.rs+++ b/harper-cli/src/main.rs@@ -1,5 +1,7 @@#![doc = include_str!("../README.md")]+use std::fs::File;+use std::io::BufReader;use std::path::{Path, PathBuf};use anyhow::format_err;@@ -10,6 +12,7 @@ use harper_core::linting::{LintGroup, LintGroupConfig, Linter};use harper_core::parsers::{Markdown, MarkdownOptions};use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary, TokenKind};use harper_literate_haskell::LiterateHaskellParser;+use harper_stats::Stats;/// A debugging tool for the Harper grammar checker.#[derive(Debug, Parser)]@@ -41,6 +44,8 @@ enum Args {Metadata { word: String },/// Emit a decompressed, line-separated list of the words in Harper's dictionary.Words,+ /// Summarize a lint record+ SummarizeLintRecord { file: PathBuf },}fn main() -> anyhow::Result<()> {@@ -165,6 +170,16 @@ fn main() -> anyhow::Result<()> {println!("{json}");+ Ok(())+ }+ Args::SummarizeLintRecord { file } => {+ let file = File::open(file)?;+ let mut reader = BufReader::new(file);+ let stats = Stats::read_csv(&mut reader)?;++ let summary = stats.summarize_lints_applied();+ println!("{summary}");+Ok(())}}diff --git a/harper-stats/src/lib.rs b/harper-stats/src/lib.rsindex 0e4db6887..77b4bc788 100644--- a/harper-stats/src/lib.rs+++ b/harper-stats/src/lib.rs@@ -1,9 +1,12 @@mod lint_record;+mod lint_summary;-use std::io::{self, Write};+use std::io::{self, Read, Write};pub use lint_record::LintRecord;+pub use lint_summary::LintSummary;+/// A collection of logged statistics for the various Harper frontends.pub struct Stats {/// A record of the lints the user has applied.lints_applied: Vec<LintRecord>,@@ -20,6 +23,17 @@ impl Stats {self.lints_applied.push(record);}+ /// Count the number of each kind of lint applied.+ pub fn summarize_lints_applied(&self) -> LintSummary {+ let mut summary = LintSummary::new();++ for lint in &self.lints_applied {+ summary.inc(lint.kind);+ }++ summary+ }+pub fn write_csv(&self, w: &mut impl Write) -> io::Result<()> {let mut writer = csv::WriterBuilder::new().has_headers(false).from_writer(w);@@ -29,6 +43,21 @@ impl Stats {Ok(())}++ pub fn read_csv(r: &mut impl Read) -> io::Result<Self> {+ let mut reader = csv::ReaderBuilder::new().has_headers(false).from_reader(r);+ let mut records = Vec::new();++ for result in reader.deserialize() {+ let record: LintRecord = result?;++ records.push(record);+ }++ Ok(Self {+ lints_applied: records,+ })+ }}impl Default for Stats {diff --git a/harper-stats/src/lint_summary.rs b/harper-stats/src/lint_summary.rsnew file mode 100644index 000000000..b4c4fa5ac--- /dev/null+++ b/harper-stats/src/lint_summary.rs@@ -0,0 +1,38 @@+use std::{collections::HashMap, fmt::Display};++use harper_core::linting::LintKind;++pub struct LintSummary {+ counts: HashMap<LintKind, usize>,+}++impl LintSummary {+ pub fn new() -> Self {+ Self {+ counts: HashMap::new(),+ }+ }++ /// Increment the count for a particular lint kind.+ pub fn inc(&mut self, kind: LintKind) {+ self.counts+ .entry(kind)+ .and_modify(|counter| *counter += 1)+ .or_insert(1);+ }++ /// Get the count for a particular lint kind.+ pub fn get(&self, kind: LintKind) -> usize {+ self.counts.get(&kind).copied().unwrap_or(0)+ }+}++impl Display for LintSummary {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ for (kind, count) in &self.counts {+ writeln!(f, "{kind}\t{count}")?;+ }++ Ok(())+ }+}