fabrilis/tools/limits/src/rules/layers.rs
loki5512344 878d144b77 Initial commit: Fabrilis docs, rules and deterministic core
Docs (architecture, decisions, coupling, determinism, UI, references), code rules enforced by tools/limits, deterministic core with units, time, math, sched, snapshot, vec and rng, plus smoke tests. ref/ stays out of git.
2026-11-16 23:06:29 +01:00

134 lines
3.1 KiB
Rust

//! Enforces the crate layering from docs/ARCHITECTURE.md: dependencies point down
//! only, and simulation crates never depend on GUI crates.
use std::fs;
use std::path::Path;
use crate::walk;
/// Crate name without the `fabrilis-` prefix and its layer. A crate may depend only on
/// crates of a strictly lower layer. Add every new crate here.
const LAYERS: &[(&str, u8)] = &[
("core", 0),
("platform", 1),
("storage", 1),
("materials", 1),
("physics", 1),
("aero", 1),
("actuators", 1),
("analog", 1),
("cpu", 1),
("worldgen", 1),
("scripting", 1),
("sensors", 1),
("analysis", 1),
("render", 1),
("scene", 2),
("structure", 2),
("thermal", 2),
("soc", 2),
("ui", 2),
("parts", 3),
("wiring", 3),
("bridge", 3),
("modes", 4),
("app", 5),
];
/// Only these crates (and each other) may depend on GUI crates.
const GUI: [&str; 3] = ["render", "ui", "app"];
/// Tooling crates sit outside the layering.
const TOOLS: [&str; 1] = ["limits"];
struct Manifest {
name: String,
dependencies: Vec<String>,
}
fn layer_of(name: &str) -> Option<u8> {
LAYERS
.iter()
.find(|(known, _)| *known == name)
.map(|&(_, layer)| layer)
}
fn parse(text: &str) -> Option<Manifest> {
let mut section = "";
let mut name = None;
let mut dependencies = Vec::new();
for line in text.lines().map(str::trim) {
if line.starts_with('[') {
section = line;
continue;
}
match section {
"[package]" if line.starts_with("name") => {
name = line.split('"').nth(1).map(str::to_string);
}
"[dependencies]" => {
if let Some(rest) = line.strip_prefix("fabrilis-") {
let dep = rest.split([' ', '=']).next().unwrap_or("");
dependencies.push(dep.to_string());
}
}
_ => {}
}
}
Some(Manifest {
name: name?,
dependencies,
})
}
fn check_manifest(shown: &str, name: &str, dependencies: &[String]) -> Vec<String> {
let Some(layer) = layer_of(name) else {
return vec![format!(
"{shown}: crate `{name}` is not in the layer table (tools/limits/src/rules/layers.rs)"
)];
};
let mut errors = Vec::new();
for dep in dependencies {
let Some(dep_layer) = layer_of(dep) else {
errors.push(format!(
"{shown}: dependency `{dep}` is not in the layer table"
));
continue;
};
if dep_layer >= layer {
errors.push(format!(
"{shown}: `{name}` (layer {layer}) must not depend on `{dep}` (layer {dep_layer}); dependencies point down only"
));
}
if GUI.contains(&dep.as_str()) && !GUI.contains(&name) {
errors.push(format!(
"{shown}: simulation crate `{name}` must not depend on GUI crate `{dep}`"
));
}
}
errors
}
/// Layering violations in every `fabrilis-*` manifest under `root`.
pub fn check(root: &Path) -> Vec<String> {
let mut errors = Vec::new();
for path in walk::manifests(root) {
let shown = path
.strip_prefix(root)
.unwrap_or(&path)
.display()
.to_string();
let Ok(text) = fs::read_to_string(&path) else {
continue;
};
let Some(manifest) = parse(&text) else {
continue;
};
let Some(name) = manifest.name.strip_prefix("fabrilis-") else {
continue;
};
if !TOOLS.contains(&name) {
errors.extend(check_manifest(&shown, name, &manifest.dependencies));
}
}
errors
}