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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use super::constitution::*;
use crate::cargo_metadata::*;
use serde_json::Value;
use xuanji::{Polarity, RuleKey, Severity};

/// A boundary attached to one crate target, with a human-readable reason.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateBoundary {
    /// The crate governed, by package name.
    pub(crate) target: CrateTarget,
    /// What the boundary forbids or restricts; its key and label identify every violation it emits.
    pub(crate) rule: Rule,
    /// The declared intent the boundary protects, carried on every violation as the repair direction.
    pub(crate) reason: String,
    /// Whether a violation fails the reaction (`Enforce`) or is only reported (`Warn`).
    pub(crate) severity: Severity,
    /// The dependency table the rule observes.
    pub(crate) kind: DependencyKind,
    /// A durable governance pointer, distinct from `reason`; `None` unless `with_anchor` sets one.
    pub(crate) anchor: Option<String>,
}

impl CrateBoundary {
    /// Begin a crate boundary for the crate named `package`.
    pub fn crate_(package: &str) -> CrateBoundaryBuilder {
        CrateBoundaryBuilder {
            target: CrateTarget {
                package: package.to_string(),
            },
        }
    }

    /// The crate this boundary governs.
    pub fn target(&self) -> &CrateTarget {
        &self.target
    }

    /// The rule the boundary enforces.
    pub fn rule(&self) -> &Rule {
        &self.rule
    }

    /// The human-readable reason recorded with the boundary (the repair hint).
    pub fn reason(&self) -> &str {
        &self.reason
    }

    /// The boundary's severity (`enforce` or `warn`).
    pub fn severity(&self) -> Severity {
        self.severity
    }

    /// The dependency table this boundary observes (`Normal` by default).
    pub fn dependency_kind(&self) -> DependencyKind {
        self.kind
    }

    /// Attach a durable governance anchor (e.g. `"ADR-014"`) — a stable pointer into the
    /// project's governance, distinct from the free-text `reason`. Optional; a boundary with
    /// none projects and reacts exactly as before. Chained after [`because`](CrateBoundaryDraft::because).
    pub fn with_anchor(mut self, anchor: &str) -> Self {
        self.anchor = Some(anchor.to_string());
        self
    }

    /// The durable governance anchor recorded with the boundary, if any.
    pub fn anchor(&self) -> Option<&str> {
        self.anchor.as_deref()
    }
}

/// A crate identified by its package name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateTarget {
    /// The crate's package name, as it appears in `cargo metadata`.
    pub package: String,
}

impl CrateTarget {
    /// Create a new crate target for the given package name.
    pub fn new(package: impl Into<String>) -> Self {
        Self {
            package: package.into(),
        }
    }

    /// Access the crate's package name as a string slice.
    pub fn as_str(&self) -> &str {
        &self.package
    }
}

impl AsRef<str> for CrateTarget {
    fn as_ref(&self) -> &str {
        &self.package
    }
}

impl std::fmt::Display for CrateTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.package)
    }
}

