guibiao 0.9.0

圭表 (Guibiao) — Tianheng's dependency-light static observation core (derived from modou). Declare crate, module, and dependency-source boundaries in Rust — dependencies by name, module imports, and each dependency's declared source kind (git/registry/path) for manifest hygiene — observed from cargo metadata and source use-scans, reacted in CI. No AST: the syn-free gnomon of the family.
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use super::*;
use serde_json::Value;

use crate::module_scan::{Edition, package_name_to_import_ident};

pub(crate) use xingbiao::{
    cargo_metadata, compilation_unit_label, crate_roots, find_package, member_src_dirs,
};

/// What the targets rooted at one file ask of reading it: the edition they are read in, and whether one is a
/// proc-macro crate, whose extern prelude holds `proc_macro` without an `extern crate` — measured against rustc 1.96.0,
/// edition 2021, a `[lib] proc-macro = true` crate calls `proc_macro::TokenStream::new()` with none.
pub(crate) struct RootReading {
    /// The one edition the root is read in; a mix of 2018 and later reads as 2018, and a mix with 2015 is refused.
    pub(crate) edition: Edition,
    /// Whether any target rooted at the file is a proc-macro crate, so `proc_macro` resolves with no `extern crate`.
    pub(crate) proc_macro: bool,
}

/// The [`RootReading`] of the targets rooted at `root_file`, or of every target of the package where the roots are not
/// reported. rustc compiles each target in its own edition, and `cargo metadata` reports a target's `[lib]` or
/// `[[bin]]` `edition` on the target and the package's own beside it — measured under cargo 1.96.0, a 2024 package with
/// `[lib] edition = "2015"` reports `2015` on its library target and `2024` on the package, and builds a 2015 crate. A
/// target reporting no edition reads as the package's. Targets sharing the root in editions the scanner reads apart are
/// compiled twice, once in each, so one reading cannot judge both and the root is refused; 2018 beside 2021, whose
/// paths it reads alike, are one reading, in 2018: the two differ only in whether a `c` before a string opens a C
/// string, and the 2018 reading lexes as code what the 2021 one could read as a string's contents, so it misses
/// nothing the other reads.
pub(crate) fn root_reading(
    package: &Value,
    root_file: Option<&Path>,
    crate_package: &str,
) -> Result<RootReading, String> {
    let package_edition = package["edition"].as_str();
    let targets: Vec<&Value> = package["targets"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(|target| {
            root_file.is_none_or(|root| target["src_path"].as_str().map(Path::new) == Some(root))
        })
        .collect();
    let proc_macro = targets.iter().any(|target| {
        target["kind"]
            .as_array()
            .is_some_and(|kinds| kinds.iter().any(|kind| kind.as_str() == Some("proc-macro")))
    });
    let edition = match root_file {
        None => Edition::of(package_edition),
        Some(root) => {
            let mut written: Vec<Option<&str>> = targets
                .iter()
                .map(|target| target["edition"].as_str().or(package_edition))
                .collect();
            written.sort();
            written.dedup();
            let readings: Vec<Edition> = written
                .iter()
                .map(|edition| Edition::of(*edition))
                .collect();
            let paths_alike = |reading: &Edition| reading != &Edition::Rust2015;
            match readings.first() {
                None => Edition::of(package_edition),
                Some(first) if readings.iter().all(|reading| reading == first) => *first,
                Some(_) if readings.iter().all(paths_alike) => Edition::Rust2018,
                Some(_) => {
                    return Err(crate::errors::root_in_several_editions_error(
                        crate_package,
                        root,
                        &written.iter().map(|e| e.unwrap_or("?")).collect::<Vec<_>>(),
                    ));
                }
            }
        }
    };
    Ok(RootReading {
        edition,
        proc_macro,
    })
}

/// The membership set, or why it could not be read.
///
/// **Typed apart, because an empty answer had several causes and reported one.** A `packages` array
/// that is absent or is not an array, a package whose `name` this reader cannot take, and a workspace
/// that genuinely has no member all produced the same empty `Vec` — and both consumers read empty as
/// *nothing to govern*. Coverage rendered `total = 0, uncovered = []` as complete coverage over a
/// membership it never read.
///
/// The sibling rule is stated in this crate already, on
/// [`crate::workspace_member_src_dirs`]: *an unreadable workspace is a constitution error, never a
/// silent empty set*. This reader is the same question about the same metadata and did not follow it.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Members {
    /// The workspace's member names, sorted and deduplicated. Empty means the workspace declares none.
    Read(Vec<String>),
    /// The membership could not be read, and saying so is not the same fact as reading none.
    Unreadable(String),
}

