crawk 0.7.0

Dependency crawler for Rust. It crawls so you don't have to untangle
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//! Config-file resolution, deserialization, and validation.

use std::collections::BTreeSet;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use serde::Deserialize;
use toml::Spanned;
use tracing::warn;

use crate::error::{AnalysisError, Result};
use crate::graph::Cycle;

use super::{
    AllowedCycle, DenyRule, InitOutcome, LayerRule, ModulePattern, RestrictRule, RuleSet,
    is_parent_child_cycle,
};

/// Preferred config file name (searched first).
const CONFIG_FILE: &str = "crawk.toml";
/// Hidden config file name, searched as a fallback.
const CONFIG_FILE_HIDDEN: &str = ".crawk.toml";

/// Serde shape of the whole config file: a single `[check]` table.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConfig {
    check: RawCheck,
}

/// Serde shape of the `[check]` table.
///
/// `deny_same_layer` here is the crate-wide *default*; each `[[check.layers]]`
/// group may override it (see [`RawLayer`]).
// A deserialization shape, not an API: the bools mirror independent TOML keys,
// so folding them into enums would only distort the config surface.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Default, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
struct RawCheck {
    layers: Vec<RawLayer>,
    deny: Vec<RawDeny>,
    restrict: Vec<RawRestrict>,
    allow_cycle: Vec<RawAllowCycle>,
    strict_layers: bool,
    deny_same_layer: bool,
    deny_cycles: bool,
    deny_parent_child_cycles: bool,
}

/// Serde shape of one `[[check.allow-cycle]]` entry: a loop that predates the
/// rule and is tolerated until it is untangled.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawAllowCycle {
    modules: Vec<String>,
    #[serde(default)]
    reason: Option<String>,
}

/// Serde shape of one `[[check.deny]]` rule. Subtree matching requires an
/// explicit `::*` suffix on the pattern.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawDeny {
    from: String,
    to: String,
}

/// Serde shape of one `[[check.restrict]]` rule. Subtree matching requires an
/// explicit `::*` suffix on the pattern.
///
/// `to` has no default on purpose: an empty allowance must be spelled
/// `to = []` explicitly, so a forgotten key is a parse error instead of
/// silently becoming the strictest possible rule.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRestrict {
    from: String,
    to: Vec<String>,
}

/// Serde shape of one `[[check.layers]]` group.
///
/// `name` is [`Spanned`] so a duplicate group name can be
/// reported with the source line it occurs on. `deny_same_layer` is optional:
/// `None` inherits the `[check]`-level default.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct RawLayer {
    name: Spanned<String>,
    order: Vec<String>,
    #[serde(default)]
    deny_same_layer: Option<bool>,
}

/// Resolve which rule-config file to use.
///
/// With an explicit path, that file is used verbatim (error if missing). Without
/// one, the crate root is searched for `crawk.toml` then `.crawk.toml`; the plain
/// name wins when both exist (with a warning). Missing config is an operational
/// error so a typo'd filename fails CI rather than silently passing.
pub(crate) fn resolve_config_path(crate_root: &Path, explicit: Option<&Path>) -> Result<PathBuf> {
    if let Some(path) = explicit {
        if path.is_file() {
            return Ok(path.to_path_buf());
        }
        return Err(AnalysisError::RuleConfigError {
            path: path.to_path_buf(),
            reason: "config file does not exist".to_owned(),
        });
    }

    let plain = crate_root.join(CONFIG_FILE);
    let hidden = crate_root.join(CONFIG_FILE_HIDDEN);
    match (plain.is_file(), hidden.is_file()) {
        (true, true) => {
            warn!("both {CONFIG_FILE} and {CONFIG_FILE_HIDDEN} found, using {CONFIG_FILE}");
            Ok(plain)
        }
        (true, false) => Ok(plain),
        (false, true) => Ok(hidden),
        (false, false) => Err(AnalysisError::RuleConfigError {
            path: crate_root.to_path_buf(),
            reason: format!(
                "no {CONFIG_FILE} or {CONFIG_FILE_HIDDEN} found in {}; \
                 run `crawk check --init` to generate a starter config",
                crate_root.display()
            ),
        }),
    }
}

/// Quote and join module names into a TOML array body.
fn toml_list<'a>(names: impl Iterator<Item = &'a str>) -> String {
    names
        .map(|name| format!("\"{name}\""))
        .collect::<Vec<_>>()
        .join(", ")
}

