code-ranker-plugin-rust 2.0.0

Code Ranker Rust plugin: cargo metadata + syn module/crate graph.
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
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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
use anyhow::Result;
use code_ranker_plugin_api::{
    attrs::{AttrValue, ValueType},
    default_cycle_kinds, default_node_kinds,
    edge::Edge,
    graph::Graph,
    level::{AttributeSpec, Direction, EdgeKindSpec, Grouping, Level, Thresholds},
    log,
    node::Node,
    plugin::{LanguagePlugin, PluginInput, Preset},
};
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;

use cargo_metadata::MetadataCommand;

mod crate_graph;
mod ids;
mod internal;
mod module_graph;
mod rust_ts;

use internal::{EdgeKind, GraphBuilder, InternalGraph, NodeKind};

pub struct RustPlugin;

/// One Rust-only metric-lens preset: (id, title, sort_metric, connections,
/// doc_slug, prompt body). Same shape as the generic catalog in
/// `code-ranker-cli/src/presets.rs`, but these rank modules by a single
/// coupling/size metric rather than a design principle. Slugs resolve to
/// `principles/rust/<slug>.md`.
type MetricPreset = (
    &'static str,
    &'static str,
    &'static str,
    &'static [&'static str],
    &'static str,
    &'static str,
);

const RUST_METRIC_PRESETS: &[MetricPreset] = &[
    (
        "HK",
        "HK — Henry-Kafura Coupling",
        "hk",
        &["in", "out"],
        "henry-kafura-coupling",
        "These modules carry heavy Henry-Kafura coupling — HK = sloc × (fan_in × fan_out)²,\n\
         where sloc is the module's source lines of code (real code lines, excluding blanks\n\
         and comment-only lines), fan_in is how many modules depend on it, and fan_out is how\n\
         many it depends on.\n\
         A high score is a large module sitting on a busy crossroads of incoming and outgoing\n\
         dependencies, so any change here ripples widely.\n\n\
         For each module below, lower the factor that dominates its HK: shrink the module by\n\
         extracting cohesive pieces, or cut fan-in/fan-out by narrowing its public surface and\n\
         depending on fewer collaborators (introduce an abstraction, move a responsibility).\n\
         Keep existing API contracts intact.",
    ),
    (
        "SLOC",
        "SLOC — Module Size",
        "sloc",
        &[],
        "module-size",
        "These are the largest modules by source lines of code. Size alone is not a defect, but\n\
         oversized files usually bundle several responsibilities and are hard to read, test and\n\
         review.\n\n\
         For each module below, identify the distinct responsibilities it holds and propose how\n\
         to split it into smaller, cohesive modules — each with a single clear purpose — without\n\
         changing external behaviour.",
    ),
    (
        "FANIN",
        "Fan-in — Afferent Coupling",
        "fan_in",
        &["in"],
        "fan-in-afferent-coupling",
        "These modules have high fan-in: many other modules depend on them. They are\n\
         load-bearing — a change here forces changes (or re-review) across every dependant, and\n\
         a bug here is widely felt.\n\n\
         For each module below, confirm its public surface is a stable, minimal contract. Narrow\n\
         the API to what callers actually need, split it if different callers use disjoint parts\n\
         (see Interface Segregation), and stabilise the abstractions the rest of the codebase\n\
         leans on.",
    ),
    (
        "FANOUT",
        "Fan-out — Efferent Coupling",
        "fan_out",
        &["out"],
        "fan-out-efferent-coupling",
        "These modules have high fan-out: they depend on many other modules. High efferent\n\
         coupling makes a module fragile (it breaks when any dependency changes) and hard to\n\
         test or reuse in isolation.\n\n\
         For each module below, reduce its direct dependencies: depend on abstractions rather\n\
         than concretes (see Dependency Inversion), collapse several fine-grained collaborators\n\
         behind one focused interface, and move logic that pulls in unrelated dependencies into\n\
         a more appropriate module.",
    ),
];

impl LanguagePlugin for RustPlugin {
    fn name(&self) -> &str {
        "rust"
    }

    fn detect(&self, workspace: &Path, _input: &PluginInput) -> bool {
        workspace.join("Cargo.toml").exists()
    }

    fn levels(&self) -> Vec<Level> {
        let mut edge_kinds: BTreeMap<String, EdgeKindSpec> = BTreeMap::new();
        edge_kinds.insert(
            "uses".into(),
            EdgeKindSpec {
                flow: true,
                label: Some("uses".into()),
                description: Some(
                    "Code dependency — this file references an item the target file defines.<br>\
                     Captured from `use path::Item;`, a qualified path (`crate::a::Item`, \
                     `other_crate::Item`), or a derive (`#[derive(serde::Serialize)]`).<br>\
                     The path resolves to the file that defines the item (following `pub use` \
                     re-exports), so the edge points at the definition, not a re-export hub.<br>\
                     This is the real dependency: it counts toward fan-in / fan-out, \
                     Henry-Kafura coupling and cycles."
                        .into(),
                ),
            },
        );
        edge_kinds.insert(
            "contains".into(),
            EdgeKindSpec {
                flow: false,
                label: Some("contains".into()),
                description: Some(
                    "Module ownership — the parent declares the child module \
                     (`mod foo;` / `pub mod foo;`), so `foo.rs` (or `foo/mod.rs`) belongs to it.<br>\
                     This is the Rust module tree: structure, not a code dependency.<br>\
                     Kept in the data but not drawn on the main map, and excluded from \
                     fan-in / fan-out / HK / cycles."
                        .into(),
                ),
            },
        );
        edge_kinds.insert(
            "reexports".into(),
            EdgeKindSpec {
                flow: false,
                label: Some("reexport".into()),
                description: Some(
                    "Re-export (`pub use foo::Item;`) — re-publishes another file's item as part of \
                     this file's public API (the crate-root / prelude facade, e.g. `lib.rs` doing \
                     `pub use access_scope::AccessScope;`).<br>\
                     A facade, not a dependency: excluded from fan-in / fan-out / HK / cycles and \
                     not drawn on the main map, like `contains`.<br>\
                     A consumer's `use this_crate::Item` is attributed to the file that defines \
                     `Item`, so re-export hubs (`lib.rs` / `mod.rs`) collect no false coupling — the \
                     `pub use` is still recorded here so you can see what a file exposes."
                        .into(),
                ),
            },
        );
        edge_kinds.insert(
            "super".into(),
            EdgeKindSpec {
                flow: false,
                label: Some("super".into()),
                description: Some(
                    "Namespace pull from an enclosing module — a glob `use` that reaches \
                     *up* the module tree (`use super::*`, `use crate::<ancestor>::*`), \
                     bringing the parent's items into the child's scope.<br>\
                     Usually structural scope-sugar (a module split across files referring \
                     back to itself). But if the child actually uses a parent item brought \
                     in by the glob, it IS a real back-dependency — technically a cycle. \
                     code-ranker can't tell the two apart without name resolution, so it \
                     treats `super` as a **low-priority** cycle and leaves it non-flow: \
                     deprioritized next to obvious cross-module cycles.<br>\
                     Kept in the data but not drawn on the main map, and excluded from \
                     fan-in / fan-out / HK / cycles — like `contains`."
                        .into(),
                ),
            },
        );

        let aspec = AttributeSpec::new;

        let mut node_attributes: BTreeMap<String, AttributeSpec> = BTreeMap::new();
        node_attributes.insert("path".into(), aspec(ValueType::Str, "Path"));
        node_attributes.insert("crate".into(), aspec(ValueType::Str, "Crate"));
        node_attributes.insert("loc".into(), aspec(ValueType::Int, "Lines"));
        node_attributes.insert("visibility".into(), aspec(ValueType::Str, "Visibility"));
        node_attributes.insert("external".into(), aspec(ValueType::Bool, "External"));
        node_attributes.insert("version".into(), aspec(ValueType::Str, "Version"));
        node_attributes.insert("items".into(), aspec(ValueType::Int, "Items"));
        let mut unsafe_spec = aspec(ValueType::Int, "Unsafe");
        unsafe_spec.short = Some("Unsafe".into());
        unsafe_spec.description = Some(
            "Count of `unsafe` blocks and `unsafe fn`/`impl`/`trait` declarations \
             in production code (test items are excluded). Syntactic count: \
             `unsafe` inside a macro body is not seen, and the figure is not \
             type-checked."
                .into(),
        );
        unsafe_spec.direction = Direction::LowerBetter;
        node_attributes.insert("unsafe".into(), unsafe_spec);

        let mut edge_attributes: BTreeMap<String, AttributeSpec> = BTreeMap::new();
        edge_attributes.insert("visibility".into(), aspec(ValueType::Str, "Visibility"));

        vec![Level {
            name: "files".into(),
            edge_kinds,
            node_attributes,
            edge_attributes,
            attribute_groups: BTreeMap::new(),
            node_kinds: default_node_kinds(),
            cycle_kinds: default_cycle_kinds(),
            // Cluster the diagram by the owning crate (compilation unit), not by
            // the source folder. Falls back to `dir` if `crate` is ever absent.
            grouping: Some(Grouping {
                key: Some("crate".into()),
                function: None,
            }),
        }]
    }

