#![forbid(unsafe_code)]
use std::path::{Path, PathBuf};
const TEXT: &[&str] = &["rs", "md", "toml", "yml", "yaml", "lock", "txt"];
const SKIP_DIRS: &[&str] = &["target", ".git"];
const SKIP_FILES: &[&str] = &["CLAUDE.local.md"];
fn root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn text_files(directory: &Path, into: &mut Vec<PathBuf>) {
let mut entries: Vec<PathBuf> = std::fs::read_dir(directory)
.unwrap_or_else(|error| panic!("reading {}: {error}", directory.display()))
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
.collect();
entries.sort();
for path in entries {
let name = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
if path.is_dir() {
if !SKIP_DIRS.contains(&name.as_str()) {
text_files(&path, into);
}
} else if !SKIP_FILES.contains(&name.as_str())
&& path
.extension()
.is_some_and(|extension| TEXT.iter().any(|text| extension == *text))
{
into.push(path);
}
}
}
#[test]
fn no_tracked_source_file_carries_a_byte_order_mark_or_a_non_ascii_byte() {
let root = root();
let mut files = Vec::new();
text_files(&root, &mut files);
assert!(
files.len() > 10,
"only {} text files were found under {}; is the walk working?",
files.len(),
root.display()
);
let mut failures: Vec<String> = Vec::new();
for path in &files {
let name = path
.strip_prefix(&root)
.unwrap_or(path)
.display()
.to_string();
let bytes = std::fs::read(path).unwrap_or_else(|error| panic!("reading {name}: {error}"));
if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
failures.push(format!("{name}: starts with a UTF-8 byte-order mark"));
continue;
}
if bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[0xFE, 0xFF]) {
failures.push(format!("{name}: starts with a UTF-16 byte-order mark"));
continue;
}
let text = match std::str::from_utf8(&bytes) {
Ok(text) => text,
Err(error) => {
failures.push(format!("{name}: is not valid UTF-8 ({error})"));
continue;
}
};
for (index, character) in text.char_indices() {
if character.is_ascii() {
continue;
}
let line = text[..index].matches('\n').count() + 1;
failures.push(format!(
"{name}:{line}: U+{:04X} {character:?} is not ASCII, and this repository's \
tracked source is ASCII so that a re-encode has nothing to corrupt",
character as u32
));
}
}
assert!(
failures.is_empty(),
"{} encoding problem(s) across {} text files:\n{}",
failures.len(),
files.len(),
failures.join("\n")
);
println!(
"{} text files checked: no byte-order mark, valid UTF-8, every byte ASCII",
files.len()
);
}
#[test]
fn the_library_source_is_ascii_only_and_is_the_reason_the_rule_can_be_ascii() {
let source = root().join("src");
let mut files = Vec::new();
text_files(&source, &mut files);
assert!(!files.is_empty(), "src/ holds no source files");
let mut offenders: Vec<String> = Vec::new();
let mut bytes_seen = 0usize;
for path in &files {
let raw = std::fs::read(path).expect("a source file reads");
bytes_seen += raw.len();
let name = path
.file_name()
.expect("a file has a name")
.to_string_lossy();
for (offset, byte) in raw.iter().enumerate() {
if !byte.is_ascii() {
offenders.push(format!("{name}: byte 0x{byte:02x} at offset {offset}"));
}
}
}
assert!(
offenders.is_empty(),
"src/ is not ASCII-only:\n{}",
offenders.join("\n")
);
println!(
"src/: {} files, {bytes_seen} bytes, every one of them ASCII",
files.len()
);
}