/// The names of the workspace's member crates. Because Modou runs
/// `cargo metadata --no-deps`, the `packages` array contains exactly the workspace
/// members (no transitive dependencies), so their names are the membership set used
/// by the workspace-scoped rule and by coverage. A `path` dependency that points
/// outside the workspace is therefore absent here, as intended.
///
/// A package whose `name` is absent or is not a string **refuses** rather than being dropped: a
/// member this reader could not name is one it did not compare, which is not a member that is absent.
pub(crate) fn workspace_member_names(metadata: &Value) -> Members {
    let Some(packages) = metadata["packages"].as_array() else {
        return Members::Unreadable(
            "cargo metadata carries no `packages` array, so which crates are workspace members \
             cannot be decided"
                .to_string(),
        );
    };
    let mut names: Vec<String> = Vec::with_capacity(packages.len());
    for package in packages {
        match package["name"].as_str() {
            Some(name) => names.push(name.to_string()),
            None => {
                return Members::Unreadable(
                    "cargo metadata carries a package whose `name` is absent or is not a string, \
                     so the membership set this reader would compare against is incomplete"
                        .to_string(),
                );
            }
        }
    }
    names.sort();
    names.dedup();
    Members::Read(names)
}

/// Whether a `cargo metadata` dependency belongs to the selected table. `kind` is
/// null for normal deps, `"dev"` / `"build"` otherwise.
fn kind_matches(dependency: &Value, kind: DependencyKind) -> bool {
    matches!(
        (kind, dependency["kind"].as_str()),
        (DependencyKind::Normal, None)
            | (DependencyKind::Dev, Some("dev"))
            | (DependencyKind::Build, Some("build"))
    )
}

