use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
const EM_DASH: char = '\u{2014}';
const EN_DASH: char = '\u{2013}';
const BINARY_SUFFIXES: [&str; 13] = [
"png", "jpg", "jpeg", "gif", "ico", "webp", "pdf", "zip", "gz", "tar", "whl", "so", "dylib",
];
const EXCLUDED_DIRS: [&str; 9] = [
".git",
"__pycache__",
".pytest_cache",
".venv",
"venv",
"node_modules",
"build",
"dist",
"target",
];
struct ScanError(String);
fn has_binary_suffix(path: &Path) -> bool {
match path.extension().and_then(|ext| ext.to_str()) {
Some(ext) => BINARY_SUFFIXES.contains(&ext.to_lowercase().as_str()),
None => false,
}
}
fn collect(root: &Path, out: &mut Vec<PathBuf>) -> Result<(), ScanError> {
let mut dirs: Vec<PathBuf> = Vec::new();
let mut files: Vec<PathBuf> = Vec::new();
let entries = std::fs::read_dir(root)
.map_err(|err| ScanError(format!("cannot read {}, {err}", root.display())))?;
for entry in entries {
let entry =
entry.map_err(|err| ScanError(format!("cannot read {}, {err}", root.display())))?;
let path = entry.path();
let file_type = entry
.file_type()
.map_err(|err| ScanError(format!("cannot read {}, {err}", path.display())))?;
if file_type.is_dir() {
dirs.push(path);
} else if file_type.is_file() {
files.push(path);
} else if file_type.is_symlink() {
match std::fs::metadata(&path) {
Ok(meta) if meta.is_file() => files.push(path),
Ok(_) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => {
return Err(ScanError(format!("cannot read {}, {err}", path.display())));
}
}
}
}
dirs.sort();
files.sort();
for file in files {
if !has_binary_suffix(&file) {
out.push(file);
}
}
for dir in dirs {
let name = dir
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
if !EXCLUDED_DIRS.contains(&name.as_str()) {
collect(&dir, out)?;
}
}
Ok(())
}
fn scan(root: &Path) -> Result<(usize, usize, String), ScanError> {
let mut files = Vec::new();
collect(root, &mut files)?;
let mut em_count = 0;
let mut en_count = 0;
let mut report = String::new();
for path in &files {
let bytes = std::fs::read(path)
.map_err(|err| ScanError(format!("cannot read {}, {err}", path.display())))?;
let text = std::str::from_utf8(&bytes).map_err(|err| {
ScanError(format!("cannot decode {} as UTF-8, {err}", path.display()))
})?;
let mut file_em = 0;
let mut file_en = 0;
for ch in text.chars() {
if ch == EM_DASH {
file_em += 1;
} else if ch == EN_DASH {
file_en += 1;
}
}
if file_em > 0 || file_en > 0 {
report.push_str(&format!(
"{}: em dash {file_em}, en dash {file_en}\n",
path.display()
));
}
em_count += file_em;
en_count += file_en;
}
Ok((em_count, en_count, report))
}
fn eprint_line(text: &str) {
let stderr = std::io::stderr();
let mut lock = stderr.lock();
let _ = writeln!(lock, "{text}");
}
fn main() -> ExitCode {
let mut args = std::env::args_os();
let root = args
.nth(1)
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."));
if args.next().is_some() {
eprint_line("scan error, unexpected extra argument after the scan root");
return ExitCode::from(2);
}
match scan(&root) {
Ok((em_count, en_count, report)) => {
let text = format!(
"{report}em dash U+2014 count {em_count}\nen dash U+2013 count {en_count}\n"
);
let stdout = std::io::stdout();
let mut lock = stdout.lock();
if lock.write_all(text.as_bytes()).is_err() || lock.flush().is_err() {
eprint_line("scan error, the scan output could not be written");
return ExitCode::from(2);
}
if em_count > 0 || en_count > 0 {
ExitCode::from(1)
} else {
ExitCode::from(0)
}
}
Err(ScanError(message)) => {
eprint_line(&format!("scan error, {message}"));
ExitCode::from(2)
}
}
}