build(xtask): arch gate rejects Mutex in library crate production code
9dcdc04da04abb9db03dae285a2af48b427f653e
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 82deb3931722
There are many coding harnesses - but this one is fast
9dcdc04da04abb9db03dae285a2af48b427f653e
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+56 / −0 text lines
+56 / −0 text lines
/// Crates under the "no Mutex in library crates" law; binaries are exempt.const LIBRARY_CRATES: &[&str] = &[ "smith", "smith-core", "smith-ai", "smith-ui", "smith-tui", "smith-rpc", "smith-harness", "smith-alloc",];/// The first `.rs` file under `crate_dir/src` whose production slice/// (everything before `#[cfg(test)]`) mentions `Mutex`.fn mutex_in_production(crate_dir: &Path) -> Option<PathBuf> { let mut pending = vec![crate_dir.join("src")]; while let Some(dir) = pending.pop() { let Ok(entries) = fs::read_dir(&dir) else { continue; }; for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { pending.push(path); } else if path.extension().is_some_and(|ext| ext == "rs") && fs::read_to_string(&path) .is_ok_and(|source| production_slice(&source).contains("Mutex")) { return Some(path); } } } None}/// Source text before the first `#[cfg(test)]`; tests may lock freely.fn production_slice(source: &str) -> &str { source.split("#[cfg(test)]").next().unwrap_or(source)} #[test] fn production_slice_stops_at_the_test_module() { let source = "use std::sync::Arc;\n#[cfg(test)]\nmod tests { use std::sync::Mutex; }"; assert!(!super::production_slice(source).contains("Mutex")); assert!(super::production_slice("static M: Mutex<u8>;").contains("Mutex")); }