Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(stats): reorganize modules

Elijah Potter264c2c23a8f9
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index f6f0ccb2e..8eed221ae 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -209,7 +209,7 @@ fn main() -> anyhow::Result<()> {
let mut reader = BufReader::new(file);
let stats = Stats::read_csv(&mut reader)?;
- let summary = stats.summarize_lints_applied();
+ let summary = stats.summarize();
println!("{summary}");
Ok(())
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index e5fccf96a..5bd913a0c 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -1,3 +1,4 @@
+use harper_stats::RecordKind;
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::{BufWriter, Write};
@@ -36,7 +37,7 @@ use crate::config::Config;
use crate::dictionary_io::{file_dict_name, load_dict, save_dict};
use crate::document_state::DocumentState;
use crate::git_commit_parser::GitCommitParser;
-use harper_stats::{LintRecord, Stats};
+use harper_stats::{Record, Stats};
pub struct Backend {
client: Client,
@@ -530,13 +531,13 @@ impl LanguageServer for Backend {
return Ok(None);
};
- let Ok(record) = LintRecord::record_now(kind) else {
+ let Ok(record) = Record::now(RecordKind::Lint(kind)) else {
error!("System time error");
return Ok(None);
};
let mut stats = self.stats.write().await;
- stats.lint_applied(record);
+ stats.records.push(record);
}
"HarperAddToUserDict" => {
let word = &first.chars().collect::<Vec<_>>();
diff --git a/harper-stats/src/lib.rs b/harper-stats/src/lib.rs
index 77b4bc788..6c710e94b 100644
--- a/harper-stats/src/lib.rs
+++ b/harper-stats/src/lib.rs
@@ -1,34 +1,32 @@
-mod lint_record;
-mod lint_summary;
+mod record;
+mod summary;
use std::io::{self, Read, Write};
-pub use lint_record::LintRecord;
-pub use lint_summary::LintSummary;
+pub use record::Record;
+pub use record::RecordKind;
+pub use summary::Summary;
/// 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>,
+ pub records: Vec<Record>,
}
impl Stats {
pub fn new() -> Self {
Self {
- lints_applied: Vec::new(),
+ records: Vec::new(),
}
}
- pub fn lint_applied(&mut self, record: LintRecord) {
- 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();
+ pub fn summarize(&self) -> Summary {
+ let mut summary = Summary::new();
- for lint in &self.lints_applied {
- summary.inc(lint.kind);
+ for record in &self.records {
+ match record.kind {
+ RecordKind::Lint(lint_kind) => summary.inc_lint_count(lint_kind),
+ }
}
summary
@@ -37,7 +35,7 @@ impl Stats {
pub fn write_csv(&self, w: &mut impl Write) -> io::Result<()> {
let mut writer = csv::WriterBuilder::new().has_headers(false).from_writer(w);
- for record in &self.lints_applied {
+ for record in &self.records {
writer.serialize(record)?;
}
@@ -49,14 +47,12 @@ impl Stats {
let mut records = Vec::new();
for result in reader.deserialize() {
- let record: LintRecord = result?;
+ let record: Record = result?;
records.push(record);
}
- Ok(Self {
- lints_applied: records,
- })
+ Ok(Self { records })
}
}
diff --git a/harper-stats/src/lint_record.rs b/harper-stats/src/record.rs
similarity index 70%
rename from harper-stats/src/lint_record.rs
rename to harper-stats/src/record.rs
index 98c8b89a6..cbd6f2be3 100644
--- a/harper-stats/src/lint_record.rs
+++ b/harper-stats/src/record.rs
@@ -5,16 +5,16 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Deserialize, Serialize)]
-pub struct LintRecord {
- pub kind: LintKind,
+pub struct Record {
+ pub kind: RecordKind,
/// Recorded as seconds from the Unix Epoch
pub when: u64,
pub uuid: Uuid,
}
-impl LintRecord {
+impl Record {
/// Record a new instance at the current system time.
- pub fn record_now(kind: LintKind) -> Result<Self, SystemTimeError> {
+ pub fn now(kind: RecordKind) -> Result<Self, SystemTimeError> {
Ok(Self {
kind,
when: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(),
@@ -22,3 +22,8 @@ impl LintRecord {
})
}
}
+
+#[derive(Debug, Deserialize, Serialize)]
+pub enum RecordKind {
+ Lint(LintKind),
+}
diff --git a/harper-stats/src/lint_summary.rs b/harper-stats/src/summary.rs
similarity index 63%
rename from harper-stats/src/lint_summary.rs
rename to harper-stats/src/summary.rs
index 2edf59ac6..5d2a492fa 100644
--- a/harper-stats/src/lint_summary.rs
+++ b/harper-stats/src/summary.rs
@@ -2,20 +2,20 @@ use std::{collections::HashMap, fmt::Display};
use harper_core::linting::LintKind;
-pub struct LintSummary {
- counts: HashMap<LintKind, usize>,
+pub struct Summary {
+ lint_counts: HashMap<LintKind, usize>,
}
-impl LintSummary {
+impl Summary {
pub fn new() -> Self {
Self {
- counts: HashMap::new(),
+ lint_counts: HashMap::new(),
}
}
/// Increment the count for a particular lint kind.
- pub fn inc(&mut self, kind: LintKind) {
- self.counts
+ pub fn inc_lint_count(&mut self, kind: LintKind) {
+ self.lint_counts
.entry(kind)
.and_modify(|counter| *counter += 1)
.or_insert(1);
@@ -23,19 +23,19 @@ impl LintSummary {
/// Get the count for a particular lint kind.
pub fn get(&self, kind: LintKind) -> usize {
- self.counts.get(&kind).copied().unwrap_or(0)
+ self.lint_counts.get(&kind).copied().unwrap_or(0)
}
}
-impl Default for LintSummary {
+impl Default for Summary {
fn default() -> Self {
Self::new()
}
}
-impl Display for LintSummary {
+impl Display for Summary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- for (kind, count) in &self.counts {
+ for (kind, count) in &self.lint_counts {
writeln!(f, "{kind}\t{count}")?;
}