    fn thresholds(&self) -> BTreeMap<String, Thresholds> {
        // Calibrated on 21 Rust crates (≥2K SLOC). ~50% of projects breach
        // `info`, ~10% breach `warning`.
        BTreeMap::from([
            (
                "hk".into(),
                Thresholds {
                    info: 150_000.0,
                    warning: 10_000_000.0,
                },
            ),
            (
                "sloc".into(),
                Thresholds {
                    info: 800.0,
                    warning: 3_000.0,
                },
            ),
            (
                "fan_out".into(),
                Thresholds {
                    info: 8.0,
                    warning: 18.0,
                },
            ),
            (
                "items".into(),
                Thresholds {
                    info: 20.0,
                    warning: 50.0,
                },
            ),
        ])
    }

    fn presets(&self, mut defaults: Vec<Preset>, _input: &PluginInput) -> Vec<Preset> {
        // Append Rust-only metric lenses to the generic catalog. Their doc links
        // reuse the principles base directory derived from an existing default's
        // `doc_url`, so they resolve to `principles/rust/<slug>.md` without
        // duplicating the host/base constant that lives in the CLI crate.
        let base_dir = defaults
            .iter()
            .find_map(|p| p.doc_url.as_deref())
            .and_then(|u| u.rsplit_once('/').map(|(dir, _)| dir.to_string()));
        for &(id, title, sort_metric, connections, slug, prompt) in RUST_METRIC_PRESETS {
            defaults.push(Preset {
                id: id.to_string(),
                label: id.to_string(),
                title: title.to_string(),
                prompt: prompt.to_string(),
                doc_url: base_dir.as_ref().map(|d| format!("{d}/{slug}.md")),
                sort_metric: sort_metric.to_string(),
                connections: connections.iter().map(|s| (*s).to_string()).collect(),
            });
        }
        defaults
    }

    fn analyze(&self, workspace: &Path, _level: &str, input: &PluginInput) -> Result<Graph> {
        let mut builder = GraphBuilder::new();
        syn_analyze(workspace, input.ignore_tests, &mut builder)?;
        let internal = builder.build();
        Ok(collapse_to_files(internal))
    }

    fn metrics(&self, graph: &mut Graph) -> usize {
        // Each `.rs` file node is re-read (by its absolute-path `id`) and measured
        // by our `tree-sitter-rust` engine; `#[cfg(test)]` / `#[test]` items are
        // stripped first so metrics reflect production code only (their lines
        // become `tloc`).
        let mut annotated = 0;
        for node in &mut graph.nodes {
            if node.kind != "file" {
                continue;
            }
            let Ok(src) = std::fs::read(&node.id) else {
                continue;
            };
            if rust_file_metrics(node, &src) {
                annotated += 1;
            }
        }
        annotated
    }

    fn is_test_path(&self, rel_path: &str) -> bool {
        // Cargo's integration-test / bench targets live under top-level
        // `tests/` and `benches/` dirs. (Inline `#[cfg(test)]` modules are a
        // separate, attribute-based notion handled during the syn walk.)
        matches!(rel_path.split('/').next(), Some("tests") | Some("benches"))
    }

    fn versions(&self, _workspace: &Path, _input: &PluginInput) -> Vec<(String, String)> {
        version_string()
            .map(|rv| vec![("rustc".to_string(), rv)])
            .unwrap_or_default()
    }

    fn roots(&self, _workspace: &Path) -> Vec<(String, String)> {
        rust_toolchain_roots()
    }

    fn metric_specs(
        &self,
        mut defaults: BTreeMap<String, AttributeSpec>,
    ) -> BTreeMap<String, AttributeSpec> {
        // Rust strips inline `#[cfg(test)]` / `#[test]` / `#[bench]` items before
        // measuring, so the LOC metrics count production code only — a nuance the
        // language-neutral default descriptions omit. Refine them for Rust.
        let rust_loc_note: &[(&str, &str)] = &[
            (
                "sloc",
                "Source lines of code — lines with at least one non-whitespace, non-comment character. Blank and comment-only lines are not counted. In Rust, lines inside `#[cfg(test)]` / `#[test]` items are excluded too, so this counts production code only (unlike `loc`, the raw file line count).",
            ),
            (
                "lloc",
                "Logical lines — counts statements, not physical lines. In Rust, measured on production code only (inline `#[cfg(test)]` / `#[test]` tests are excluded, like `sloc`; their lines are `tloc`).",
            ),
            (
                "cloc",
                "Comment-only lines (inline comments on code lines are not counted). In Rust, measured on production code only (inline `#[cfg(test)]` / `#[test]` tests are excluded, like `sloc`; their lines are `tloc`).",
            ),
            (
                "blank",
                "Empty or whitespace-only lines. In Rust, measured on production code only (inline `#[cfg(test)]` / `#[test]` tests are excluded, like `sloc`; their lines are `tloc`).",
            ),
        ];
        for (key, desc) in rust_loc_note {
            if let Some(spec) = defaults.get_mut(*key) {
                spec.description = Some((*desc).to_string());
            }
        }
        defaults
    }
}

