cargo-rail 0.28.1

Cargo-native Rust workspace engine for affected CI, Rust source-surface analysis, compiler reuse, dependency coherence, exact-SHA releases, and split/sync
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! Sparse configuration field inventory.
//!
//! Defaults live with their typed configuration fields. This module records why
//! a field exists; it is deliberately not a second defaults registry.

use std::fmt;

/// One concrete TOML path whose keys remain distinct from TOML separators.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct ConfigPath {
    segments: Vec<String>,
}

impl ConfigPath {
    pub(crate) fn root() -> Self {
        Self { segments: Vec::new() }
    }

    pub(crate) fn from_dotted(path: &str) -> Self {
        Self {
            segments: path.split('.').map(str::to_string).collect(),
        }
    }

    pub(crate) fn child(&self, segment: impl Into<String>) -> Self {
        let mut segments = self.segments.clone();
        segments.push(segment.into());
        Self { segments }
    }

    pub(crate) fn segments(&self) -> &[String] {
        &self.segments
    }

    pub(crate) fn is_root(&self) -> bool {
        self.segments.is_empty()
    }
}

impl fmt::Display for ConfigPath {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (index, segment) in self.segments.iter().enumerate() {
            if index > 0 {
                formatter.write_str(".")?;
            }
            if !segment.is_empty()
                && segment
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
            {
                formatter.write_str(segment)?;
            } else {
                let quoted = serde_json::to_string(segment).map_err(|_| fmt::Error)?;
                formatter.write_str(&quoted)?;
            }
        }
        Ok(())
    }
}

/// One leaf in the public configuration schema.
#[derive(Debug, Clone, Copy)]
pub struct FieldSpec {
    /// Dotted TOML path. `<name>` and `<index>` match dynamic map/array keys.
    pub path: &'static str,
    /// Why changing the value changes observable behavior.
    pub why: &'static str,
    /// Built-in work that consumes this exact effective field.
    pub consumers: &'static [&'static str],
}

const fn policy(path: &'static str, why: &'static str) -> FieldSpec {
    FieldSpec {
        path,
        why,
        consumers: &[],
    }
}

const fn subscribed_policy(path: &'static str, why: &'static str, consumers: &'static [&'static str]) -> FieldSpec {
    FieldSpec { path, why, consumers }
}

