1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Architecture guard for the repository's Rust source-file hard limit.
use std::fs;
use std::path::{Path, PathBuf};
const HARD_LINE_LIMIT: usize = 800;
fn rust_files_under(directory: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
let mut directories = vec![directory.to_path_buf()];
while let Some(current) = directories.pop() {
for entry in fs::read_dir(¤t).expect("source directory should be readable") {
let path = entry.expect("source entry should be readable").path();
if path.is_dir() {
directories.push(path);
} else if path.extension().is_some_and(|extension| extension == "rs") {
files.push(path);
}
}
}
files.sort();
files
}
#[test]
fn rust_source_files_respect_hard_line_limit() {
let repository = Path::new(env!("CARGO_MANIFEST_DIR"));
let violations: Vec<String> = ["src", "tests", "examples", "benches"]
.into_iter()
.flat_map(|directory| rust_files_under(&repository.join(directory)))
.filter_map(|path| {
let line_count = fs::read_to_string(&path)
.expect("Rust source should be readable")
.lines()
.count();
(line_count > HARD_LINE_LIMIT).then(|| {
format!(
"{}: {line_count} lines",
path.strip_prefix(env!("CARGO_MANIFEST_DIR"))
.expect("source path should be inside repository")
.display()
)
})
})
.collect();
assert!(
violations.is_empty(),
"Rust source files exceed the {HARD_LINE_LIMIT}-line hard limit:\n{}",
violations.join("\n")
);
}