/// The Rust/Cargo toolchain path roots used to shorten external node ids in the
/// snapshot: `cargo` (`$CARGO_HOME`), `registry` (the crates.io source dir),
/// `rustup` (`$RUSTUP_HOME`), and `rust-src` (the stdlib source under the active
/// sysroot). These are Rust-specific, so they live here in the Rust plugin rather
/// than in the language-agnostic orchestrator.
fn rust_toolchain_roots() -> Vec<(String, String)> {
    let mut roots = Vec::new();
    let home = std::env::var("HOME").unwrap_or_default();

    let cargo = std::env::var("CARGO_HOME").unwrap_or_else(|_| format!("{home}/.cargo"));
    let rustup = std::env::var("RUSTUP_HOME").unwrap_or_else(|_| format!("{home}/.rustup"));

    if !cargo.is_empty() {
        // Auto-detect crates.io registry hash dir (e.g. index.crates.io-<hash>).
        let registry_src = format!("{cargo}/registry/src");
        if let Ok(entries) = std::fs::read_dir(&registry_src) {
            for entry in entries.flatten() {
                let name = entry.file_name().to_string_lossy().to_string();
                if name.starts_with("index.crates.io") {
                    roots.push(("registry".to_string(), format!("{registry_src}/{name}")));
                    break;
                }
            }
        }
        roots.push(("cargo".to_string(), cargo));
    }
    if !rustup.is_empty() {
        // Add rust-src root: sysroot/lib/rustlib/src/rust/library — shortens stdlib
        // paths from {rustup}/toolchains/.../library/... to {rust-src}/...
        if which::which("rustc").is_ok()
            && let Ok(out) = log::timed("rustc --print sysroot", || {
                std::process::Command::new("rustc")
                    .args(["--print", "sysroot"])
                    .output()
            })
            && out.status.success()
        {
            let sysroot = String::from_utf8_lossy(&out.stdout).trim().to_string();
            let rust_lib = format!("{sysroot}/lib/rustlib/src/rust/library");
            if std::path::Path::new(&rust_lib).exists() {
                roots.push(("rust-src".to_string(), rust_lib));
            }
        }
        roots.push(("rustup".to_string(), rustup));
    }
    roots
}

/// Syntactic stage: resolve the workspace via `cargo metadata` and build the
/// internal crate + module/use graphs.
fn syn_analyze(workspace: &Path, ignore_tests: bool, builder: &mut GraphBuilder) -> Result<()> {
    let manifest = workspace.join("Cargo.toml");
    // code-ranker is an offline tool: it never fetches from the network. See the
    // comment in the original lib.rs for the research notes on --offline vs
    // --no-deps vs full. Short version: --offline keeps external/cross-crate
    // edges AND never goes to the network; the cache must be warm.
    let metadata = log::timed("cargo metadata --offline", || {
        MetadataCommand::new()
            .manifest_path(&manifest)
            .other_options(vec!["--offline".to_string()])
            .exec()
    })
    .map_err(|err| offline_metadata_error(&manifest, err))?;

    crate_graph::contribute(&metadata, builder);
    module_graph::contribute(&metadata, ignore_tests, builder)?;
    Ok(())
}

fn offline_metadata_error(manifest: &Path, err: cargo_metadata::Error) -> anyhow::Error {
    anyhow::anyhow!(
        "cargo metadata (offline) failed for {manifest}\n\n\
         code-ranker is an offline tool — it never downloads dependencies. It reads \
         the dependency graph from cargo's local cache, which must already be \
         populated for this project.\n\n\
         Warm the cache once (with network), then re-run code-ranker:\n    \
         cargo metadata --manifest-path {manifest} >/dev/null\n\
         (a prior `cargo build` / `cargo fetch` works too).\n\n\
         In CI: run code-ranker on the same image/cache as your build or test jobs, \
         where the cache is already warm.\n\n\
         Underlying cargo error: {err}",
        manifest = manifest.display(),
    )
}

fn version_string() -> Option<String> {
    which::which("rustc").ok()?;
    let out = log::timed("rustc --version", || {
        std::process::Command::new("rustc")
            .arg("--version")
            .output()
    })
    .ok()?;
    if out.status.success() {
        Some(
            String::from_utf8_lossy(&out.stdout)
                .split_whitespace()
                .nth(1)
                .unwrap_or("unknown")
                .to_string(),
        )
    } else {
        None
    }
}