/// What a crate boundary forbids. Each variant is a reaction with an observation
/// source in `cargo metadata`; no variant is named for a reaction that does not
/// exist.
///
/// Rules are constructed through [`CrateBoundary::crate_`], not variant struct expressions. A
/// consumer inspecting a rule can match known fields forward-compatibly:
///
/// ```
/// use guibiao::{CrateBoundary, Rule};
///
/// let boundary = CrateBoundary::crate_("core")
///     .forbid_dependency_on(["serde"])
///     .because("core owns no serialization vocabulary");
/// match boundary.rule() {
///     Rule::ForbidDependencyOn { crates, .. } => assert_eq!(crates, &["serde"]),
///     _ => unreachable!(),
/// }
/// ```
///
/// ```compile_fail
/// use guibiao::Rule;
///
/// let _ = Rule::ForbidDependencyOn { crates: vec!["serde".to_string()] };
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Rule {
    /// Deny external (registry/git) dependencies, except any named in `allowed`.
    #[non_exhaustive]
    DenyExternalDependencies {
        /// External crate names permitted despite the deny rule.
        allowed: Vec<String>,
    },
    /// Forbid a normal dependency on any of these crates (external or internal).
    #[non_exhaustive]
    ForbidDependencyOn {
        /// The forbidden crate names.
        crates: Vec<String>,
    },
    /// Restrict normal dependencies to a closed allowlist: any normal dependency
    /// (external or internal) whose name is not in `allowed` is a violation. An
    /// empty allowlist forbids every normal dependency.
    #[non_exhaustive]
    RestrictDependenciesTo {
        /// The closed allowlist of permitted dependency names.
        allowed: Vec<String>,
    },
    /// Restrict the target's dependencies on *other workspace members* to a closed
    /// allowlist: any normal dependency on a workspace member not in `allowed` is a
    /// violation, while external dependencies are ignored. Workspace membership is
    /// observed from `cargo metadata`, so a newly added member is governed by default.
    /// An empty allowlist forbids every workspace dependency.
    #[non_exhaustive]
    RestrictWorkspaceDependenciesTo {
        /// The closed allowlist of permitted workspace-member names.
        allowed: Vec<String>,
    },
    /// Restrict the **declared source kinds** of the target's dependencies to a closed
    /// allowlist: any dependency whose classified [`SourceKind`] (from its `cargo
    /// metadata` declared `source`) is not in `allowed` is a violation. The source-kind
    /// counterpart of [`RestrictDependenciesTo`](Rule::RestrictDependenciesTo) (which
    /// governs dependency *names*). An empty allowlist forbids every dependency by
    /// source. Governs the *declared* source, not the resolved one — a `[patch]`/
    /// `replace-with` redirect is not observed (the resolved layer is cargo-deny's
    /// `[sources]` lane, not a Tianheng capability).
    #[non_exhaustive]
    RestrictDependencySourcesTo {
        /// The closed allowlist of permitted declared source kinds.
        allowed: Vec<SourceKind>,
    },
    /// Restrict the **declared features** the target requests on a named dependency
    /// `crate_` to a closed allowlist: any feature in the target's declared set for
    /// `crate_` (its authored `features = [...]`, ∪ the `default` pseudo-feature when
    /// default features are left on) whose name is not in `allowed` is a violation. The
    /// feature-granularity counterpart of
    /// [`RestrictDependenciesTo`](Rule::RestrictDependenciesTo) (which governs dependency
    /// *names*). An empty allowlist forbids the target from declaring **any** feature of
    /// `crate_`, `default` included (i.e. requires `default-features = false` and no
    /// explicit features). Governs the *declared* request, not the resolved/unified
    /// feature set — a feature that `crate_`'s own `[features]` graph or a sibling crate's
    /// unification enables transitively is not chased (declared-not-resolved).
    #[non_exhaustive]
    RestrictFeaturesOf {
        /// The dependency whose declared features are governed (matched by package name).
        crate_: String,
        /// The closed allowlist of permitted feature names (`default` is the pseudo-feature
        /// for default features).
        allowed: Vec<String>,
    },
    /// Forbid the target from declaring specific named features of a dependency `crate_`:
    /// any feature in the target's declared set for `crate_` matching a `forbidden` name is
    /// a violation; a forbidden feature the target does not declare is not. The
    /// feature-granularity counterpart of
    /// [`ForbidDependencyOn`](Rule::ForbidDependencyOn). Forbidding the `default`
    /// pseudo-feature is the way to require `default-features = false`. An empty forbidden
    /// set is a no-op that always reports clean (symmetric with forbidding a crate the
    /// target does not depend on). Governs the *declared* request, not the resolved/unified
    /// feature set (transitive enables are not chased).
    #[non_exhaustive]
    ForbidFeaturesOf {
        /// The dependency whose declared features are governed (matched by package name).
        crate_: String,
        /// The forbidden feature names (`default` is the pseudo-feature for default features).
        forbidden: Vec<String>,
    },
}