/// Complete leaf-field inventory for `rail.toml`.
///
/// Dynamic tables use `<name>` and arrays of tables use `<index>`. Adding a
/// deserializable field requires adding it here or a schema coverage test fails.
pub const FIELD_SPECS: &[FieldSpec] = &[
    policy(
        "targets",
        "Selects additional Cargo target-resolution views; it does not define a compiler/linker execution matrix.",
    ),
    policy(
        "unify.compiler_targets",
        "Selects the exact resolution-target subset where Unify acquires compiler diagnostics.",
    ),
    policy(
        "unify.include_paths",
        "Chooses whether path dependencies participate in unification.",
    ),
    policy(
        "unify.include_renamed",
        "Chooses whether renamed dependency declarations participate in unification.",
    ),
    policy(
        "unify.transitive_pinning.host",
        "Enables host-owned pins for fragmented transitive features and selects the owning manifest.",
    ),
    policy("unify.exclude", "Excludes named dependency cohorts from unification."),
    policy("unify.include", "Force-includes named dependencies in unification."),
    policy(
        "unify.max_backups",
        "Bounds retained recovery copies after manifest mutation.",
    ),
    policy(
        "unify.compiler_artifact_soft_limit_bytes",
        "Reports storage pressure when the command-owned compiler working set reaches this many bytes.",
    ),
    policy(
        "unify.compiler_artifact_hard_limit_bytes",
        "Stops compiler acquisition before its command-owned artifact working set can grow without bound.",
    ),
    policy(
        "unify.msrv_policy.mode",
        "Enables or disables workspace MSRV computation.",
    ),
    policy(
        "unify.msrv_policy.source",
        "Selects which authoritative inputs determine computed workspace MSRV.",
    ),
    policy(
        "unify.msrv_policy.inherit",
        "Chooses whether members inherit the computed workspace rust-version.",
    ),
    policy(
        "unify.consumer_scope",
        "Declares whether the workspace is the complete consumer universe for destructive pruning.",
    ),
    policy(
        "unify.preserve_features",
        "Names features that repository policy keeps even when dormant.",
    ),
    policy(
        "unify.strict_version_compat",
        "Chooses whether incompatible dependency requirements block unification.",
    ),
    policy(
        "unify.exact_pin_handling",
        "Chooses how exact dependency pins are preserved or rejected.",
    ),
    policy(
        "unify.major_version_conflict",
        "Chooses whether major-version conflicts remain split or are explicitly bumped.",
    ),
    policy(
        "unify.skip_undeclared_patterns",
        "Declares borrowed-feature names that are intentionally non-actionable.",
    ),
    policy(
        "release.source",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.require_changelog_entries",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.require_release_notes",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.release_notes_dir",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.unconventional_commits",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.require_change_files",
        "Selects release input or reviewed presentation policy.",
    ),
    policy(
        "release.changelog.entry_format",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.emoji",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.group_order",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.fallback",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.groups",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.groups.<index>.types",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.groups.<index>.title",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.groups.<index>.emoji",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.filters.skip_types",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.filters.skip_scopes",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.filters.include_paths",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.filters.exclude_paths",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.commit_url",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "release.changelog.pr_url",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.entry_format",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.emoji",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.group_order",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.fallback",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.groups",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.groups.<index>.types",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.groups.<index>.title",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.groups.<index>.emoji",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.filters.skip_types",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.filters.skip_scopes",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.filters.include_paths",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.filters.exclude_paths",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.commit_url",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy(
        "crates.<name>.changelog.pr_url",
        "Controls changelog presentation and commit attribution without granting publication authority.",
    ),
    policy("release.tag_prefix", "Defines the repository's release tag prefix."),
    policy(
        "release.tag_format",
        "Defines the repository's crate/version tag namespace.",
    ),
    policy(
        "release.remote_effects",
        "Selects one valid remote-effect boundary: none, push, auto, GitHub, or GitLab.",
    ),
    policy(
        "release.registry_publication",
        "Selects an exact package-registry publication boundary independently from Git and forge effects.",
    ),
    policy("release.sign_tags", "Requires cryptographic signing of release tags."),
    policy(
        "release.change_dir",
        "Selects the repository directory containing reviewed release intent.",
    ),
    policy(
        "release.pre_1_breaking_bump",
        "Defines how reviewed breaking intent maps onto pre-1.0 versions.",
    ),
    subscribed_policy(
        "release.semver_check",
        "Selects how cargo-semver-checks evidence gates a release.",
        &["release.semver"],
    ),
    policy(
        "release.version_groups",
        "Defines named crate sets that version and release in lockstep.",
    ),
    policy(
        "release.hosted_workflow",
        "Selects the GitHub workflow that executes durable release requests.",
    ),
    policy(
        "release.aliases",
        "Authorizes explicit mutable tag promotion after immutable release verification.",
    ),
    policy(
        "release.aliases.<name>",
        "Names the mutable tag for one selected package.",
    ),
    policy(
        "release.validation",
        "Binds release authorization to exact GitHub workflows and required jobs.",
    ),
    policy(
        "release.validation.<name>",
        "Names the jobs that must succeed in this workflow's release-commit run.",
    ),
    policy(
        "release.artifacts",
        "Binds complete native release inventories to authorized producers.",
    ),
    policy(
        "release.artifacts.<name>.workflow",
        "Identifies the validated workflow producing this package's assets.",
    ),
    policy(
        "release.artifacts.<name>.files",
        "Declares the complete flat inventory published with the package.",
    ),
    policy(
        "release.artifacts.<name>.files.<name>.target",
        "Records the target asserted by the product's packaging validation.",
    ),
    policy(
        "release.artifacts.<name>.files.<name>.source",
        "Requires asset bytes to match this regular file at the release commit.",
    ),
    policy(
        "release.version_groups.<name>",
        "Defines crates that version and release in lockstep.",
    ),
    policy(
        "release.auxiliary_cargo_manifests",
        "Names standalone Cargo manifests whose committed lockfiles are exact release projections.",
    ),
    policy("release.changelog.path", "Defines the default changelog path."),
    policy(
        "release.changelog.relative_to",
        "Defines the root used to resolve changelog paths.",
    ),
    policy(
        "surface.enabled",
        "Chooses whether the planner selects source-surface analysis for relevant repository changes.",
    ),
    policy(
        "surface.consumer_scope",
        "Declares whether reviewed external-consumer evidence proves the workspace is the complete consumer universe for internal source visibility.",
    ),
    policy(
        "surface.targets",
        "Selects Surface compiler environments explicitly or multiplies every automatic view across the host and top-level resolution targets.",
    ),
    policy(
        "surface.crate_visibility",
        "Chooses whether pub(crate) to pub(super) findings are enabled.",
    ),
    policy(
        "surface.preserve_uniform_fields",
        "Chooses whether visibility repair preserves uniform field visibility within a declaration.",
    ),
    policy(
        "surface.product",
        "Defines the complete set of shipped production roots.",
    ),
    policy(
        "surface.product.<index>.package",
        "Selects the exact workspace package that owns a production root.",
    ),
    policy(
        "surface.product.<index>.bin",
        "Selects one shipped Cargo binary target as a production root.",
    ),
    policy(
        "surface.product.<index>.lib",
        "Selects one shipped Cargo library target as a public production root.",
    ),
    policy(
        "surface.product.<index>.target",
        "Restricts a production root to one Cargo target selector.",
    ),
    policy(
        "surface.product.<index>.reason",
        "Records why the selected target belongs to the complete production root set.",
    ),
    policy("surface.lint", "Defines ordered workspace-wide surface lint levels."),
    policy(
        "surface.lint.<index>.selector",
        "Selects one exact surface lint or the warnings group.",
    ),
    policy(
        "surface.lint.<index>.level",
        "Sets the ordered disposition for the selected lint group.",
    ),
    policy(
        "surface.feature-profile",
        "Defines the exact Cargo feature views included in source-surface analysis.",
    ),
    policy(
        "surface.feature-profile.<index>.name",
        "Names one stable source-surface feature view.",
    ),
    policy(
        "surface.feature-profile.<index>.all-features",
        "Selects every declared Cargo feature for one source-surface view.",
    ),
    policy(
        "surface.feature-profile.<index>.no-default-features",
        "Disables Cargo default features for one source-surface view.",
    ),
    policy(
        "surface.feature-profile.<index>.features",
        "Selects exact Cargo features for one source-surface view.",
    ),
    policy(
        "surface.doctest",
        "Defines the exact workspace packages whose doctests are compiled.",
    ),
    policy(
        "surface.doctest.<index>.package",
        "Selects one doctest-enabled workspace package.",
    ),
    policy(
        "surface.doctest_coverage",
        "Chooses automatic or disabled doctest coverage when no exact package list exists.",
    ),
    policy(
        "surface.external",
        "Keeps explicitly external compiler crates outside closed-world authority.",
    ),
    policy(
        "surface.external.<index>.crate",
        "Selects one exact Rust compiler crate with external consumers.",
    ),
    policy(
        "surface.external.<index>.reason",
        "Records why the compiler crate remains open to external consumers.",
    ),
    policy("surface.override", "Defines item-specific surface diagnostic policy."),
    policy(
        "surface.override.<index>.lint",
        "Selects the exact surface lint to override.",
    ),
    policy(
        "surface.override.<index>.package",
        "Selects the exact workspace package owning the overridden declaration.",
    ),
    policy(
        "surface.override.<index>.crate",
        "Selects the exact Rust compiler crate owning the overridden declaration.",
    ),
    policy(
        "surface.override.<index>.item",
        "Selects the declaration by its compiler diagnostic path.",
    ),
    policy(
        "surface.override.<index>.kind",
        "Disambiguates the selected declaration by its exact Rust item kind.",
    ),
    policy(
        "surface.override.<index>.target",
        "Restricts an item override to one Cargo target selector.",
    ),
    policy(
        "surface.override.<index>.level",
        "Keeps, suppresses, or expects the selected surface finding.",
    ),
    policy(
        "surface.override.<index>.reason",
        "Records why the item-specific surface policy exists.",
    ),
    policy(
        "surface.exclude",
        "Excludes one module or source file from surface diagnostics.",
    ),
    policy(
        "surface.exclude.<index>.package",
        "Selects the exact workspace package owning the excluded scope.",
    ),
    policy(
        "surface.exclude.<index>.crate",
        "Selects the exact Rust compiler crate owning the excluded scope.",
    ),
    policy(
        "surface.exclude.<index>.module",
        "Selects an excluded compiler diagnostic module path.",
    ),
    policy(
        "surface.exclude.<index>.file",
        "Selects an excluded repository-relative source file.",
    ),
    policy(
        "surface.exclude.<index>.target",
        "Restricts an excluded scope to one Cargo target selector.",
    ),
    policy(
        "surface.exclude.<index>.level",
        "Suppresses or expects findings in the excluded scope.",
    ),
    policy(
        "surface.exclude.<index>.reason",
        "Records why the excluded source scope exists.",
    ),
    policy("plan.work", "Declares input-only repository work."),
    policy(
        "plan.work.<name>.scope",
        "Selects the typed scope emitted for declared work.",
    ),
    policy(
        "plan.work.<name>.paths",
        "Declares positive repository-relative path inputs.",
    ),
    policy(
        "plan.work.<name>.config",
        "Declares exact effective configuration inputs.",
    ),
    policy(
        "plan.work.<name>.cargo",
        "Subscribes declared work to code-owned Cargo work.",
    ),
    policy(
        "plan.work.<name>.variant_catalog",
        "Selects a checked-in declarative variant catalog.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites",
        "Declares bounded one-hop Cargo artifact prerequisites for this named work item.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.source_work",
        "Names the code-owned Cargo execution work that activates this prerequisite edge.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.when",
        "Selects source packages or exact targets that activate this prerequisite edge.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.when.<index>.package",
        "Names an exact source workspace package.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.when.<index>.target",
        "Selects an optional exact source Cargo target.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.when.<index>.target.name",
        "Names the exact source Cargo target.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.when.<index>.target.kind",
        "Names one exact source Cargo target kind.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.require",
        "Selects prerequisite packages or exact targets emitted by this named work item.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.require.<index>.package",
        "Names an exact prerequisite workspace package.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.require.<index>.target",
        "Selects an optional exact prerequisite Cargo target.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.require.<index>.target.name",
        "Names the exact prerequisite Cargo target.",
    ),
    policy(
        "plan.work.<name>.cargo_prerequisites.<index>.require.<index>.target.kind",
        "Names one exact prerequisite Cargo target kind.",
    ),
    policy("crates", "Defines per-crate policy overrides."),
    policy(
        "crates.<name>.split.remote",
        "Selects the split repository for an owned crate.",
    ),
    policy(
        "crates.<name>.split.branch",
        "Selects the destination branch for split/sync history.",
    ),
    policy(
        "crates.<name>.split.mode",
        "Selects single-crate or combined-repository splitting.",
    ),
    policy(
        "crates.<name>.split.workspace_mode",
        "Selects combined split repository layout.",
    ),
    policy(
        "crates.<name>.split.members",
        "Names Cargo workspace members whose snapshot-derived roots are owned by a split.",
    ),
    policy(
        "crates.<name>.split.include",
        "Declares explicit non-Cargo assets owned by a split.",
    ),
    policy(
        "crates.<name>.split.exclude",
        "Excludes paths from an otherwise owned split tree.",
    ),
    policy(
        "crates.<name>.release.publish",
        "Overrides whether a crate participates in registry publication.",
    ),
    policy("crates.<name>.changelog.path", "Overrides a crate's changelog path."),
    policy(
        "crates.<name>.changelog.relative_to",
        "Overrides a crate's changelog path root.",
    ),
    policy(
        "crates.<name>.changelog.skip",
        "Excludes a crate from changelog generation.",
    ),
];