/// Collapse the internal module graph into a file-level `api::Graph`.
///
/// - Every `Module` node maps to a `file` node keyed by its ABSOLUTE source
///   path (no `file:` prefix). Inline modules collapse into the file they live
///   in. The file-backed module (line == None) is the source of truth for
///   structural attrs.
/// - External crate nodes become one `external` node each (id `ext:{name}`).
/// - `use`/`pub use` edges are re-pointed to files; self-edges (within the same
///   file) are dropped.
/// - Crate→crate dependency edges (metadata-level) are dropped; precise
///   file→file edges come from `use` statements.
fn collapse_to_files(full: InternalGraph) -> Graph {
    let mut id_map: HashMap<String, String> = HashMap::new();
    let mut file_nodes: HashMap<String, Node> = HashMap::new();
    let mut ext_nodes: HashMap<String, Node> = HashMap::new();

    // Pre-pass: map each LOCAL crate node to its crate-root source file
    // (lib.rs / main.rs) via the crate→root-module Contains edge. This lets
    // cross-crate `use other_crate::…` become file→file edges.
    let node_by_id: HashMap<&str, &internal::Node> =
        full.nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    let crate_ids: HashSet<&str> = full
        .nodes
        .iter()
        .filter(|n| n.kind == NodeKind::Crate)
        .map(|n| n.id.as_str())
        .collect();
    let mut crate_root_file: HashMap<String, String> = HashMap::new();
    for e in &full.edges {
        if e.kind != EdgeKind::Contains {
            continue;
        }
        let (Some(from), Some(to)) = (
            node_by_id.get(e.from.as_str()),
            node_by_id.get(e.to.as_str()),
        ) else {
            continue;
        };
        if from.kind == NodeKind::Crate && to.kind == NodeKind::Module && !to.path.is_empty() {
            let file = to.path.clone(); // ABSOLUTE path, no prefix
            match crate_root_file.entry(e.from.clone()) {
                Entry::Vacant(v) => {
                    v.insert(file);
                }
                Entry::Occupied(mut o) if to.path.ends_with("lib.rs") => {
                    *o.get_mut() = file;
                }
                Entry::Occupied(_) => {}
            }
        }
    }

    for node in &full.nodes {
        match node.kind {
            NodeKind::Module => {
                let fid = node.path.clone(); // ABSOLUTE path
                id_map.insert(node.id.clone(), fid.clone());
                let name = Path::new(&node.path)
                    .file_name()
                    .map(|s| s.to_string_lossy().into_owned())
                    .unwrap_or_else(|| node.name.clone());
                match file_nodes.entry(fid.clone()) {
                    Entry::Vacant(v) => {
                        let mut attrs = BTreeMap::new();
                        if let Some(vis) = &node.visibility {
                            attrs.insert(
                                "visibility".to_string(),
                                AttrValue::Str(vis.as_str().to_string()),
                            );
                        }
                        if let Some(loc) = node.loc {
                            attrs.insert("loc".to_string(), AttrValue::Int(loc as i64));
                        }
                        if let Some(items) = node.item_count {
                            attrs.insert("items".to_string(), AttrValue::Int(items as i64));
                        }
                        // Omit when zero, like other metrics — files with no
                        // `unsafe` simply carry no key.
                        if let Some(u) = node.unsafe_count
                            && u > 0
                        {
                            attrs.insert("unsafe".to_string(), AttrValue::Int(u as i64));
                        }
                        if let Some(krate) = &node.crate_label {
                            attrs.insert("crate".to_string(), AttrValue::Str(krate.clone()));
                        }
                        v.insert(Node {
                            id: fid,
                            kind: "file".into(),
                            name,
                            parent: None,
                            attrs,
                        });
                    }
                    Entry::Occupied(mut o) => {
                        // The file-backed module (line == None) is the source
                        // of truth for the file's structural attrs.
                        if node.line.is_none() {
                            let n = o.get_mut();
                            if let Some(vis) = &node.visibility {
                                n.attrs.insert(
                                    "visibility".to_string(),
                                    AttrValue::Str(vis.as_str().to_string()),
                                );
                            }
                            if let Some(loc) = node.loc {
                                n.attrs
                                    .insert("loc".to_string(), AttrValue::Int(loc as i64));
                            }
                            if let Some(items) = node.item_count {
                                n.attrs
                                    .insert("items".to_string(), AttrValue::Int(items as i64));
                            }
                            if let Some(u) = node.unsafe_count
                                && u > 0
                            {
                                n.attrs
                                    .insert("unsafe".to_string(), AttrValue::Int(u as i64));
                            }
                            if let Some(krate) = &node.crate_label {
                                n.attrs
                                    .insert("crate".to_string(), AttrValue::Str(krate.clone()));
                            }
                        }
                    }
                }
            }
            NodeKind::Crate if node.external.unwrap_or(false) => {
                let eid = format!("ext:{}", node.name);
                id_map.insert(node.id.clone(), eid.clone());
                // The on-disk directory of this dependency (parent of its
                // Cargo.toml), e.g. `…/registry/src/…/serde-1.0.228`.
                let lib_path = Path::new(&node.path)
                    .parent()
                    .map(|p| p.to_string_lossy().into_owned())
                    .unwrap_or_default();
                ext_nodes.entry(eid.clone()).or_insert_with(|| {
                    let mut attrs = BTreeMap::new();
                    attrs.insert("external".to_string(), AttrValue::Bool(true));
                    if let Some(v) = &node.version {
                        attrs.insert("version".to_string(), AttrValue::Str(v.clone()));
                    }
                    if !lib_path.is_empty() {
                        attrs.insert("path".to_string(), AttrValue::Str(lib_path));
                    }
                    Node {
                        id: eid,
                        kind: "external".into(),
                        name: node.name.clone(),
                        parent: None,
                        attrs,
                    }
                });
            }
            // A local workspace crate maps to its root file.
            NodeKind::Crate => {
                if let Some(file) = crate_root_file.get(&node.id) {
                    id_map.insert(node.id.clone(), file.clone());
                }
            }
        }
    }

    // Re-point edges to file/external granularity.
    let mut seen: HashSet<(String, String, String)> = HashSet::new();
    let mut edges: Vec<Edge> = Vec::new();
    for e in &full.edges {
        // Drop crate→crate dependency edges; precise file→file edges come from
        // `use` statements.
        if crate_ids.contains(e.from.as_str()) && crate_ids.contains(e.to.as_str()) {
            continue;
        }
        let (Some(from), Some(to)) = (id_map.get(&e.from), id_map.get(&e.to)) else {
            continue;
        };
        if from == to {
            continue; // within the same file — not a connection
        }
        let kind_str = match e.kind {
            EdgeKind::Contains => "contains",
            EdgeKind::Uses => "uses",
            EdgeKind::Reexports => "reexports",
            EdgeKind::Super => "super",
        };
        if !seen.insert((from.clone(), to.clone(), kind_str.to_string())) {
            continue;
        }
        let mut attrs = BTreeMap::new();
        if e.kind == EdgeKind::Reexports
            && let Some(vis) = &e.visibility
        {
            attrs.insert(
                "visibility".to_string(),
                AttrValue::Str(vis.as_str().to_string()),
            );
        }
        edges.push(Edge {
            source: from.clone(),
            target: to.clone(),
            kind: kind_str.to_string(),
            line: e.line,
            attrs,
        });
    }

    // Assemble nodes: all files + only the libraries actually referenced.
    let referenced_ext: HashSet<&str> = edges
        .iter()
        .filter(|e| ext_nodes.contains_key(&e.target))
        .map(|e| e.target.as_str())
        .collect();
    let mut nodes: Vec<Node> = file_nodes.into_values().collect();
    nodes.extend(
        ext_nodes
            .into_iter()
            .filter(|(id, _)| referenced_ext.contains(id.as_str()))
            .map(|(_, n)| n),
    );

    // Deterministic output ordering.
    nodes.sort_by(|a, b| a.id.cmp(&b.id));
    edges.sort_by(|a, b| {
        a.source
            .cmp(&b.source)
            .then(a.target.cmp(&b.target))
            .then(a.kind.cmp(&b.kind))
    });

    Graph { nodes, edges }
}

// ─────────────────────────────────────────────────────────────────────────────
// Complexity: strip inline tests, run the tree-sitter-rust engine, write metrics
// ─────────────────────────────────────────────────────────────────────────────

/// Compute and write Rust complexity metrics for one file node from its source
/// bytes. `#[cfg(test)]` / `#[test]` / `#[bench]` items are stripped first (their
/// lines become `tloc`), then the in-tree `rust_ts` engine runs. Returns `true`
/// if metrics were written (`false` if the source did not parse).
fn rust_file_metrics(node: &mut Node, src: &[u8]) -> bool {
    let (prod, tloc) = strip_cfg_test(src);
    let Some(mut m) = rust_ts::compute(&prod) else {
        return false;
    };
    m.tloc = tloc as f64;
    code_ranker_graph::write_metrics(node, &m);
    true
}

/// True if any attribute gates an item to tests: `#[test]`, `#[bench]`, or
/// `#[cfg(test)]` / `#[cfg(all(test, …))]` / `#[cfg(any(test, …))]`. A `test`
/// **identifier** inside `cfg(...)` is what matches — `cfg(feature = "test")`
/// (a string literal) does not.
fn is_test_attr(attr: &syn::Attribute) -> bool {
    if attr.path().is_ident("test") || attr.path().is_ident("bench") {
        return true;
    }
    if attr.path().is_ident("cfg")
        && let syn::Meta::List(list) = &attr.meta
    {
        return tokens_have_test_ident(list.tokens.clone());
    }
    false
}

/// Recursively scan a token stream for a bare `test` identifier (descends into
/// `all(...)` / `any(...)` groups).
fn tokens_have_test_ident(ts: proc_macro2::TokenStream) -> bool {
    ts.into_iter().any(|t| match t {
        proc_macro2::TokenTree::Ident(i) => i == "test",
        proc_macro2::TokenTree::Group(g) => tokens_have_test_ident(g.stream()),
        _ => false,
    })
}

/// Visitor collecting the 1-based, inclusive line ranges of test-only items
/// (`#[cfg(test)]` modules, `#[test]`/`#[cfg(test)]` fns), attribute line
/// included. It recurses into ordinary modules to catch nested test modules but
/// not into a test item it already captured.
#[derive(Default)]
struct TestSpans {
    ranges: Vec<(usize, usize)>,
}

impl TestSpans {
    fn record(&mut self, attrs: &[syn::Attribute], span: proc_macro2::Span) {
        use syn::spanned::Spanned;
        let start = attrs
            .iter()
            .map(|a| a.span().start().line)
            .chain(std::iter::once(span.start().line))
            .min()
            .unwrap_or(0);
        self.ranges.push((start, span.end().line));
    }
}