impl Rule {
    /// Stable semantic identity for this declared crate rule.
    pub fn key(&self) -> RuleKey {
        match self {
            Rule::DenyExternalDependencies { allowed } => RuleKey::of(
                "tianheng.rule/guibiao/deny-external-dependencies",
                [("allowed", canonical_set(allowed))],
            ),
            Rule::ForbidDependencyOn { crates } => RuleKey::of(
                "tianheng.rule/guibiao/forbid-dependency-on",
                [("crates", canonical_set(crates))],
            ),
            Rule::RestrictDependenciesTo { allowed } => RuleKey::of(
                "tianheng.rule/guibiao/restrict-dependencies-to",
                [("allowed", canonical_set(allowed))],
            ),
            Rule::RestrictWorkspaceDependenciesTo { allowed } => RuleKey::of(
                "tianheng.rule/guibiao/restrict-workspace-dependencies-to",
                [("allowed", canonical_set(allowed))],
            ),
            Rule::RestrictDependencySourcesTo { allowed } => RuleKey::of(
                "tianheng.rule/guibiao/restrict-dependency-sources-to",
                [(
                    "allowed",
                    canonical_set(allowed.iter().map(SourceKind::label)),
                )],
            ),
            Rule::RestrictFeaturesOf { crate_, allowed } => RuleKey::of(
                "tianheng.rule/guibiao/restrict-features-of",
                [
                    ("allowed", canonical_set(allowed)),
                    ("crate", crate_.clone()),
                ],
            ),
            Rule::ForbidFeaturesOf { crate_, forbidden } => RuleKey::of(
                "tianheng.rule/guibiao/forbid-features-of",
                [
                    ("crate", crate_.clone()),
                    ("forbidden", canonical_set(forbidden)),
                ],
            ),
        }
    }