/// The loops worth writing into a starter config, sorted for a stable file.
///
/// Containment loops are left out: `deny-cycles` never reports them, so an entry
/// for one would be noise the reader has to disprove.
fn freezable_cycles(cycles: &[Cycle]) -> Vec<&BTreeSet<String>> {
    let mut frozen: Vec<&BTreeSet<String>> = cycles
        .iter()
        .map(|cycle| &cycle.modules)
        .filter(|modules| !is_parent_child_cycle(modules))
        .collect();
    frozen.sort();
    frozen
}

/// Render a starter `crawk.toml` body from the crate's modules and cycles.
///
/// Emits a single `[[check.layers]]` group named after the crate, listing its
/// top-level modules alphabetically, with a comment telling the user to order
/// them. crawk deliberately does not guess the hierarchy — layer ordering
/// encodes intent the source cannot reveal.
///
/// Cycles are the opposite case: the source *does* reveal them, so the scaffold
/// turns `deny-cycles` on and freezes every existing loop as an
/// `[[check.allow-cycle]]` entry. The rule therefore starts green and ratchets
/// from day one instead of drowning a new adopter in pre-existing tangles.
pub(crate) fn scaffold(crate_name: &str, modules: &BTreeSet<String>, cycles: &[Cycle]) -> String {
    // Collapse to top-level segments; the BTreeSet dedups and sorts them.
    let top: BTreeSet<&str> = modules
        .iter()
        .filter_map(|m| m.split("::").next())
        .collect();
    let order = toml_list(top.into_iter());
    // The `[check]` keys must precede the first `[[check.layers]]`: in TOML,
    // bare keys after an array-of-tables header belong to that table.
    let mut out = format!(
        "# Generated by `crawk check --init`. Reorder `order` so the highest\n\
         # layer comes first: a lower layer must not depend on a higher one.\n\
         # Split into multiple groups as needed; a module may appear in several,\n\
         # each checked independently. See `crawk check\n\
         # --help` for the full `layers` semantics.\n\
         \n\
         [check]\n\
         # No dependency loops. A parent tangled with its own submodules is\n\
         # containment, not a tangle, and stays exempt unless you also set\n\
         # `deny-parent-child-cycles = true`.\n\
         deny-cycles = true\n\
         \n\
         [[check.layers]]\n\
         name = \"{crate_name}\"\n\
         order = [{order}]\n"
    );

    let frozen = freezable_cycles(cycles);
    if !frozen.is_empty() {
        out.push_str(
            "\n# Loops this crate already has, frozen so `deny-cycles` starts green.\n\
             # Delete an entry once its loop is untangled; a module joining a frozen\n\
             # loop falls outside the entry and is reported.\n",
        );
        for loop_modules in frozen {
            let list = toml_list(loop_modules.iter().map(String::as_str));
            let _ = write!(out, "\n[[check.allow-cycle]]\nmodules = [{list}]\n");
        }
    }
    out
}

/// Write a starter rule config, reporting where it landed and how many existing
/// loops it froze.
///
/// With an explicit path (`--config`/`-c`), writes there. Otherwise, writes
/// `crawk.toml` in `crate_root`. Either way it refuses (operational error) when
/// the target — or, for the default case, any discovered `crawk.toml` /
/// `.crawk.toml` — already exists, so `--init` never clobbers a real config.
///
/// # Errors
///
/// [`AnalysisError::RuleConfigError`] when a config already exists or the file
/// cannot be written.
pub(crate) fn scaffold_config(
    crate_root: &Path,
    explicit: Option<&Path>,
    crate_name: &str,
    modules: &BTreeSet<String>,
    cycles: &[Cycle],
) -> Result<InitOutcome> {
    let path = if let Some(target) = explicit {
        if target.exists() {
            return Err(AnalysisError::RuleConfigError {
                path: target.to_path_buf(),
                reason: "config already exists; edit it directly or remove it first".to_owned(),
            });
        }
        target.to_path_buf()
    } else {
        if let Ok(existing) = resolve_config_path(crate_root, None) {
            return Err(AnalysisError::RuleConfigError {
                path: existing,
                reason: "config already exists; edit it directly or remove it first".to_owned(),
            });
        }
        crate_root.join(CONFIG_FILE)
    };
    std::fs::write(&path, scaffold(crate_name, modules, cycles)).map_err(|e| {
        AnalysisError::RuleConfigError {
            path: path.clone(),
            reason: e.to_string(),
        }
    })?;
    Ok(InitOutcome {
        path,
        frozen_cycles: freezable_cycles(cycles).len(),
    })
}

