use std::path::{Path, PathBuf};
use walkdir::WalkDir;
const FORBIDDEN_TOKENS: &[&str] = &[
"kreuzberg",
"kreuzcrawl",
"html_to_markdown",
"html-to-markdown",
"tree_sitter_language_pack",
"tree-sitter-language-pack",
"liter_llm",
"liter-llm",
"spikard",
];
const SCAN_ROOTS: &[&str] = &[
"crates/alef-codegen/src",
"crates/alef-extract/src",
"crates/alef-cli/src",
];
const KNOWN_ALLOWED_VIOLATIONS: &[(&str, &str)] = &[];
#[derive(Debug)]
struct Violation {
file: PathBuf,
line: usize,
token: &'static str,
content: String,
}
#[test]
fn no_project_name_special_casing_in_production_code() {
let workspace_root = workspace_root();
let mut violations: Vec<Violation> = Vec::new();
for relative_root in SCAN_ROOTS {
let root = workspace_root.join(relative_root);
assert!(root.is_dir(), "scan root does not exist: {}", root.display());
scan_directory(&root, &mut violations);
}
if !violations.is_empty() {
let report = format_report(&workspace_root, &violations);
panic!("Found project-name special-casing:\n{report}");
}
}
fn scan_directory(root: &Path, violations: &mut Vec<Violation>) {
for entry in WalkDir::new(root).into_iter().filter_map(Result::ok) {
let path = entry.path();
if !is_scannable_rust_file(path) {
continue;
}
let Ok(content) = std::fs::read_to_string(path) else {
continue;
};
scan_file(path, &content, violations);
}
}
fn is_scannable_rust_file(path: &Path) -> bool {
if !path.is_file() {
return false;
}
if path.extension().and_then(|ext| ext.to_str()) != Some("rs") {
return false;
}
if path.file_name().and_then(|n| n.to_str()) == Some("tests.rs") {
return false;
}
true
}
fn scan_file(path: &Path, content: &str, violations: &mut Vec<Violation>) {
let mut state = ScanState::new();
for (idx, raw_line) in content.lines().enumerate() {
let line_number = idx + 1;
let in_test_before = state.in_test_block();
state.observe_line(raw_line);
if in_test_before || state.in_test_block() {
continue;
}
let trimmed = raw_line.trim_start();
if trimmed.starts_with("//") {
continue;
}
for token in FORBIDDEN_TOKENS {
if raw_line.contains(token) {
if is_grandfathered(path, raw_line) {
continue;
}
violations.push(Violation {
file: path.to_path_buf(),
line: line_number,
token,
content: raw_line.to_string(),
});
}
}
}
}
struct ScanState {
pending_cfg_test: bool,
brace_depth: i64,
test_block_starts: Vec<i64>,
}
impl ScanState {
fn new() -> Self {
Self {
pending_cfg_test: false,
brace_depth: 0,
test_block_starts: Vec::new(),
}
}
fn in_test_block(&self) -> bool {
!self.test_block_starts.is_empty()
}
fn observe_line(&mut self, raw_line: &str) {
let trimmed = raw_line.trim();
if is_cfg_test_attribute(trimmed) {
self.pending_cfg_test = true;
}
let opens = count_unescaped(raw_line, '{');
let closes = count_unescaped(raw_line, '}');
if self.pending_cfg_test && opens > 0 {
self.test_block_starts.push(self.brace_depth);
self.pending_cfg_test = false;
} else if self.pending_cfg_test
&& !trimmed.is_empty()
&& !trimmed.starts_with("//")
&& !trimmed.starts_with('#')
{
self.pending_cfg_test = false;
}
self.brace_depth += opens as i64;
self.brace_depth -= closes as i64;
if self.brace_depth < 0 {
self.brace_depth = 0;
}
while let Some(&start_depth) = self.test_block_starts.last() {
if self.brace_depth <= start_depth {
self.test_block_starts.pop();
} else {
break;
}
}
}
}
fn is_grandfathered(path: &Path, raw_line: &str) -> bool {
let workspace_root = workspace_root();
let Ok(rel) = path.strip_prefix(&workspace_root) else {
return false;
};
let rel_str = rel.to_string_lossy().replace('\\', "/");
for (allowed_path, allowed_substring) in KNOWN_ALLOWED_VIOLATIONS {
if rel_str == *allowed_path && raw_line.contains(allowed_substring) {
return true;
}
}
false
}
fn is_cfg_test_attribute(trimmed: &str) -> bool {
if trimmed.starts_with("#[cfg(test)") {
return true;
}
if trimmed.starts_with("#[cfg(all(test") || trimmed.starts_with("#[cfg(any(test") {
return true;
}
if trimmed.starts_with("#[test]") {
return true;
}
false
}
fn count_unescaped(line: &str, target: char) -> usize {
line.chars().filter(|c| *c == target).count()
}
fn workspace_root() -> PathBuf {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
manifest_dir
.parent()
.and_then(Path::parent)
.expect("workspace root must be two levels above alef-cli manifest")
.to_path_buf()
}
fn format_report(workspace_root: &Path, violations: &[Violation]) -> String {
let mut out = String::new();
for v in violations {
let rel = v.file.strip_prefix(workspace_root).unwrap_or(&v.file).display();
out.push_str(&format!(
" {}:{}: forbidden token `{}` in: {}\n",
rel,
v.line,
v.token,
v.content.trim()
));
}
out.push_str(&format!(
"\n{} violation(s) found. Alef must remain project-agnostic — drive any\n",
violations.len()
));
out.push_str("downstream-specific behavior through `alef.toml` configuration, not\n");
out.push_str("hard-coded references in codegen, extract, or CLI source.\n");
out
}