/// Look up metadata for an exact or dynamic dotted path.
pub fn field_spec(path: &str) -> Option<&'static FieldSpec> {
    field_spec_path(&ConfigPath::from_dotted(path))
}

pub(crate) fn field_spec_path(path: &ConfigPath) -> Option<&'static FieldSpec> {
    FIELD_SPECS.iter().find(|spec| path_matches(spec.path, path))
}

/// Return built-in work consumers for one exact effective field.
pub fn field_consumers(path: &str) -> &'static [&'static str] {
    const CARGO_TARGET_CONSUMERS: &[&str] = &[
        "cargo.build",
        "cargo.clippy",
        "cargo.doc",
        "cargo.doctest",
        "cargo.package",
        "cargo.test",
        "surface",
    ];
    const SURFACE_CONSUMERS: &[&str] = &["surface"];
    let Some(spec) = field_spec(path) else {
        return &[];
    };
    if !spec.consumers.is_empty() {
        spec.consumers
    } else if spec.path == "targets" {
        CARGO_TARGET_CONSUMERS
    } else if spec.path.starts_with("surface.") {
        SURFACE_CONSUMERS
    } else {
        &[]
    }
}

pub(crate) fn is_known_config_path(path: &ConfigPath) -> bool {
    field_spec_path(path).is_some() || FIELD_SPECS.iter().any(|spec| path_prefix_matches(spec.path, path))
}