/// 1-based line number of byte `offset` within `text`.
fn line_of(text: &str, offset: usize) -> usize {
    let end = offset.min(text.len());
    text.get(..end)
        .unwrap_or(text)
        .bytes()
        .filter(|&b| b == b'\n')
        .count()
        + 1
}

/// Reject duplicate `[[check.layers]]` group names, pinning the duplicate to its
/// source line via the [`Spanned`] name.
fn check_unique_names(path: &Path, text: &str, layers: &[RawLayer]) -> Result<()> {
    let mut seen = BTreeSet::new();
    for layer in layers {
        if !seen.insert(layer.name.get_ref().as_str()) {
            let line = line_of(text, layer.name.span().start);
            return Err(AnalysisError::RuleConfigError {
                path: path.to_path_buf(),
                reason: format!(
                    "duplicate layer group name '{}' (line {line})",
                    layer.name.get_ref()
                ),
            });
        }
    }
    Ok(())
}

impl RuleSet {
    /// Read, parse, and validate a rule-config file against the crate's modules.
    ///
    /// # Errors
    ///
    /// - [`AnalysisError::RuleConfigError`] — file unreadable, malformed TOML,
    ///   a duplicate or one-module `allow-cycle` entry, or (under
    ///   `strict-layers`) an uncovered module.
    /// - [`AnalysisError::UnknownRuleModule`] — a rule names a module that does
    ///   not exist in the crate.
    pub(crate) fn load(path: &Path, modules: &BTreeSet<String>) -> Result<Self> {
        let text = std::fs::read_to_string(path).map_err(|e| AnalysisError::RuleConfigError {
            path: path.to_path_buf(),
            reason: e.to_string(),
        })?;
        let raw: RawConfig = toml::from_str(&text).map_err(|e| AnalysisError::RuleConfigError {
            path: path.to_path_buf(),
            reason: e.to_string(),
        })?;
        // Group names must be unique (violations are reported per group and name
        // it). Checked here, where the spans into `text` are still available, so
        // the duplicate can be pinned to its source line.
        check_unique_names(path, &text, &raw.check.layers)?;
        let rules = Self::from_raw(raw.check);
        rules.validate(path, modules)?;
        if !rules.deny_cycles && !rules.allow_cycles.is_empty() {
            warn!("allow-cycle entries have no effect while deny-cycles is false");
        }
        Ok(rules)
    }

    /// Convert the deserialized shape into the validated in-memory form.
    fn from_raw(raw: RawCheck) -> Self {
        // The `[check]`-level flag is the default each group inherits unless it
        // sets its own `deny-same-layer`.
        let default_deny = raw.deny_same_layer;
        let layers = raw
            .layers
            .into_iter()
            .map(|raw_layer| LayerRule {
                name: raw_layer.name.into_inner(),
                order: raw_layer
                    .order
                    .iter()
                    .map(|entry| ModulePattern::parse_subtree(entry))
                    .collect(),
                deny_same_layer: raw_layer.deny_same_layer.unwrap_or(default_deny),
            })
            .collect();
        let deny = raw
            .deny
            .into_iter()
            .map(|raw_deny| DenyRule {
                from: ModulePattern::parse(&raw_deny.from),
                to: ModulePattern::parse(&raw_deny.to),
            })
            .collect();
        let restrict = raw
            .restrict
            .into_iter()
            .map(|raw_restrict| RestrictRule {
                from: ModulePattern::parse(&raw_restrict.from),
                to: raw_restrict
                    .to
                    .iter()
                    .map(|target| ModulePattern::parse(target))
                    .collect(),
            })
            .collect();
        let allow_cycles = raw
            .allow_cycle
            .into_iter()
            .map(|raw_entry| AllowedCycle {
                modules: raw_entry.modules.into_iter().collect(),
                reason: raw_entry.reason,
            })
            .collect();
        Self {
            layers,
            deny,
            restrict,
            allow_cycles,
            strict_layers: raw.strict_layers,
            deny_cycles: raw.deny_cycles,
            deny_parent_child_cycles: raw.deny_parent_child_cycles,
        }
    }