    /// Each crate rule is the single source of truth for its own behavior: its
    /// label, text and JSON projection, and which declared dependencies it flags
    /// (including its observation source). Every method is one exhaustive match, so
    /// adding a variant is a compile error until it is handled everywhere
    /// (see PROJECT.md). The label feeds human violation/projection text; [`Rule::key`]
    /// separately carries semantic identity so wording remains free to evolve.
    pub(crate) fn label(&self) -> &'static str {
        match self {
            Rule::DenyExternalDependencies { .. } => "deny external dependencies",
            Rule::ForbidDependencyOn { .. } => "forbid dependency on",
            Rule::RestrictDependenciesTo { .. } => "restrict dependencies to",
            Rule::RestrictWorkspaceDependenciesTo { .. } => "restrict workspace dependencies to",
            Rule::RestrictDependencySourcesTo { .. } => "restrict dependency sources to",
            Rule::RestrictFeaturesOf { .. } => "restrict features of",
            Rule::ForbidFeaturesOf { .. } => "forbid features of",
        }
    }

    /// The repair-direction [`Polarity`] of a violation of this rule. `ForbidDependencyOn` names
    /// specific forbidden crates (repair: remove) → `DenyBreach`; the rest permit a set and react
    /// to a member outside it (repair: remove or declare) → `AllowlistGap`. `DenyExternalDependencies`
    /// is `AllowlistGap` **by repair direction, not name**: its `allow_external` exceptions are an
    /// in-boundary declaration path, so a new external dep is either removed or excepted.
    pub(crate) fn polarity(&self) -> Polarity {
        match self {
            Rule::ForbidDependencyOn { .. } | Rule::ForbidFeaturesOf { .. } => Polarity::DenyBreach,
            Rule::DenyExternalDependencies { .. }
            | Rule::RestrictDependenciesTo { .. }
            | Rule::RestrictWorkspaceDependenciesTo { .. }
            | Rule::RestrictDependencySourcesTo { .. }
            | Rule::RestrictFeaturesOf { .. } => Polarity::AllowlistGap,
        }
    }

    /// The human-readable rule text with its parameters, for the text projection.
    pub(crate) fn text(&self) -> String {
        match self {
            Rule::DenyExternalDependencies { allowed } if allowed.is_empty() => {
                "deny external dependencies".to_string()
            }
            Rule::DenyExternalDependencies { allowed } => {
                format!("deny external dependencies (allow: {})", allowed.join(", "))
            }
            Rule::ForbidDependencyOn { crates } => {
                format!("forbid dependency on: {}", crates.join(", "))
            }
            Rule::RestrictDependenciesTo { allowed } if allowed.is_empty() => {
                "restrict dependencies to nothing".to_string()
            }
            Rule::RestrictDependenciesTo { allowed } => {
                format!("restrict dependencies to: {}", allowed.join(", "))
            }
            Rule::RestrictWorkspaceDependenciesTo { allowed } if allowed.is_empty() => {
                "forbid all workspace dependencies".to_string()
            }
            Rule::RestrictWorkspaceDependenciesTo { allowed } => {
                format!("restrict workspace dependencies to: {}", allowed.join(", "))
            }
            Rule::RestrictDependencySourcesTo { allowed } if allowed.is_empty() => {
                "forbid all dependencies (by source)".to_string()
            }
            Rule::RestrictDependencySourcesTo { allowed } => {
                format!(
                    "restrict dependency sources to: {}",
                    allowed
                        .iter()
                        .map(SourceKind::label)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
            Rule::RestrictFeaturesOf { crate_, allowed } if allowed.is_empty() => {
                format!("restrict features of {crate_} to nothing")
            }
            Rule::RestrictFeaturesOf { crate_, allowed } => {
                format!("restrict features of {crate_} to: {}", allowed.join(", "))
            }
            Rule::ForbidFeaturesOf { crate_, forbidden } if forbidden.is_empty() => {
                format!("forbid no features of {crate_}")
            }
            Rule::ForbidFeaturesOf { crate_, forbidden } => {
                format!("forbid features of {crate_}: {}", forbidden.join(", "))
            }
        }
    }

    /// The JSON parameter fields for the projection. Deny-external's `allowed` is an
    /// optional exception list (emitted only when non-empty); restrict-to's `only` is
    /// the intrinsic closed set (always emitted, as `[]` when empty); forbid lists
    /// its `crates`; the workspace-scoped restrict-to uses `only_workspace`, distinct
    /// from `only` so the projection says which dependency surface it governs.
    pub(crate) fn json_params(&self) -> Vec<(&'static str, Value)> {
        match self {
            Rule::DenyExternalDependencies { allowed } if allowed.is_empty() => Vec::new(),
            Rule::DenyExternalDependencies { allowed } => {
                vec![("allowed", serde_json::json!(allowed))]
            }
            Rule::ForbidDependencyOn { crates } => vec![("crates", serde_json::json!(crates))],
            Rule::RestrictDependenciesTo { allowed } => vec![("only", serde_json::json!(allowed))],
            Rule::RestrictWorkspaceDependenciesTo { allowed } => {
                vec![("only_workspace", serde_json::json!(allowed))]
            }
            Rule::RestrictDependencySourcesTo { allowed } => {
                let sources: Vec<&str> = allowed.iter().map(SourceKind::label).collect();
                vec![("allowed_sources", serde_json::json!(sources))]
            }
            Rule::RestrictFeaturesOf { crate_, allowed } => vec![
                ("crate", serde_json::json!(crate_)),
                ("only_features", serde_json::json!(allowed)),
            ],
            Rule::ForbidFeaturesOf { crate_, forbidden } => vec![
                ("crate", serde_json::json!(crate_)),
                ("forbidden_features", serde_json::json!(forbidden)),
            ],
        }
    }

    /// The target's declared dependencies that violate this rule. Each rule owns both
    /// its observation source (external-only / all normal / workspace-only) and its
    /// filter. `workspace_members` is all workspace member names, observed from
    /// `cargo metadata`; only the workspace-scoped rule consults it — and excludes the
    /// TARGET's own name from that set (see the workspace-scoped arm below): Cargo genuinely
    /// permits a crate declaring itself as a `[dev-dependencies]` path dependency on itself
    /// (a common doctest/dogfooding pattern, `main = { path = "." }`), which `cargo metadata
    /// --no-deps` emits verbatim — a real edge, not a parse artifact. A self-dependency is
    /// never an inter-crate layering violation (there is no OTHER crate to leak across a
    /// boundary to), so it must never be governed as one (calling this case "harmless" while
    /// `workspace_members` still included the target's own name
    /// unfiltered, which is what actually made it flag).
    #[cfg(test)]
    pub(crate) fn findings(
        &self,
        package: &Value,
        workspace_members: &[String],
        kind: DependencyKind,
    ) -> Vec<String> {
        self.facts(package, workspace_members, kind)
            .into_iter()
            .map(|fact| fact.into_finding().text().to_string())
            .collect()
    }

    /// The target's offences against this rule in its `kind` table, as typed facts each carrying `kind`, so one
    /// dependency offending in two tables is two identities.
    pub(crate) fn facts(
        &self,
        package: &Value,
        workspace_members: &[String],
        kind: DependencyKind,
    ) -> Vec<crate::finding::CrateFact> {
        let dependencies: Vec<String> = match self {
            Rule::DenyExternalDependencies { allowed } => external_dependencies(package, kind)
                .into_iter()
                .filter(|dependency| !allowed.contains(dependency))
                .collect(),
            Rule::ForbidDependencyOn { crates } => dependencies(package, kind)
                .into_iter()
                .filter(|dependency| crates.contains(dependency))
                .collect(),
            Rule::RestrictDependenciesTo { allowed } => dependencies(package, kind)
                .into_iter()
                .filter(|dependency| !allowed.contains(dependency))
                .collect(),
            Rule::RestrictWorkspaceDependenciesTo { allowed } => dependencies(package, kind)
                .into_iter()
                .filter(|dependency| {
                    workspace_members.contains(dependency) && !allowed.contains(dependency)
                })
                .collect(),
            Rule::RestrictDependencySourcesTo { allowed } => {
                return dependencies_with_disallowed_source(package, kind, allowed)
                    .into_iter()
                    .map(|(dependency, source)| {
                        crate::finding::CrateFact::source(dependency, source, kind)
                    })
                    .collect();
            }
            Rule::RestrictFeaturesOf { crate_, allowed } => {
                return declared_features(package, crate_, kind)
                    .into_iter()
                    .filter(|feature| !allowed.contains(feature))
                    .map(|feature| {
                        crate::finding::CrateFact::feature(crate_.clone(), feature, kind)
                    })
                    .collect();
            }
            Rule::ForbidFeaturesOf { crate_, forbidden } => {
                return declared_features(package, crate_, kind)
                    .into_iter()
                    .filter(|feature| forbidden.contains(feature))
                    .map(|feature| {
                        crate::finding::CrateFact::feature(crate_.clone(), feature, kind)
                    })
                    .collect();
            }
        };
        dependencies
            .into_iter()
            .map(|dependency| crate::finding::CrateFact::dependency(dependency, kind))
            .collect()
    }
}

/// Fluent builder: `CrateBoundary::crate_("x").deny_external_dependencies().because("…")`
/// or `CrateBoundary::crate_("x").forbid_dependency_on(["y"]).because("…")`.
pub struct CrateBoundaryBuilder {
    /// The crate the finished boundary governs.
    target: CrateTarget,
}

impl CrateBoundaryBuilder {
    /// Deny external dependencies. Chain [`DenyExternalDraft::allow_external`] to
    /// name exceptions, and [`DenyExternalDraft::warn`] to make it advisory, before
    /// [`DenyExternalDraft::because`].
    pub fn deny_external_dependencies(self) -> DenyExternalDraft {
        DenyExternalDraft {
            target: self.target,
            allowed: Vec::new(),
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Forbid a normal dependency on any of `crates`, whether it resolves to an
    /// external source or to an internal workspace path (crate-to-crate layering).
    pub fn forbid_dependency_on<I, S>(self, crates: I) -> CrateBoundaryDraft
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::ForbidDependencyOn {
                crates: crates.into_iter().map(Into::into).collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Restrict this crate's normal dependencies to a closed allowlist: any normal
    /// dependency (external or internal) not named in `allowed` is a violation. An
    /// empty allowlist forbids every normal dependency.
    pub fn restrict_dependencies_to<I, S>(self, allowed: I) -> CrateBoundaryDraft
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::RestrictDependenciesTo {
                allowed: allowed.into_iter().map(Into::into).collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Restrict this crate's dependencies on *other workspace members* to a closed
    /// allowlist: any normal dependency on a workspace member not named in `allowed`
    /// is a violation; external dependencies are ignored. Workspace members are
    /// derived from `cargo metadata`, so a newly added member is governed by default.
    /// Unlike [`restrict_dependencies_to`](Self::restrict_dependencies_to), which
    /// governs *all* normal dependencies (external included), this governs only the
    /// workspace surface.
    pub fn restrict_workspace_dependencies_to<I, S>(self, allowed: I) -> CrateBoundaryDraft
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::RestrictWorkspaceDependenciesTo {
                allowed: allowed.into_iter().map(Into::into).collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Forbid this crate from depending on *any* other workspace member — the
    /// empty-allowlist shorthand for
    /// [`restrict_workspace_dependencies_to`](Self::restrict_workspace_dependencies_to).
    pub fn forbid_all_workspace_dependencies(self) -> CrateBoundaryDraft {
        self.restrict_workspace_dependencies_to(Vec::<String>::new())
    }

    /// Restrict the **declared source kinds** of this crate's dependencies to a closed
    /// allowlist: any dependency whose classified [`SourceKind`] is not in `allowed` is
    /// a violation (a publishable infra crate declares `[Registry, Path]` to forbid a
    /// `git` source; a workspace tool may declare the opposite). An empty allowlist
    /// forbids every dependency by source. Chain [`warn`](CrateBoundaryDraft::warn),
    /// [`dependency_kind`](CrateBoundaryDraft::dependency_kind), and
    /// [`because`](CrateBoundaryDraft::because) as with the other crate rules.
    ///
    /// Two stated bounds (deliberate, not silent):
    /// - It governs the **declared** source, not the *resolved* one. A registry
    ///   dependency redirected to git/path by `[patch]` or `[source] replace-with`
    ///   reads as `Registry` (no violation) — correct for manifest hygiene, since
    ///   `[patch]` is workspace-local and never blocks `cargo publish`. Observing the
    ///   resolved source is cargo-deny's `[sources]` lane, not a Tianheng capability.
    /// - It is source-kind **hygiene**, not a `cargo publish` oracle. A
    ///   `{ git = "…", version = "…" }` (or `{ path = "…", version = "…" }`) dependency
    ///   declares a non-registry source and is flagged even though it would publish
    ///   successfully; the rule does not parse the `version` key.
    pub fn restrict_dependency_sources_to<I>(self, allowed: I) -> CrateBoundaryDraft
    where
        I: IntoIterator<Item = SourceKind>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::RestrictDependencySourcesTo {
                allowed: allowed.into_iter().collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Restrict the **declared features** this crate requests on dependency `crate_` to a
    /// closed allowlist: any feature in the target's declared set for `crate_` (its authored
    /// `features = [...]`, ∪ the `default` pseudo-feature when default features are left on)
    /// not named in `allowed` is a violation. An empty allowlist forbids declaring **any**
    /// feature of `crate_`, `default` included (i.e. requires `default-features = false`).
    /// The feature-granularity mirror of
    /// [`restrict_dependencies_to`](Self::restrict_dependencies_to). `crate_` is matched by
    /// package name, not a local `rename`/alias. Chain [`warn`](CrateBoundaryDraft::warn),
    /// [`dependency_kind`](CrateBoundaryDraft::dependency_kind), and
    /// [`because`](CrateBoundaryDraft::because) as with the other crate rules.
    pub fn restrict_features_of<C, I, S>(self, crate_: C, allowed: I) -> CrateBoundaryDraft
    where
        C: Into<String>,
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::RestrictFeaturesOf {
                crate_: crate_.into(),
                allowed: allowed.into_iter().map(Into::into).collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Forbid this crate from declaring specific named `forbidden` features of dependency
    /// `crate_`: any feature in the target's declared set for `crate_` matching a forbidden
    /// name is a violation; a forbidden feature the target does not declare is not. Forbidding
    /// the `default` pseudo-feature requires `default-features = false`. An empty forbidden
    /// set is a no-op that always reports clean. The feature-granularity mirror of
    /// [`forbid_dependency_on`](Self::forbid_dependency_on). `crate_` is matched by package
    /// name, not a local `rename`/alias. Chain [`warn`](CrateBoundaryDraft::warn),
    /// [`dependency_kind`](CrateBoundaryDraft::dependency_kind), and
    /// [`because`](CrateBoundaryDraft::because) as with the other crate rules.
    pub fn forbid_features_of<C, I, S>(self, crate_: C, forbidden: I) -> CrateBoundaryDraft
    where
        C: Into<String>,
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        CrateBoundaryDraft {
            target: self.target,
            rule: Rule::ForbidFeaturesOf {
                crate_: crate_.into(),
                forbidden: forbidden.into_iter().map(Into::into).collect(),
            },
            severity: Severity::Enforce,
            kind: DependencyKind::Normal,
        }
    }

    /// Forbid this crate from declaring the single `feature` of dependency `crate_` — the
    /// singular convenience for [`forbid_features_of`](Self::forbid_features_of). Forbidding
    /// `"default"` requires `default-features = false`.
    pub fn forbid_feature<C, S>(self, crate_: C, feature: S) -> CrateBoundaryDraft
    where
        C: Into<String>,
        S: Into<String>,
    {
        self.forbid_features_of(crate_, [feature])
    }
}

/// A deny-external boundary awaiting an optional allowlist, severity, and reason.
pub struct DenyExternalDraft {
    /// The crate the finished boundary governs.
    target: CrateTarget,
    /// External crates exempted from the deny rule, accumulated across `allow_external` calls.
    allowed: Vec<String>,
    /// `Enforce` until `warn` makes the boundary advisory.
    severity: Severity,
    /// The dependency table the rule observes; `Normal` until `dependency_kind` changes it.
    kind: DependencyKind,
}

impl DenyExternalDraft {
    /// Allow these external dependencies as named exceptions to the deny rule.
    pub fn allow_external<I, S>(mut self, crates: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.allowed.extend(crates.into_iter().map(Into::into));
        self
    }

    /// Make this boundary advisory: its violations are reported but do not fail CI.
    pub fn warn(mut self) -> Self {
        self.severity = Severity::Warn;
        self
    }

    /// Observe a different dependency table (`Dev` or `Build`); the default is `Normal`.
    pub fn dependency_kind(mut self, kind: DependencyKind) -> Self {
        self.kind = kind;
        self
    }

    /// Finish the boundary, recording the human-readable `reason` (the repair hint).
    pub fn because(self, reason: &str) -> CrateBoundary {
        CrateBoundary {
            target: self.target,
            rule: Rule::DenyExternalDependencies {
                allowed: self.allowed,
            },
            reason: reason.to_string(),
            severity: self.severity,
            kind: self.kind,
            anchor: None,
        }
    }
}

/// A crate boundary awaiting its severity and reason.
pub struct CrateBoundaryDraft {
    /// The crate the finished boundary governs.
    target: CrateTarget,
    /// The rule the builder method chose, moved unchanged into the boundary.
    rule: Rule,
    /// `Enforce` until `warn` makes the boundary advisory.
    severity: Severity,
    /// The dependency table the rule observes; `Normal` until `dependency_kind` changes it.
    kind: DependencyKind,
}

impl CrateBoundaryDraft {
    /// Make this boundary advisory: its violations are reported but do not fail CI.
    pub fn warn(mut self) -> Self {
        self.severity = Severity::Warn;
        self
    }

    /// Observe a different dependency table (`Dev` or `Build`); the default is `Normal`.
    pub fn dependency_kind(mut self, kind: DependencyKind) -> Self {
        self.kind = kind;
        self
    }

    /// Finish the boundary, recording the human-readable `reason` (the repair hint).
    pub fn because(self, reason: &str) -> CrateBoundary {
        CrateBoundary {
            target: self.target,
            rule: self.rule,
            reason: reason.to_string(),
            severity: self.severity,
            kind: self.kind,
            anchor: None,
        }
    }
}