/// Every dependency-table edge of the target's declared `kind`, optionally excluding the
/// target's own self-referential edge (see [`is_self_dependency`]) — the shared filter every
/// consuming rule ([`external_dependencies`] / [`dependencies`] /
/// [`dependencies_with_disallowed_source`]) needs, so kind-matching and self-dependency
/// exclusion cannot silently diverge across them — the shared-filter reason
/// [`is_self_dependency`]'s own doc states. [`external_dependencies`] passes
/// `exclude_self: false`: its own `!source.is_null()` filter already excludes a self-dependency
/// (always a null-source `path` edge), so an extra explicit exclusion there would be redundant,
/// not a divergence.
fn governed_dependencies(
    package: &Value,
    kind: DependencyKind,
    exclude_self: bool,
) -> impl Iterator<Item = &Value> {
    package["dependencies"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(move |dependency| {
            kind_matches(dependency, kind)
                && !(exclude_self && is_self_dependency(package, dependency))
        })
}

/// Names of the target's dependencies in the selected table that resolve to a registry
/// or git source (any non-null source). Path/internal dependencies, and dependencies in other tables, are
/// excluded.
///
/// Names are package names, not local renames (`foo = { package = "bar" }` is
/// reported as `bar`), and platform-specific (`[target.'cfg(…)'.dependencies]`) and
/// `optional` deps are included — a declared dependency is governed as declared
/// (PROJECT.md).
pub(crate) fn external_dependencies(package: &Value, kind: DependencyKind) -> Vec<String> {
    let mut found: Vec<String> = governed_dependencies(package, kind, false)
        .filter(|dependency| !dependency["source"].is_null())
        .filter_map(|dependency| dependency["name"].as_str().map(str::to_string))
        .collect();
    found.sort();
    found.dedup();
    found
}

/// Whether `dependency` is the package's OWN self-referential edge — a null-`source` **path**
/// dependency on itself (e.g. a doctest/dogfooding `[dev-dependencies]` entry like
/// `main = { path = "." }`). `intent/crate-dependency-boundary.md` exempts only a null-source self-edge;
/// a same-named registry or git dependency remains governed, so `source.is_null()` must gate the name match.
/// Filtering here, at the shared
/// observation source, is what every consuming rule ([`dependencies`] /
/// [`dependencies_with_disallowed_source`]) relies on — a per-rule copy left the identical false
/// positive live in every sibling rule.
fn is_self_dependency(package: &Value, dependency: &Value) -> bool {
    let own_name = package["name"].as_str();
    own_name.is_some() && dependency["name"].as_str() == own_name && dependency["source"].is_null()
}

/// Names of the target's dependencies in the selected table, regardless of source —
/// internal workspace path dependencies included. Used by the forbid and restrict-to
/// rules, which (unlike the external rule) must see internal crate-to-crate
/// dependencies. Same conventions as [`external_dependencies`]: package names (not
/// local renames), and platform-specific / `optional` deps are included (PROJECT.md).
/// Never includes the target's own self-referential edge (see [`is_self_dependency`]).
pub(crate) fn dependencies(package: &Value, kind: DependencyKind) -> Vec<String> {
    let mut found: Vec<String> = governed_dependencies(package, kind, true)
        .filter_map(|dependency| dependency["name"].as_str().map(str::to_string))
        .collect();
    found.sort();
    found.dedup();
    found
}

/// The **import identifiers** a crate's declared dependencies are written under in source: each
/// dependency's `rename` when present (a Cargo `pkg = { package = "…" }` / `dep = { package = "…" }`
/// rename), else its package `name`, normalized `-`→`_` to the Rust path spelling (`async-trait` →
/// `async_trait`). This is the vocabulary the inline confinement's `strict_external` modifier
/// matches a fully-qualified path head against.
///
/// 圭表-own (三儀 ⊥ 三儀 — see the module preamble): a small parallel of
/// `hunyi::crate_scope::dependency_names`, **not** a dependency on 渾儀, reading only the
/// `package["dependencies"]` value 圭表 already obtains via 星表 (so no new crate dependency). Unlike
/// [`dependencies`]/[`external_dependencies`] (which read `name` only), it is rename-aware and
/// `-`→`_`-folded, matching the source spelling.
///
/// **Deliberately unfiltered by kind or source** (unlike [`dependencies`]/[`external_dependencies`]):
/// dev-, build-, and path dependencies are all included. A broader name set makes MORE heads resolve
/// as external, never fewer — the fail-safe direction for the one forbidden bug (a false negative) —
/// while the scope lookup still keeps any genuinely-local item local. The only cost is a
/// possible reaction on a dev/build-dep name used inside scanned test code.
pub(crate) fn dependency_import_names(package: &Value) -> Vec<String> {
    let mut names: Vec<String> = package["dependencies"]
        .as_array()
        .map(|deps| {
            deps.iter()
                .filter_map(|dep| {
                    dep["rename"]
                        .as_str()
                        .or_else(|| dep["name"].as_str())
                        .map(package_name_to_import_ident)
                })
                .collect()
        })
        .unwrap_or_default();
    names.sort();
    names.dedup();
    names
}

/// The import identifiers of the package's own library targets — what a binary root of the same package
/// names that library by. A target's `name` is its crate name, `[lib] name` when one is declared, so it is
/// read rather than derived from the package name.
pub(crate) fn library_import_names(package: &Value) -> Vec<String> {
    const LIBRARY_KINDS: [&str; 6] = ["lib", "rlib", "dylib", "cdylib", "staticlib", "proc-macro"];
    package["targets"]
        .as_array()
        .map(|targets| {
            targets
                .iter()
                .filter(|target| {
                    target["kind"].as_array().is_some_and(|kinds| {
                        kinds
                            .iter()
                            .any(|kind| kind.as_str().is_some_and(|k| LIBRARY_KINDS.contains(&k)))
                    })
                })
                .filter_map(|target| target["name"].as_str().map(package_name_to_import_ident))
                .collect()
        })
        .unwrap_or_default()
}

/// Classify a dependency's **declared** source kind from its `cargo metadata` (`--no-deps`)
/// `source` field: null → `Path`, `git+`-prefixed → `Git`, any other non-null → `Registry` (the
/// residual, covering `registry+`/`sparse+`/alternative registries). Verified
/// against a probe manifest: a `git = "…"` dependency reads `git+…` even with a `version` key,
/// `optional = true`, or a workspace-inherited (`{ workspace = true }`) source.
fn classify_source(dependency: &Value) -> SourceKind {
    match dependency["source"].as_str() {
        None => SourceKind::Path,
        Some(source) if source.starts_with("git+") => SourceKind::Git,
        Some(_) => SourceKind::Registry,
    }
}

/// The **real package names** (not local renames) and declared source kinds of the target's
/// dependencies in the selected table whose classified [`SourceKind`] is not in `allowed` — the
/// observation for [`Rule::RestrictDependencySourcesTo`]. Every declared edge in the selected
/// table is observed, including optional edges. Never includes the target's own self-referential edge (see
/// [`is_self_dependency`]) — its declared source (always `Path`, a null `source`) is otherwise
/// indistinguishable from a genuine internal dependency.
pub(crate) fn dependencies_with_disallowed_source(
    package: &Value,
    kind: DependencyKind,
    allowed: &[SourceKind],
) -> Vec<(String, SourceKind)> {
    let mut found: Vec<(String, SourceKind)> = governed_dependencies(package, kind, true)
        .filter_map(|dependency| {
            let source = classify_source(dependency);
            if allowed.contains(&source) {
                return None;
            }
            dependency["name"]
                .as_str()
                .map(|name| (name.to_string(), source))
        })
        .collect();
    found.sort_by(|(left_name, left_source), (right_name, right_source)| {
        left_name
            .cmp(right_name)
            .then_with(|| left_source.label().cmp(right_source.label()))
    });
    found.dedup();
    found
}

/// The **declared feature request** the target authors on a dependency `crate_name` in the
/// selected table — the union across every matching edge of its `features = [...]` list plus the
/// pseudo-feature `default` when any such edge leaves default features enabled. Transitive and
/// sibling-crate enables are not attributed to the target. Matches `crate_name` by package
/// name, not a local rename, like [`dependencies`]/[`external_dependencies`]. The target's own
/// self-referential edge (see [`is_self_dependency`]) is never matched either, if `crate_name`
/// happens to name the target's own package — a self-dependency's feature request is not a
/// cross-crate concern this rule governs.
pub(crate) fn declared_features(
    package: &Value,
    crate_name: &str,
    kind: DependencyKind,
) -> Vec<String> {
    let mut found = Vec::new();
    for dependency in matching_dependency_edges(package, crate_name, kind) {
        found.extend(dependency_feature_request(dependency));
    }
    found.sort();
    found.dedup();
    found
}

/// Every dependency-table edge on `crate_name` of the requested `kind` — matched by resolved
/// package name, never the local `rename`/alias — excluding the target's own self-dependency edge
/// (see [`is_self_dependency`]).
fn matching_dependency_edges<'a>(
    package: &'a Value,
    crate_name: &str,
    kind: DependencyKind,
) -> impl Iterator<Item = &'a Value> {
    governed_dependencies(package, kind, true)
        .filter(move |dependency| dependency["name"].as_str() == Some(crate_name))
}