    /// Validate rules against the crate's known modules.
    fn validate(&self, path: &Path, modules: &BTreeSet<String>) -> Result<()> {
        // 1. Every layer pattern must reference a real module (catch typos).
        for layer in &self.layers {
            for pattern in &layer.order {
                if !pattern.references_known(modules) {
                    return Err(AnalysisError::UnknownRuleModule {
                        module: pattern.display(),
                        rule: format!("layers '{}'", layer.name),
                    });
                }
            }
        }
        // 2. Both patterns of every deny rule must reference a real module.
        for rule in &self.deny {
            for pattern in [&rule.from, &rule.to] {
                if !pattern.references_known(modules) {
                    return Err(AnalysisError::UnknownRuleModule {
                        module: pattern.pattern_display(),
                        rule: rule.display(),
                    });
                }
            }
        }
        // 3. Every restrict pattern (`from` and each `to`) must reference a
        //    real module. An empty `to` list is valid: it reads "nothing
        //    beyond my own scope", not a typo.
        for rule in &self.restrict {
            for pattern in std::iter::once(&rule.from).chain(rule.to.iter()) {
                if !pattern.references_known(modules) {
                    return Err(AnalysisError::UnknownRuleModule {
                        module: pattern.pattern_display(),
                        rule: rule.display(),
                    });
                }
            }
        }
        // 4. Allowlist entries name exact modules and must be able to match at
        //    all — a typo or a one-module entry would silently never fire.
        let mut seen: BTreeSet<&BTreeSet<String>> = BTreeSet::new();
        for entry in &self.allow_cycles {
            if entry.modules.len() < 2 {
                return Err(AnalysisError::RuleConfigError {
                    path: path.to_path_buf(),
                    reason: format!("{}: a cycle needs at least two modules", entry.display()),
                });
            }
            for module in &entry.modules {
                if !modules.contains(module) {
                    return Err(AnalysisError::UnknownRuleModule {
                        module: module.clone(),
                        rule: entry.display(),
                    });
                }
            }
            if !seen.insert(&entry.modules) {
                return Err(AnalysisError::RuleConfigError {
                    path: path.to_path_buf(),
                    reason: format!("duplicate {}", entry.display()),
                });
            }
        }
        // 5. Under strict mode, every module must be covered by some group.
        //    Groups may overlap, so coverage just means at least one membership.
        if self.strict_layers {
            for module in modules {
                if self.memberships(module).is_empty() {
                    return Err(AnalysisError::RuleConfigError {
                        path: path.to_path_buf(),
                        reason: format!(
                            "strict-layers: module '{module}' is not assigned to any layer"
                        ),
                    });
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::AnnotatedEdges;
    use std::io::Write;

    fn write_config(dir: &Path, name: &str, body: &str) -> PathBuf {
        let path = dir.join(name);
        let mut file = std::fs::File::create(&path).expect("create config");
        file.write_all(body.as_bytes()).expect("write config");
        path
    }

    fn module_set(names: &[&str]) -> BTreeSet<String> {
        names.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn explicit_path_used_verbatim() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(dir.path(), "custom.toml", "[check]\n");
        let resolved = resolve_config_path(dir.path(), Some(&cfg)).expect("resolve");
        assert_eq!(resolved, cfg);
    }

    #[test]
    fn explicit_missing_path_errors() {
        let dir = tempfile::tempdir().expect("tempdir");
        let missing = dir.path().join("nope.toml");
        assert!(resolve_config_path(dir.path(), Some(&missing)).is_err());
    }

    #[test]
    fn plain_name_wins_over_hidden() {
        let dir = tempfile::tempdir().expect("tempdir");
        write_config(dir.path(), "crawk.toml", "[check]\n");
        write_config(dir.path(), ".crawk.toml", "[check]\n");
        let resolved = resolve_config_path(dir.path(), None).expect("resolve");
        assert_eq!(resolved, dir.path().join("crawk.toml"));
    }

    #[test]
    fn hidden_name_used_when_alone() {
        let dir = tempfile::tempdir().expect("tempdir");
        write_config(dir.path(), ".crawk.toml", "[check]\n");
        let resolved = resolve_config_path(dir.path(), None).expect("resolve");
        assert_eq!(resolved, dir.path().join(".crawk.toml"));
    }

    #[test]
    fn no_config_errors() {
        let dir = tempfile::tempdir().expect("tempdir");
        assert!(resolve_config_path(dir.path(), None).is_err());
    }

    #[test]
    fn unknown_module_in_layer_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"app\"\norder = [\"cli\", \"typo_mod\"]\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject unknown module");
        assert!(matches!(err, AnalysisError::UnknownRuleModule { .. }));
    }

    #[test]
    fn overlapping_groups_are_allowed() {
        let dir = tempfile::tempdir().expect("tempdir");
        // `parser` belongs to both groups; overlap is no longer a config error.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"a\"\norder = [\"cli\", \"parser\"]\n\
             [[check.layers]]\nname = \"b\"\norder = [\"parser\", \"discover\"]\n",
        );
        let modules = module_set(&["cli", "parser", "discover"]);
        assert!(RuleSet::load(&cfg, &modules).is_ok());
    }

    #[test]
    fn duplicate_group_names_are_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"app\"\norder = [\"cli\"]\n\
             [[check.layers]]\nname = \"app\"\norder = [\"analyzer\"]\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject duplicate names");
        assert!(matches!(&err, AnalysisError::RuleConfigError { .. }));
        if let AnalysisError::RuleConfigError { reason, .. } = err {
            // The duplicate "app" sits on line 5 of the config; the message pins it.
            assert!(
                reason.contains("duplicate layer group name 'app'"),
                "{reason}"
            );
            assert!(reason.contains("line 5"), "{reason}");
        }
    }

    #[test]
    fn strict_layers_requires_full_coverage() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\nstrict-layers = true\n\
             [[check.layers]]\nname = \"app\"\norder = [\"cli\"]\n",
        );
        let modules = module_set(&["cli", "uncovered"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should require coverage");
        assert!(matches!(err, AnalysisError::RuleConfigError { .. }));
    }

    #[test]
    fn unknown_top_level_key_is_rejected() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(dir.path(), "crawk.toml", "[check]\nlayerz = []\n");
        let modules = module_set(&["cli"]);
        assert!(RuleSet::load(&cfg, &modules).is_err());
    }

    #[test]
    fn scaffold_lists_top_level_modules() {
        let modules = module_set(&["cli", "analyzer", "web", "web::api", "web::repo"]);
        let body = scaffold("my_crate", &modules, &[]);
        assert!(body.contains("[[check.layers]]"));
        // Group is named after the crate.
        assert!(body.contains("name = \"my_crate\""));
        // Top-level segments only, deduplicated and alphabetical.
        assert!(body.contains(r#"order = ["analyzer", "cli", "web"]"#));
        assert!(!body.contains("web::api"));
    }

    fn cycle_of(modules: &[&str]) -> Cycle {
        Cycle::new(module_set(modules), AnnotatedEdges::new())
    }

    #[test]
    fn scaffold_enables_the_cycle_rule() {
        let body = scaffold("my_crate", &module_set(&["cli"]), &[]);
        // The `[check]` table must precede the first `[[check.layers]]`, or TOML
        // would attach the key to the layer group instead.
        let check_at = body.find("[check]").expect("[check] table");
        let layers_at = body.find("[[check.layers]]").expect("layers group");
        assert!(check_at < layers_at, "{body}");
        assert!(body.contains("deny-cycles = true"), "{body}");
        // Nothing to freeze in an acyclic crate.
        assert!(!body.contains("[[check.allow-cycle]]"), "{body}");
    }

    #[test]
    fn scaffold_freezes_existing_cycles() {
        let modules = module_set(&["alpha", "beta", "gamma", "delta", "epsilon"]);
        let cycles = [
            cycle_of(&["delta", "epsilon"]),
            cycle_of(&["alpha", "beta", "gamma"]),
        ];
        let body = scaffold("my_crate", &modules, &cycles);
        assert!(
            body.contains(r#"modules = ["alpha", "beta", "gamma"]"#),
            "{body}"
        );
        assert!(body.contains(r#"modules = ["delta", "epsilon"]"#), "{body}");
        // Sorted, so the generated file does not churn between runs.
        let alpha_at = body.find(r#"["alpha""#).expect("alpha entry");
        let delta_at = body.find(r#"["delta""#).expect("delta entry");
        assert!(alpha_at < delta_at, "{body}");
    }

    #[test]
    fn scaffold_skips_containment_cycles() {
        // A parent tangled with its own submodule is never reported, so writing
        // an entry for it would be noise.
        let modules = module_set(&["nest", "nest::inner"]);
        let cycles = [cycle_of(&["nest", "nest::inner"])];
        let body = scaffold("my_crate", &modules, &cycles);
        assert!(!body.contains("[[check.allow-cycle]]"), "{body}");
    }

    #[test]
    fn scaffolded_cycle_baseline_round_trips() {
        // The generated text must load back as a valid rule set.
        let dir = tempfile::tempdir().expect("tempdir");
        let modules = module_set(&["alpha", "beta"]);
        let body = scaffold("my_crate", &modules, &[cycle_of(&["alpha", "beta"])]);
        let cfg = write_config(dir.path(), "crawk.toml", &body);
        let rules = RuleSet::load(&cfg, &modules).expect("generated config must load");
        assert!(rules.deny_cycles);
        assert_eq!(rules.allow_cycles.len(), 1);
        assert!(rules.allow_cycles[0].covers(&module_set(&["alpha", "beta"])));
    }

    #[test]
    fn scaffold_config_writes_when_absent() {
        let dir = tempfile::tempdir().expect("tempdir");
        let modules = module_set(&["cli", "analyzer"]);
        let outcome =
            scaffold_config(dir.path(), None, "my_crate", &modules, &[]).expect("scaffold");
        assert_eq!(outcome.path, dir.path().join("crawk.toml"));
        assert_eq!(outcome.frozen_cycles, 0);
        let body = std::fs::read_to_string(&outcome.path).expect("read back");
        assert!(body.contains("[[check.layers]]"));
    }

    #[test]
    fn scaffold_config_reports_frozen_cycles() {
        let dir = tempfile::tempdir().expect("tempdir");
        let modules = module_set(&["alpha", "beta", "nest", "nest::inner"]);
        // Only the real tangle counts; the containment loop is not written.
        let cycles = [
            cycle_of(&["alpha", "beta"]),
            cycle_of(&["nest", "nest::inner"]),
        ];
        let outcome =
            scaffold_config(dir.path(), None, "my_crate", &modules, &cycles).expect("scaffold");
        assert_eq!(outcome.frozen_cycles, 1);
    }

    #[test]
    fn scaffold_config_honours_explicit_path() {
        let dir = tempfile::tempdir().expect("tempdir");
        let target = dir.path().join("custom.toml");
        let modules = module_set(&["cli"]);
        let outcome = scaffold_config(dir.path(), Some(&target), "my_crate", &modules, &[])
            .expect("scaffold to explicit path");
        assert_eq!(outcome.path, target);
        assert!(target.is_file());
    }

    #[test]
    fn scaffold_config_refuses_existing_explicit_path() {
        let dir = tempfile::tempdir().expect("tempdir");
        let target = write_config(dir.path(), "custom.toml", "[check]\n");
        let modules = module_set(&["cli"]);
        // Explicit target that exists is refused even though discovery finds nothing.
        assert!(scaffold_config(dir.path(), Some(&target), "my_crate", &modules, &[]).is_err());
    }

    #[test]
    fn scaffold_config_refuses_when_config_exists() {
        let dir = tempfile::tempdir().expect("tempdir");
        write_config(dir.path(), "crawk.toml", "[check]\n");
        let modules = module_set(&["cli"]);
        assert!(scaffold_config(dir.path(), None, "my_crate", &modules, &[]).is_err());
    }

    #[test]
    fn scaffold_config_refuses_when_hidden_config_exists() {
        let dir = tempfile::tempdir().expect("tempdir");
        write_config(dir.path(), ".crawk.toml", "[check]\n");
        let modules = module_set(&["cli"]);
        // Reuses discovery, so the hidden name blocks scaffolding too.
        assert!(scaffold_config(dir.path(), None, "my_crate", &modules, &[]).is_err());
    }

    #[test]
    fn deny_rules_parse_with_explicit_subtree() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.deny]]\nfrom = \"cli\"\nto = \"web::*\"\n",
        );
        let modules = module_set(&["cli", "web", "web::repo"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.deny.len(), 1);
        assert!(!rules.deny[0].from.subtree, "bare name stays exact");
        assert!(rules.deny[0].to.subtree, "explicit ::* marks the subtree");
        assert_eq!(rules.deny[0].display(), "deny cli -> web::*");
    }

    #[test]
    fn unknown_module_in_deny_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.deny]]\nfrom = \"clii\"\nto = \"web\"\n",
        );
        let modules = module_set(&["cli", "web"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject unknown module");
        assert!(matches!(
            &err,
            AnalysisError::UnknownRuleModule { module, rule }
                if module == "clii" && rule == "deny clii -> web"
        ));
    }

    #[test]
    fn unknown_key_in_deny_is_rejected() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.deny]]\nfrom = \"cli\"\nto = \"web\"\nvia = \"x\"\n",
        );
        let modules = module_set(&["cli", "web"]);
        assert!(RuleSet::load(&cfg, &modules).is_err());
    }

    #[test]
    fn restrict_rules_parse_with_explicit_subtree() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"format::*\"\nto = [\"lib\", \"graph::*\"]\n",
        );
        let modules = module_set(&["format", "format::use_cmd", "lib", "graph", "graph::edges"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.restrict.len(), 1);
        assert!(
            rules.restrict[0].from.subtree,
            "explicit ::* marks the subtree"
        );
        assert!(!rules.restrict[0].to[0].subtree, "bare name stays exact");
        assert!(
            rules.restrict[0].to[1].subtree,
            "explicit ::* marks the subtree"
        );
        assert_eq!(
            rules.restrict[0].display(),
            "restrict format::* -> [lib, graph::*]"
        );
    }

    #[test]
    fn unknown_module_in_restrict_from_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"clii\"\nto = [\"web\"]\n",
        );
        let modules = module_set(&["cli", "web"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject unknown module");
        assert!(matches!(
            &err,
            AnalysisError::UnknownRuleModule { module, rule }
                if module == "clii" && rule == "restrict clii -> [web]"
        ));
    }

    #[test]
    fn unknown_module_in_restrict_to_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"cli\"\nto = [\"webb\"]\n",
        );
        let modules = module_set(&["cli", "web"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject unknown module");
        assert!(matches!(
            &err,
            AnalysisError::UnknownRuleModule { module, rule }
                if module == "webb" && rule == "restrict cli -> [webb]"
        ));
    }

    #[test]
    fn unknown_key_in_restrict_is_rejected() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"cli\"\nto = [\"web\"]\nvia = \"x\"\n",
        );
        let modules = module_set(&["cli", "web"]);
        assert!(RuleSet::load(&cfg, &modules).is_err());
    }

    #[test]
    fn empty_restrict_allowance_is_valid() {
        let dir = tempfile::tempdir().expect("tempdir");
        // "Nothing beyond my own scope" — a deliberate rule, not a typo (D6).
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"web::*\"\nto = []\n",
        );
        let modules = module_set(&["web", "web::api"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.restrict.len(), 1);
        assert!(rules.restrict[0].to.is_empty());
    }

    #[test]
    fn missing_restrict_to_key_is_parse_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        // `to` has no serde default: an empty allowance must be an explicit
        // `to = []`, so a forgotten key cannot become the strictest rule.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.restrict]]\nfrom = \"cli\"\n",
        );
        let modules = module_set(&["cli"]);
        assert!(RuleSet::load(&cfg, &modules).is_err());
    }

    #[test]
    fn restrict_coexists_with_deny_and_layers() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"app\"\norder = [\"cli\", \"analyzer\"]\n\
             [[check.deny]]\nfrom = \"cli\"\nto = \"analyzer\"\n\
             [[check.restrict]]\nfrom = \"cli\"\nto = [\"analyzer\"]\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.layers.len(), 1);
        assert_eq!(rules.deny.len(), 1);
        assert_eq!(rules.restrict.len(), 1);
    }

    #[test]
    fn deny_and_layers_coexist() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"app\"\norder = [\"cli\", \"analyzer\"]\n\
             [[check.deny]]\nfrom = \"cli\"\nto = \"analyzer\"\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.layers.len(), 1);
        assert_eq!(rules.deny.len(), 1);
    }

    #[test]
    fn valid_config_loads() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[[check.layers]]\nname = \"app\"\norder = [\"cli\", \"analyzer\"]\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        assert!(RuleSet::load(&cfg, &modules).is_ok());
    }

    #[test]
    fn group_deny_same_layer_overrides_default() {
        let dir = tempfile::tempdir().expect("tempdir");
        // Global default off; one group opts in, the other omits the key.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-same-layer = false\n\
             [[check.layers]]\nname = \"strict\"\norder = [\"cli\"]\ndeny-same-layer = true\n\
             [[check.layers]]\nname = \"lax\"\norder = [\"analyzer\"]\n",
        );
        let modules = module_set(&["cli", "analyzer"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert!(rules.layers[0].deny_same_layer, "explicit override wins");
        assert!(
            !rules.layers[1].deny_same_layer,
            "omitted key stays default"
        );
    }

    #[test]
    fn global_deny_same_layer_propagates_to_groups() {
        let dir = tempfile::tempdir().expect("tempdir");
        // Global default on; the group omits the key, so it inherits `true`.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-same-layer = true\n\
             [[check.layers]]\nname = \"app\"\norder = [\"cli\"]\n",
        );
        let modules = module_set(&["cli"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert!(
            rules.layers[0].deny_same_layer,
            "group inherits the default"
        );
    }

    #[test]
    fn cycle_keys_default_to_off() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(dir.path(), "crawk.toml", "[check]\n");
        let rules = RuleSet::load(&cfg, &module_set(&["cli"])).expect("load");
        assert!(!rules.deny_cycles);
        assert!(!rules.deny_parent_child_cycles);
        assert!(rules.allow_cycles.is_empty());
    }

    #[test]
    fn cycle_keys_parse() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-cycles = true\ndeny-parent-child-cycles = true\n",
        );
        let rules = RuleSet::load(&cfg, &module_set(&["cli"])).expect("load");
        assert!(rules.deny_cycles);
        assert!(rules.deny_parent_child_cycles);
    }

    #[test]
    fn allow_cycle_parses_with_reason() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-cycles = true\n\
             [[check.allow-cycle]]\nmodules = [\"alpha\", \"beta\"]\nreason = \"tracked in #1\"\n",
        );
        let modules = module_set(&["alpha", "beta"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert_eq!(rules.allow_cycles.len(), 1);
        assert_eq!(
            rules.allow_cycles[0].reason.as_deref(),
            Some("tracked in #1")
        );
        assert_eq!(rules.allow_cycles[0].display(), "allow-cycle [alpha, beta]");
    }

    #[test]
    fn unknown_module_in_allow_cycle_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-cycles = true\n\
             [[check.allow-cycle]]\nmodules = [\"alpha\", \"alfa\"]\n",
        );
        let modules = module_set(&["alpha", "beta"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject unknown module");
        assert!(matches!(
            &err,
            AnalysisError::UnknownRuleModule { module, rule }
                if module == "alfa" && rule == "allow-cycle [alfa, alpha]"
        ));
    }

    #[test]
    fn single_module_allow_cycle_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        // A cycle needs two modules, so a one-element entry can never match.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-cycles = true\n\
             [[check.allow-cycle]]\nmodules = [\"alpha\"]\n",
        );
        let modules = module_set(&["alpha", "beta"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject one-module entry");
        assert!(matches!(err, AnalysisError::RuleConfigError { .. }));
    }

    #[test]
    fn duplicate_allow_cycle_is_error() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-cycles = true\n\
             [[check.allow-cycle]]\nmodules = [\"alpha\", \"beta\"]\n\
             [[check.allow-cycle]]\nmodules = [\"beta\", \"alpha\"]\n",
        );
        let modules = module_set(&["alpha", "beta"]);
        let err = RuleSet::load(&cfg, &modules).expect_err("should reject duplicate entry");
        assert!(matches!(err, AnalysisError::RuleConfigError { .. }));
    }

    #[test]
    fn unknown_key_in_allow_cycle_is_rejected() {
        let dir = tempfile::tempdir().expect("tempdir");
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\n[[check.allow-cycle]]\nmodules = [\"alpha\", \"beta\"]\nwhy = \"x\"\n",
        );
        assert!(RuleSet::load(&cfg, &module_set(&["alpha", "beta"])).is_err());
    }

    #[test]
    fn group_can_override_global_deny_to_false() {
        let dir = tempfile::tempdir().expect("tempdir");
        // Global default on; the group turns it back off for itself.
        let cfg = write_config(
            dir.path(),
            "crawk.toml",
            "[check]\ndeny-same-layer = true\n\
             [[check.layers]]\nname = \"app\"\norder = [\"cli\"]\ndeny-same-layer = false\n",
        );
        let modules = module_set(&["cli"]);
        let rules = RuleSet::load(&cfg, &modules).expect("load");
        assert!(
            !rules.layers[0].deny_same_layer,
            "group override to false wins"
        );
    }
}