impl<'ast> syn::visit::Visit<'ast> for TestSpans {
    fn visit_item_mod(&mut self, m: &'ast syn::ItemMod) {
        use syn::spanned::Spanned;
        if m.attrs.iter().any(is_test_attr) {
            self.record(&m.attrs, m.span());
        } else {
            syn::visit::visit_item_mod(self, m);
        }
    }
    fn visit_item_fn(&mut self, f: &'ast syn::ItemFn) {
        use syn::spanned::Spanned;
        if f.attrs.iter().any(is_test_attr) {
            self.record(&f.attrs, f.span());
        }
    }
}

/// Step 1 of the Rust line accounting: remove `#[cfg(test)]` / `#[test]` /
/// `#[bench]` items so the production metrics (`sloc` / `cloc` / `blank` / `hk` /
/// complexity) are then measured on production code only. Returns the production
/// source **and** `tloc` — the number of test lines removed (the whole test
/// region: attribute, body, braces). Parse failures or no test items return the
/// source unchanged with `tloc = 0`.
fn strip_cfg_test(src: &[u8]) -> (Vec<u8>, usize) {
    use syn::visit::Visit;
    let Ok(text) = std::str::from_utf8(src) else {
        return (src.to_vec(), 0);
    };
    let Ok(file) = syn::parse_file(text) else {
        return (src.to_vec(), 0);
    };
    let mut spans = TestSpans::default();
    spans.visit_file(&file);
    if spans.ranges.is_empty() {
        return (src.to_vec(), 0);
    }
    let drop: std::collections::HashSet<usize> =
        spans.ranges.iter().flat_map(|&(s, e)| s..=e).collect();
    let tloc = drop.len();
    let mut out: String = text
        .lines()
        .enumerate()
        .filter(|(i, _)| !drop.contains(&(i + 1)))
        .map(|(_, l)| l)
        .collect::<Vec<_>>()
        .join("\n");
    out.push('\n');
    (out.into_bytes(), tloc)
}

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

    fn strip(src: &str) -> String {
        String::from_utf8(strip_cfg_test(src.as_bytes()).0).unwrap()
    }

    /// Build a `Module` internal node for one file, with structural attrs.
    /// `line` distinguishes an inline module (`Some`) from a file-backed one
    /// (`None`); `collapse_to_files` lets the file-backed node win.
    #[allow(clippy::too_many_arguments)]
    fn module_node(
        id: &str,
        path: &str,
        line: Option<u32>,
        visibility: internal::Visibility,
        loc: u32,
        items: u32,
        unsafe_count: u32,
        krate: &str,
    ) -> internal::Node {
        internal::Node {
            id: id.into(),
            kind: NodeKind::Module,
            name: id.into(),
            path: path.into(),
            parent: None,
            external: None,
            version: None,
            visibility: Some(visibility),
            loc: Some(loc),
            line,
            item_count: Some(items),
            unsafe_count: Some(unsafe_count),
            crate_label: Some(krate.into()),
        }
    }

    #[test]
    fn collapse_lets_the_file_backed_module_overwrite_structural_attrs() {
        // Two modules map to one file id (same `path`): an inline module
        // (`line = Some`) is seen first and seeds the file node, then the
        // file-backed module (`line = None`) is the source of truth and must
        // overwrite every structural attr (visibility / loc / items / unsafe /
        // crate). This exercises the Occupied-entry update branch of
        // `collapse_to_files`.
        let mut builder = GraphBuilder::new();
        builder.add_node(module_node(
            "inline",
            "/x/foo.rs",
            Some(5),
            internal::Visibility::Private,
            1,
            1,
            0,
            "wrong-crate",
        ));
        builder.add_node(module_node(
            "file",
            "/x/foo.rs",
            None,
            internal::Visibility::Public,
            42,
            7,
            3,
            "mycrate",
        ));

        let graph = collapse_to_files(builder.build());

        let file = graph
            .nodes
            .iter()
            .find(|n| n.id == "/x/foo.rs")
            .expect("the two modules collapsed into one file node");
        assert_eq!(file.kind, "file");
        assert_eq!(
            file.attrs.get("visibility"),
            Some(&AttrValue::Str("public".into())),
            "file-backed visibility wins"
        );
        assert_eq!(
            file.attrs.get("loc"),
            Some(&AttrValue::Int(42)),
            "file-backed loc wins"
        );
        assert_eq!(
            file.attrs.get("items"),
            Some(&AttrValue::Int(7)),
            "file-backed item count wins"
        );
        assert_eq!(
            file.attrs.get("unsafe"),
            Some(&AttrValue::Int(3)),
            "file-backed unsafe count wins (and is non-zero so it is kept)"
        );
        assert_eq!(
            file.attrs.get("crate"),
            Some(&AttrValue::Str("mycrate".into())),
            "file-backed crate label wins"
        );
    }

    #[test]
    fn strips_cfg_test_module_with_its_attribute() {
        let out = strip(
            "pub fn prod() -> i32 {\n    1\n}\n\n\
             #[cfg(test)]\nmod tests {\n    use super::*;\n    #[test]\n    fn t() { assert_eq!(prod(), 1); }\n}\n",
        );
        assert!(out.contains("pub fn prod"), "production kept: {out}");
        assert!(!out.contains("mod tests"), "test mod removed: {out}");
        assert!(
            !out.contains("#[cfg(test)]"),
            "the cfg attr line removed too: {out}"
        );
        assert!(!out.contains("fn t()"), "test fn removed: {out}");
    }

    #[test]
    fn strips_standalone_test_and_bench_fns() {
        let out = strip("fn prod() {}\n#[test]\nfn it_works() {}\n#[bench]\nfn b(_: &mut ()) {}\n");
        assert!(out.contains("fn prod"));
        assert!(
            !out.contains("it_works") && !out.contains("fn b("),
            "test/bench fns removed: {out}"
        );
    }

    #[test]
    fn keeps_non_test_cfg_and_similarly_named_items() {
        // `cfg(feature = "test")` is a string literal, not a `test` ident; a
        // `mod tests_data` is not gated. Both stay.
        let out = strip("#[cfg(feature = \"test\")]\npub mod gated {}\npub mod tests_data {}\n");
        assert!(out.contains("pub mod gated"), "feature-cfg kept: {out}");
        assert!(
            out.contains("tests_data"),
            "non-gated lookalike kept: {out}"
        );
    }

    #[test]
    fn strips_cfg_all_test_combinations() {
        let out = strip("fn p() {}\n#[cfg(all(test, feature = \"x\"))]\nmod t {}\n");
        assert!(out.contains("fn p"));
        assert!(!out.contains("mod t"), "cfg(all(test,…)) removed: {out}");
    }

    #[test]
    fn unchanged_without_tests_or_on_parse_error() {
        let prod = "pub fn a() {}\n";
        assert_eq!(
            strip_cfg_test(prod.as_bytes()),
            (prod.as_bytes().to_vec(), 0)
        );
        let broken = "@@@ not rust @@@";
        assert_eq!(
            strip_cfg_test(broken.as_bytes()),
            (broken.as_bytes().to_vec(), 0)
        );
    }

    #[test]
    fn tloc_counts_the_whole_removed_test_region() {
        // 4 lines removed: the #[cfg(test)] attr, `mod tests {`, the body line,
        // and the closing `}`.
        let src = "pub fn p() {}\n#[cfg(test)]\nmod tests {\n    fn t() {}\n}\n";
        let (_prod, tloc) = strip_cfg_test(src.as_bytes());
        assert_eq!(tloc, 4);
    }

    fn metric(node: &code_ranker_plugin_api::node::Node, key: &str) -> Option<f64> {
        match node.attrs.get(key) {
            Some(code_ranker_plugin_api::attrs::AttrValue::Int(v)) => Some(*v as f64),
            Some(code_ranker_plugin_api::attrs::AttrValue::Float(v)) => Some(*v),
            _ => None,
        }
    }

    /// Strip inline tests from `src`, run the in-tree Rust engine, write the
    /// metrics onto a fresh file node, and read one metric — the in-process
    /// building block for the metamorphic tests below. Handles `.rs` only.
    fn metric_of(_path: &str, src: &str, key: &str) -> Option<f64> {
        let (prod, tloc) = strip_cfg_test(src.as_bytes());
        let mut m = rust_ts::compute(&prod)?;
        m.tloc = tloc as f64;
        let mut node = code_ranker_plugin_api::node::Node {
            id: "t.rs".into(),
            kind: "file".into(),
            name: "t.rs".into(),
            parent: None,
            attrs: Default::default(),
        };
        code_ranker_graph::write_metrics(&mut node, &m);
        metric(&node, key)
    }

    // ---- Layer 1: metamorphic FP / FN matrix (see docs/metric-correctness.md) --
    //
    // Asserts the AST-Accurate principle across `metric × language × lexical
    // position × direction`: a control-flow / exit keyword appearing only as a
    // look-alike must NOT move the per-function metrics (no false positive); every
    // real construct form MUST be counted (no false negative). Pure in-process
    // parses — ~0 cost against the 20s budget. (LOC / Halstead are intentionally
    // NOT in the keyword-invariance set: a real comment line legitimately changes
    // `cloc`, a string legitimately adds Halstead operands — that is not an FP.)

    /// A Rust function carrying real branching (so all five per-function metrics
    /// are non-zero), with an optional doc-comment prefix and an optional
    /// statement injected into the body. Used to build FP-matrix variants.
    fn rs_src(doc: &str, body_inject: &str) -> String {
        format!(
            "{doc}fn f(a: i32, b: i32) -> i32 {{\n\
             {body_inject}    let g = |x: i32| x + 1;\n\
                 if a > 0 {{ return g(b); }}\n\
                 a + b\n\
             }}\n"
        )
    }

    // Per-language keyword look-alike guard set — the construct keywords/operators
    // a complexity (or `unsafe`) metric can key on. The FP matrix injects these
    // *only* as look-alikes and asserts no metric moves. This mirrors the
    // "Keyword look-alike guard set" in principles/rust/metrics.md, and
    // `rust_trigger_set_documented_in_spec` asserts the spec documents every entry
    // — so the two cannot drift. A superset of the analyzer's real triggers is
    // fine.
    const RUST_TRIGGERS: &[&str] = &[
        "if", "else", "match", "while", "for", "loop", "return", "unsafe", "&&", "||", "?",
    ];

    #[test]
    fn rust_complexity_fp_matrix() {
        // Every lexical position that could smuggle a keyword in as text. None may
        // change cyclomatic / cognitive / exits / args / closures vs the base.
        let base = rs_src("", "");
        let kw = RUST_TRIGGERS.join(" ");
        let positions: &[(&str, String)] = &[
            (
                "line comment",
                rs_src("", &format!("    // {kw} && || ?\n")),
            ),
            (
                "block comment",
                rs_src("", &format!("    /* {kw} && || ? */\n")),
            ),
            ("doc comment", rs_src(&format!("/// {kw}\n"), "")),
            (
                "string",
                rs_src("", &format!("    let _s = \"{kw} && || ?\";\n")),
            ),
            (
                "raw string",
                rs_src("", &format!("    let _r = r#\"{kw} && ||\"#;\n")),
            ),
            (
                "identifier",
                rs_src(
                    "",
                    "    let if_match_return_loop = 0; let _ = if_match_return_loop;\n",
                ),
            ),
            (
                "format string",
                rs_src("", "    let _f = format!(\"if {} while\", a);\n"),
            ),
            (
                "macro body",
                rs_src("", "    let _m = vec![\"if\", \"match\", \"while\"];\n"),
            ),
            (
                "raw identifier",
                rs_src("", "    let r#match = 1; let _ = r#match;\n"),
            ),
        ];
        for key in ["cyclomatic", "cognitive", "exits", "args", "closures"] {
            let want = metric_of("t.rs", &base, key);
            for (pos, src) in positions {
                assert_eq!(
                    metric_of("t.rs", src, key),
                    want,
                    "metric `{key}` moved when a keyword appeared only in: {pos}"
                );
            }
        }
    }

    #[test]
    fn cyclomatic_counts_every_branch_form() {
        // FN guard: every branch form the analyzer recognizes must raise
        // cyclomatic above a branch-free baseline. (Exact per-form increments are
        // the analyzer's rule — layer 4; here we only assert "detected".)
        let baseline =
            metric_of("t.rs", "fn f() -> i32 { 0 }\n", "cyclomatic").expect("baseline cyclomatic");
        let forms: &[(&str, &str)] = &[
            ("if", "fn f(a: i32) -> i32 { if a > 0 { 1 } else { 2 } }\n"),
            (
                "else-if",
                "fn f(a: i32) -> i32 { if a > 0 { 1 } else if a < 0 { 2 } else { 3 } }\n",
            ),
            (
                "match",
                "fn f(a: i32) -> i32 { match a { 0 => 1, _ => 2 } }\n",
            ),
            (
                "while",
                "fn f(mut a: i32) -> i32 { while a > 0 { a -= 1; } a }\n",
            ),
            (
                "for",
                "fn f(a: i32) -> i32 { let mut s = 0; for i in 0..a { s += i; } s }\n",
            ),
            ("loop", "fn f() -> i32 { loop { break; } 0 }\n"),
            (
                "&&",
                "fn f(a: i32, b: i32) -> i32 { let _ = a > 0 && b > 0; 0 }\n",
            ),
            (
                "||",
                "fn f(a: i32, b: i32) -> i32 { let _ = a > 0 || b > 0; 0 }\n",
            ),
            ("?", "fn f() -> Option<i32> { let x = Some(1)?; Some(x) }\n"),
            (
                "if let",
                "fn f() -> i32 { if let Some(x) = Some(1) { x } else { 0 } }\n",
            ),
            (
                "while let",
                "fn f() -> i32 { let mut it = [1].into_iter(); let mut n = 0; while let Some(_) = it.next() { n += 1; } n }\n",
            ),
        ];
        for (name, src) in forms {
            let c = metric_of("t.rs", src, "cyclomatic")
                .unwrap_or_else(|| panic!("cyclomatic missing for `{name}`"));
            assert!(
                c > baseline,
                "branch form `{name}` not counted (cyclomatic {c} <= baseline {baseline})"
            );
        }
        // Magnitude anchor: one extra `if` adds exactly 1.
        let one = metric_of(
            "t.rs",
            "fn f(a: i32) -> i32 { if a > 0 { 1 } else { 2 } }\n",
            "cyclomatic",
        )
        .unwrap();
        let two = metric_of(
            "t.rs",
            "fn f(a: i32) -> i32 { if a > 0 { 1 } else if a < 0 { 2 } else { 3 } }\n",
            "cyclomatic",
        )
        .unwrap();
        assert_eq!(two - one, 1.0, "one extra real `if` must add exactly 1");
    }

    #[test]
    fn rust_complexity_fn_per_metric() {
        // FN guard for the non-cyclomatic per-function metrics: a real construct
        // must surface the metric.
        let cognitive = metric_of(
            "t.rs",
            "fn f(a: i32, b: i32) -> i32 { if a > 0 { if b > 0 { 1 } else { 2 } } else { 3 } }\n",
            "cognitive",
        )
        .expect("cognitive present");
        assert!(cognitive > 0.0, "nested branches must raise cognitive");

        let exits = metric_of("t.rs", "fn f(a: i32) -> i32 { return a; }\n", "exits")
            .expect("exits present");
        assert!(exits >= 1.0, "a real `return` must be counted as an exit");

        let args = metric_of(
            "t.rs",
            "fn f(a: i32, b: i32, c: i32) -> i32 { a + b + c }\n",
            "args",
        )
        .expect("args present");
        assert!(
            args >= 3.0,
            "three parameters must count as >=3 args, got {args}"
        );

        let closures = metric_of(
            "t.rs",
            "fn f() -> i32 { let g = |x: i32| x + 1; g(1) }\n",
            "closures",
        )
        .expect("closures present");
        assert!(closures >= 1.0, "a real closure must be counted");
    }

    #[test]
    fn rust_only_complexity_fp_matrix() {
        // FP invariance for cyclomatic / cognitive, driven by Rust's documented
        // trigger set injected into comment / string positions.
        let check = |path: &str, base: &str, traps: &[String]| {
            for key in ["cyclomatic", "cognitive"] {
                let want = metric_of(path, base, key);
                for trap in traps {
                    assert_eq!(
                        metric_of(path, trap, key),
                        want,
                        "{path} metric `{key}` moved on a keyword look-alike"
                    );
                }
            }
        };

        let kw = RUST_TRIGGERS.join(" ");
        let base = "fn f(a: i32) -> i32 { if a > 0 { 1 } else { 2 } }\n";
        check(
            "t.rs",
            base,
            &[
                format!("// {kw}\n{base}"),
                format!(
                    "fn f(a: i32) -> i32 {{ let _ = \"{kw}\"; if a > 0 {{ 1 }} else {{ 2 }} }}\n"
                ),
            ],
        );
    }

    #[test]
    fn rust_trigger_set_documented_in_spec() {
        // Lock-step guard: every keyword the FP matrix injects must be documented
        // in Rust's metrics spec, so the trigger list and the spec's "Keyword
        // look-alike guard set" cannot drift apart.
        let root = concat!(env!("CARGO_MANIFEST_DIR"), "/../..");
        let path = format!("{root}/principles/rust/metrics.md");
        let spec = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {path}: {e}"));
        for kw in RUST_TRIGGERS {
            assert!(
                spec.contains(&format!("`{kw}`")),
                "trigger `{kw}` is not documented in principles/rust/metrics.md — spec and FP test drifted"
            );
        }
    }

    // ---- Layer 2: generative tests (see docs/metric-correctness.md) ------------
    //
    // Generate programs with a KNOWN construct count, then assert the metric
    // equals ground truth across a combinatorial grid. Deterministic (no random
    // dependency, no flakiness) — proptest-style randomized fuzz is a later
    // nightly extension. Still pure in-process parses; the whole grid is ~ms.

    /// A Rust function with `noise` keyword-laden look-alike lines (a comment plus
    /// a string binding, neither a real construct) followed by `branches` real,
    /// independent `if` statements (each adds exactly 1 to cyclomatic).
    fn gen_rs(branches: usize, noise: usize) -> String {
        let mut body = String::new();
        for i in 0..noise {
            body.push_str(&format!(
                "    // if match while for loop return && || ? noise {i}\n"
            ));
            body.push_str(&format!(
                "    let _n{i} = \"if match while return && ||\";\n"
            ));
        }
        for i in 0..branches {
            body.push_str(&format!("    if x > {i} {{ let _ = {i}; }}\n"));
        }
        format!("fn f(x: i32) -> i32 {{\n{body}    0\n}}\n")
    }

    #[test]
    fn generative_cyclomatic_counts_branches_not_noise() {
        // Ground truth by construction: cyclomatic = baseline + (real `if` count),
        // independent of how many keyword look-alike lines surround it. Sweeps an
        // 8×8 grid of (branches, noise) — 64 generated programs.
        for noise in 0..8 {
            let base =
                metric_of("t.rs", &gen_rs(0, noise), "cyclomatic").expect("cyclomatic present");
            for branches in 0..8 {
                let cyc = metric_of("t.rs", &gen_rs(branches, noise), "cyclomatic")
                    .expect("cyclomatic present");
                assert_eq!(
                    cyc,
                    base + branches as f64,
                    "cyclomatic must add exactly 1 per real `if` and 0 per noise line \
                     (branches={branches}, noise={noise})"
                );
            }
        }
    }

    #[test]
    fn generative_complexity_invariant_to_noise() {
        // A fixed real structure (2 args, a closure, a branch, a `return`) with a
        // growing pile of keyword look-alikes around it. Every per-function metric
        // must stay exactly at its noise-free value — no false positive at any
        // noise level.
        let mk = |noise: usize| -> String {
            let mut body = String::new();
            for i in 0..noise {
                body.push_str(&format!("    // if match return unsafe && || {i}\n"));
                body.push_str(&format!("    let _n{i} = \"if match return && ||\";\n"));
            }
            format!(
                "fn f(a: i32, b: i32) -> i32 {{\n\
                 {body}    let g = |x: i32| x + 1;\n\
                     if a > 0 {{ return g(b); }}\n\
                     a + b\n\
                 }}\n"
            )
        };
        for key in ["cyclomatic", "cognitive", "exits", "args", "closures"] {
            let want = metric_of("t.rs", &mk(0), key);
            for noise in 1..10 {
                assert_eq!(
                    metric_of("t.rs", &mk(noise), key),
                    want,
                    "metric `{key}` moved at noise={noise} — keyword look-alikes leaked in"
                );
            }
        }
    }

    #[test]
    fn per_function_metrics_aggregate_over_child_functions() {
        // Regression for the whole "root-vs-sum" class: `write_metrics` once read
        // the ROOT space value for `cyclomatic` / `cognitive` / `exits` / `args` /
        // `closures`, which for a file is the vacuous root count (0, or 1 for
        // cyclomatic) — every file looked identical. The real signal lives in the
        // child function spaces, so each must be the SUM over them.
        //
        // `a` takes 2 args, nests two `if`s, and `return`s; `b` defines a 1-arg
        // closure. So the file must surface: cyclomatic (summed branches), a
        // non-zero cognitive (nesting), exits (the `return`), args (2 fn + 1
        // closure = 3), and closures (1).
        let src = "fn a(x: i32, y: i32) -> i32 { if x > 0 { if x > 1 { return x; } y } else { 3 } }\n\
                   fn b() -> i32 { let f = |z: i32| z + 1; f(2) }\n";
        // Each is summed over the child functions — well above the vacuous root
        // value, proving aggregation rather than a root-only read.
        let cyc = metric_of("t.rs", src, "cyclomatic").expect("cyclomatic present");
        assert!(cyc > 1.0, "cyclomatic should be summed, got {cyc}");
        let cog = metric_of("t.rs", src, "cognitive").expect("cognitive present");
        assert!(cog > 0.0, "cognitive should be summed, got {cog}");
        let exits = metric_of("t.rs", src, "exits").expect("exits present");
        assert!(exits >= 1.0, "exits should count the `return`, got {exits}");
        let args = metric_of("t.rs", src, "args").expect("args present");
        assert!(
            args >= 3.0,
            "args should sum fn (2) + closure (1), got {args}"
        );
        let closures = metric_of("t.rs", src, "closures").expect("closures present");
        assert!(
            closures >= 1.0,
            "closures should count the closure, got {closures}"
        );
    }

    // ---- Layer 3: asserted anchors (see docs/metric-correctness.md) -----------
    //
    // Layers 1 & 2 prove RELATIVE behaviour (noise-invariance, +1 per construct)
    // but never pin an ABSOLUTE value, so a uniform offset/scale bug (every count
    // shifted by +1, or doubled) would pass green. These anchors pin exact values
    // hand-derived from principles/rust/metrics.md, catching that scale class.

    #[test]
    fn complexity_absolute_anchors_hand_derived() {
        // Integer counting metrics, pinned to EXACT file-level values, hand-derived
        // from the spec's rules (metrics.md §cyclomatic / §exits,args,closures).
        //
        // These pin the analyzer-of-record's whole-file values (what we emit):
        //   • `cyclomatic` = the file unit's base path (1) + Σ over functions of
        //     (1 + branch points). Per-function McCabe (`V(G)=E−N+2P` = Σ over
        //     functions) is the theory; the analyzer adds the file unit on top and
        //     we emit it verbatim (it is also the value `mi` is computed from).
        //     `classify` = file 1 + fn 4 (base1+if+else-if+||) = 5.
        //   • `exits` = Σ over functions of (a value-returning `-> T` exit +
        //     explicit return/?). "Exit points" has no canonical theory, so the
        //     analyzer's rule is the source of truth (metrics.md §exits). The
        //     `-> i32` snippets below read 2 (the explicit return + the `-> T` exit).
        //   • `args` / `closures` / `cognitive` have no file-unit offset.
        // All pinned so any drift from the analyzer's output is caught.
        let classify = "fn classify(n: i32) -> &'static str {\n\
            \x20   if n < 0 { \"neg\" } else if n == 0 || n == 1 { \"small\" } else { \"big\" }\n\
            }\n";
        let two_closures =
            "fn f() { let g = |x: i32| x + 1; let h = |y: i32| y; let _ = (g, h); }\n";
        // (label, path, src, key, exact_expected)
        let cases: &[(&str, &str, &str, &str, f64)] = &[
            // file unit 1 + fn(base1 + if + else-if + ||) = 1 + 4 = 5.
            ("classify", "t.rs", classify, "cyclomatic", 5.0),
            // file unit 1 + fn(base1 + 1 if) = 1 + 2 = 3 (else is free).
            (
                "single if",
                "t.rs",
                "fn f(a: i32) -> i32 { if a > 0 { 1 } else { 2 } }\n",
                "cyclomatic",
                3.0,
            ),
            // 1 explicit return + 1 value-returning exit (`-> i32`) → 2.
            (
                "one return",
                "t.rs",
                "fn f() -> i32 { return 1; }\n",
                "exits",
                2.0,
            ),
            // 1 `?` + 1 value-returning exit (`-> Option`) → 2.
            (
                "one try op",
                "t.rs",
                "fn f() -> Option<i32> { let x = Some(1)?; Some(x) }\n",
                "exits",
                2.0,
            ),
            (
                "three params",
                "t.rs",
                "fn f(a: i32, b: i32, c: i32) -> i32 { a + b + c }\n",
                "args",
                3.0,
            ),
            ("two closures", "t.rs", two_closures, "closures", 2.0),
            ("two closure args", "t.rs", two_closures, "args", 2.0),
        ];
        let mut fails = Vec::new();
        for (label, path, src, key, want) in cases {
            match metric_of(path, src, key) {
                Some(got) if got == *want => {}
                other => fails.push(format!("{label}: {key} want {want}, got {other:?}")),
            }
        }
        assert!(
            fails.is_empty(),
            "failing integer anchors:\n{}",
            fails.join("\n")
        );
    }

    #[test]
    fn complexity_frozen_scale_anchors() {
        // Algorithm-specific metrics (cognitive nesting weights, Halstead
        // dictionaries, MI) cannot be hand-derived reliably, so they are FROZEN
        // anchors: values produced by `rust-code-analysis` for one fixed snippet,
        // verified once. Their job is to catch a uniform offset/scale regression
        // (a library bump that doubles `volume`, an MI formula edit) — not to
        // claim an independent ground truth. They change only when the underlying
        // algorithm changes, and that change should be deliberate.
        let classify = "fn classify(n: i32) -> &'static str {\n\
            \x20   if n < 0 { \"neg\" } else if n == 0 || n == 1 { \"small\" } else { \"big\" }\n\
            }\n";
        // (key, expected, abs_tolerance)
        let cases: &[(&str, f64, f64)] = &[
            ("cognitive", 4.0, 0.0),   // exact integer
            ("vocabulary", 18.0, 0.0), // η₁ + η₂, exact integer
            ("length", 28.0, 0.0),     // N₁ + N₂, exact integer
            ("volume", 116.757, 0.01), // length × log₂(vocabulary)
            ("effort", 875.684, 0.01), // difficulty × volume
            ("mi", 127.299, 0.01),     // maintainability index
            ("mi_sei", 108.463, 0.01), // SEI variant
        ];
        let mut fails = Vec::new();
        for (key, want, tol) in cases {
            match metric_of("t.rs", classify, key) {
                Some(got) if (got - *want).abs() <= *tol => {}
                other => fails.push(format!("{key}: want {want}{tol}), got {other:?}")),
            }
        }
        assert!(
            fails.is_empty(),
            "failing scale anchors:\n{}",
            fails.join("\n")
        );
    }

    #[test]
    fn declaration_only_file_emits_no_complexity() {
        // No functions → only the file unit space → cyclomatic is a vacuous 1 and
        // cognitive is 0. Both must be dropped (not shown as a meaningless "1"),
        // matching how `put` already drops cognitive's 0. Mirrors real files like
        // a clap CLI model or a type-definitions module.
        let src = "pub struct Cli { pub verbose: bool }\n\
                   pub enum Mode { A, B }\n";
        assert_eq!(
            metric_of("t.rs", src, "cyclomatic"),
            None,
            "a declaration-only file must not emit a vacuous cyclomatic"
        );
        assert_eq!(
            metric_of("t.rs", src, "cognitive"),
            None,
            "a declaration-only file must not emit cognitive"
        );
    }

    #[test]
    fn metric_specs_override_adds_rust_cfg_test_note() {
        // The neutral default descriptions carry no language nuance; the Rust
        // plugin re-adds the `#[cfg(test)]` LOC-exclusion note for sloc/lloc/
        // cloc/blank — so it appears only in Rust snapshots, never in py/js/ts.
        let defaults = code_ranker_graph::metric_specs().0;
        // sanity: the shared default is language-neutral
        assert!(
            !defaults["blank"]
                .description
                .as_deref()
                .unwrap_or("")
                .contains("#[cfg(test)]"),
            "the shared default must stay language-neutral"
        );

        let refined = RustPlugin.metric_specs(defaults);
        for key in ["sloc", "lloc", "cloc", "blank"] {
            let desc = refined[key].description.as_deref().unwrap_or("");
            assert!(
                desc.contains("#[cfg(test)]"),
                "Rust `{key}` description should note the cfg(test) exclusion"
            );
        }
    }
}