/// The declared feature request for one dependency edge: its explicit `features = [...]` list
/// plus the pseudo-feature `default` when the edge leaves default features enabled. Cargo's edge
/// carries `uses_default_features`; an absent field means defaults are on. Representing "the
/// target requests this dependency's default set" as the pseudo-feature `default` lets one rule
/// shape govern both explicit features and the default toggle (`forbid default` ≡ "require
/// default-features = false").
fn dependency_feature_request(dependency: &Value) -> Vec<String> {
    let mut requested: Vec<String> = dependency["features"]
        .as_array()
        .into_iter()
        .flatten()
        .filter_map(Value::as_str)
        .map(str::to_string)
        .collect();
    if dependency["uses_default_features"].as_bool() != Some(false) {
        requested.push("default".to_string());
    }
    requested
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    /// Classifies declared source field values against standard cargo metadata shapes,
    /// treating null or absent source as Path.
    #[test]
    fn classify_source_reads_the_declared_source_field() {
        assert_eq!(
            classify_source(&json!({ "name": "localdep", "source": null })),
            SourceKind::Path,
            "a null source is a path/internal dependency",
        );
        assert_eq!(
            classify_source(&json!({ "name": "gitdep", "source": "git+https://example.com/x" })),
            SourceKind::Git,
            "a git+ source is git",
        );
        assert_eq!(
            classify_source(&json!({
                "name": "crates_io",
                "source": "registry+https://github.com/rust-lang/crates.io-index"
            })),
            SourceKind::Registry,
            "a registry+ source is registry",
        );
        assert_eq!(
            classify_source(
                &json!({ "name": "alt", "source": "sparse+https://my.registry/index/" })
            ),
            SourceKind::Registry,
            "a sparse+ alternative registry is the residual Registry, not misread as git/path",
        );
        assert_eq!(
            classify_source(&json!({ "name": "no_source_key" })),
            SourceKind::Path,
        );
    }
}