use crate::fix::Map;
use crate::judge::{Checker, Config, File, files};
use crate::render::{self, FileStats, Format, Skipped};
use crate::scan::Unreadable;
use crate::tokens::{self, Count, Method};
struct Row {
path: String,
outside: u64,
bytes: u64,
now: Count,
after: Count,
}
pub(super) fn run(
config: &Config,
paths: &[String],
format: Format,
bpe: bool,
) -> Result<String, String> {
let pass = Pass {
checker: Checker::configured(config)?,
map: config.map()?,
method: if bpe { Method::Bpe } else { Method::Estimate },
};
let (rows, skips) = pass.walk(files(&config.root, paths)?)?;
let listed: Vec<FileStats<'_>> = rows.iter().map(stat).collect();
let skipped: Vec<Skipped<'_>> = skips.iter().map(skip).collect();
Ok(render::stats(format, &listed, &skipped))
}
type Skip = (String, Unreadable);
type Walked = (Vec<Row>, Vec<Skip>);
fn skip((path, reason): &Skip) -> Skipped<'_> {
Skipped {
path,
reason: *reason,
}
}
struct Pass {
checker: Checker,
map: Map,
method: Method,
}
impl Pass {
fn walk(
&self,
files: impl Iterator<Item = Result<File, String>>,
) -> Result<Walked, String> {
let (mut rows, mut skips) = (Vec::new(), Vec::new());
for file in files {
let File { shown, text, .. } = file?;
match text {
Ok(text) => rows.push(self.counted(&shown, &text)?),
Err(reason) => skips.push((shown, reason)),
}
}
Ok((rows, skips))
}
fn counted(&self, shown: &str, text: &str) -> Result<Row, String> {
let (set, levels) = self.checker.shared_law(shown)?;
let allowed = |point: char| set.contains(point);
let fixed = self
.checker
.judge(&set)
.fix(text, &self.map, &levels)
.map_err(|bad| format!("{shown}: {bad}"))?;
let outside = text.chars().filter(|point| !allowed(*point)).count();
Ok(Row {
path: shown.to_owned(),
outside: outside as u64,
bytes: text.len() as u64,
now: tokens::of_text(text, self.method),
after: tokens::of_text(&fixed.output, self.method),
})
}
}
fn stat(row: &Row) -> FileStats<'_> {
FileStats {
path: &row.path,
outside: row.outside,
bytes: row.bytes,
now: row.now,
after: row.after,
}
}
#[cfg(test)]
mod tests {
use super::run;
use crate::cli::testkit::fixture;
use crate::render::Format;
use std::path::Path;
fn ran(root: &Path, bpe: bool) -> String {
let asked = ["notes.md".to_owned()];
run(
&crate::cli::config::discovered(root),
&asked,
Format::Human,
bpe,
)
.unwrap_or_else(|why| why)
}
#[test]
fn a_row_carries_the_cost_now_and_after() {
let files = [("notes.md", "a \u{2026} b\n")];
let Some(root) = fixture("ctrm-stats-fixture", &files) else {
return;
};
let said = ran(&root, false);
assert!(said.starts_with("notes.md outside=1 bytes=8"), "{said}");
assert!(said.contains(" tokens=~"), "{said}");
assert!(said.contains("->"), "{said}");
}
#[test]
fn the_figure_says_which_method_produced_it() {
let files = [("notes.md", "plain ascii\n")];
let Some(root) = fixture("ctrm-stats-method-fixture", &files) else {
return;
};
assert!(ran(&root, false).contains('~'), "estimate wears a tilde");
assert!(ran(&root, true).contains("o200k"), "bpe names its encoding");
}
#[test]
fn a_binary_file_is_not_counted() {
let files = [("notes.md", "\0\u{2014}data\0")];
let Some(root) = fixture("ctrm-stats-binary-fixture", &files) else {
return;
};
assert_eq!(ran(&root, false), "notes.md: skipped, binary");
}
#[test]
fn a_file_that_is_not_utf8_is_named() {
let Some(root) = fixture("ctrm-stats-not-utf8-fixture", &[]) else {
return;
};
if std::fs::write(root.join("notes.md"), b"ab\xffcd\n").is_err() {
return;
}
let said = ran(&root, false);
assert_eq!(said, "notes.md: invalid UTF-8 at 1:3 (byte 2)");
}
#[test]
fn a_clean_file_costs_the_same_after() {
let files = [("notes.md", "plain ascii\n")];
let Some(root) = fixture("ctrm-stats-clean-fixture", &files) else {
return;
};
let said = ran(&root, false);
let row = said.lines().next().unwrap_or_default();
assert!(row.contains("outside=0"), "{said}");
let halves: Vec<&str> = row.split("->").collect();
assert_eq!(halves.len(), 2, "{said}");
assert!(
halves.last().is_some_and(|after| {
halves.first().is_some_and(|now| now.ends_with(after))
}),
"{said}"
);
}
}