Skip to main content

Module modgraph

Module modgraph 

Source
Expand description

The intra-crate module dependency graph, built heuristically from use statements — the lean, pure-Rust alternative to booting a semantic engine (the same honesty class as crate::outline). It is the single-crate analogue of crate::deps: it produces a [deps::Graph] whose nodes are modules (crate-relative paths like domain::entity) and whose edges are use dependencies, so the very same [deps::forbid_path] / [deps::cycles] / [deps::layer_violations] assertions apply at module granularity.

§What it sees, and what it does not

Nodes come from the source files walked (one module per file; lib.rs / main.rs / mod.rs map to their containing module). Edges come from use crate::…, use self::…, and use super::… statements — brace groups are expanded, as aliases stripped, globs and self segments folded to the enclosing module, and each path resolved to the longest known module prefix. External imports (use std::…, use serde::…) are ignored.

Heuristic, not a parser: each use is read from the start of its line (how rustfmt always writes them); a use reaching through a pub use re-export resolves to the re-exporting module (not the origin); use inside macros or #[cfg(...)]-disabled code, fully-qualified paths written inline without a use, and the nesting of inline mod foo { … } blocks are not modelled — the latter fold into their file’s module. When in doubt the graph under-reports rather than inventing edges.

Functions§

build_graph
Build the module-use Graph from (module_name, file_contents) pairs. Nodes are the module names; an edge A -> B means a file in module A has a use resolving into module B. Self-edges are dropped.
check
Run a mods built-in check: walk the crate source under root/--base, build the module-use graph, and assert against it. Returns the probe outcome, a one-line reason, and the violation report. Spec / walk errors are ProbeOutcome::Broken.
check_grammar
The mods check’s introspected grammar (see [crate::deps::grammar]).
module_name
The crate-relative module path for a source file, given its path relative to the crate source root. The crate root file (lib.rs / main.rs) and any mod.rs map to their containing module; every other file adds its stem.
use_targets
The intra-crate use targets in a source file: each returned string is a brace-expanded, alias-stripped path beginning with crate, self, or super (external imports are dropped).