fn path_matches(pattern: &str, path: &ConfigPath) -> bool {
    let pattern = pattern.split('.');
    pattern.clone().count() == path.segments().len()
        && pattern
            .zip(path.segments())
            .all(|(expected, actual)| expected.starts_with('<') || expected == actual)
}

fn path_prefix_matches(pattern: &str, path: &ConfigPath) -> bool {
    let pattern = pattern.split('.');
    path.segments().len() < pattern.clone().count()
        && path
            .segments()
            .iter()
            .zip(pattern)
            .all(|(actual, expected)| expected.starts_with('<') || actual == expected)
}

pub(crate) fn document_paths(doc: &toml_edit::DocumentMut) -> Vec<ConfigPath> {
    let mut paths = Vec::new();
    collect_table_like_paths(doc.as_table(), &ConfigPath::root(), &mut paths);
    paths.sort_unstable();
    paths.dedup();
    paths
}

fn collect_table_like_paths(table: &dyn toml_edit::TableLike, prefix: &ConfigPath, paths: &mut Vec<ConfigPath>) {
    for (key, item) in table.iter() {
        let path = prefix.child(key);
        paths.push(path.clone());

        if let Some(child) = item.as_table_like() {
            collect_table_like_paths(child, &path, paths);
        } else if let Some(array) = item.as_array_of_tables() {
            for (index, child) in array.iter().enumerate() {
                collect_table_like_paths(child, &path.child(index.to_string()), paths);
            }
        } else if let Some(array) = item.as_array() {
            for (index, value) in array.iter().enumerate() {
                if let Some(child) = value.as_inline_table() {
                    collect_table_like_paths(child, &path.child(index.to_string()), paths);
                }
            }
        }
    }
}

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

    #[test]
    fn dynamic_paths_match_schema_inventory() {
        assert_eq!(
            field_spec("crates.demo.split.remote").map(|field| field.path),
            Some("crates.<name>.split.remote")
        );
    }

    #[test]
    fn structural_paths_preserve_dynamic_keys() {
        let doc: toml_edit::DocumentMut = r#"
[plan.work."docs.generated"]
paths = ["docs/**"]

[crates."cli-tools".release]
publish = false
"#
        .parse()
        .unwrap();
        let paths = document_paths(&doc);
        assert!(
            paths
                .iter()
                .any(|path| path.to_string() == "plan.work.\"docs.generated\".paths")
        );
        assert!(
            paths
                .iter()
                .any(|path| path.to_string() == "crates.cli-tools.release.publish")
        );
        assert!(paths.iter().all(is_known_config_path));
    }

    #[test]
    fn structural_paths_descend_inline_tables_and_inline_table_arrays() {
        let doc: toml_edit::DocumentMut = r#"
release = { changelog = { path = "CHANGELOG.md", removed = true } }
surface = { product = [{ package = "cargo-rail", bin = "cargo-rail", removed = true }] }
"#
        .parse()
        .unwrap();
        let paths = document_paths(&doc)
            .into_iter()
            .map(|path| path.to_string())
            .collect::<Vec<_>>();

        assert!(paths.iter().any(|path| path == "release.changelog.path"));
        assert!(paths.iter().any(|path| path == "release.changelog.removed"));
        assert!(paths.iter().any(|path| path == "surface.product.0.package"));
        assert!(paths.iter().any(|path| path == "surface.product.0.removed"));
    }

    #[test]
    fn field_consumers_are_owned_by_the_schema_inventory() {
        assert_eq!(field_consumers("release.semver_check"), &["release.semver"]);
        assert_eq!(field_consumers("surface.enabled"), &["surface"]);
        assert!(field_consumers("release.tag_format").is_empty());
    }

    #[test]
    fn inventory_paths_are_unique() {
        let mut paths: Vec<_> = FIELD_SPECS.iter().map(|field| field.path).collect();
        paths.sort_unstable();
        let count = paths.len();
        paths.dedup();
        assert_eq!(paths.len(), count);
    }

    #[test]
    fn every_typed_default_leaf_is_classified() {
        fn visit(value: &serde_json::Value, path: &str, missing: &mut Vec<String>) {
            match value {
                serde_json::Value::Object(object) if !object.is_empty() => {
                    for (key, value) in object {
                        let child = if path.is_empty() {
                            key.clone()
                        } else {
                            format!("{path}.{key}")
                        };
                        visit(value, &child, missing);
                    }
                }
                _ if !path.is_empty() && field_spec(path).is_none() => missing.push(path.to_string()),
                _ => {}
            }
        }

        let defaults = serde_json::to_value(crate::config::RailConfig::default()).expect("serialize defaults");
        let mut missing = Vec::new();
        visit(&defaults, "", &mut missing);
        assert!(missing.is_empty(), "unclassified configuration fields: {missing:?}");
    }
}