rust-doctor 0.6.0

Local-first health audit for Cargo workspaces: curated Clippy lints and native detectors, scored out of 100
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
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::fmt::Write as _;

use serde::ser::{Error as _, SerializeStruct};
use serde::{Deserialize, Serialize, Serializer};

use cargo_metadata::Metadata;

use crate::execution::ScanExecution;
use crate::policy::{CorpusMeasurement, RuleTier, UNMEASURED_NOISE_BASIS_POINTS};
use crate::report::{Diagnostic, Severity, Status};
use crate::source_kernel::SourceMeasurement;

mod density;
mod source_inventory;

use density::{DensityScope, Scale, calculate_dimensions, contributions};

/// The workspace the score is computed against: how much of it there is, and
/// whether that is the workspace or a floor on it.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct SourceFileInventory {
    pub(crate) files: usize,
    /// Lines of production Rust the walk counted. Test code is excluded, since
    /// the score charges for what ships.
    pub(crate) production_lines: usize,
    pub(crate) complete: bool,
}

pub(crate) fn source_file_inventory(
    metadata: &Metadata,
    scan: Option<&ScanExecution>,
    measurement: Option<&SourceMeasurement>,
) -> SourceFileInventory {
    source_inventory::collect(metadata, scan, measurement)
}

pub const SCORE_MODEL: &str = "core-v3";
const SHARE_BASE_URL: &str = "https://rust-doctor.com/share";
const MAX_SHARED_COUNT: usize = 1_000_000;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Audit {
    pub source_files: usize,
    /// Lines of production Rust the scan counted, test code excluded. Zero when
    /// the workspace holds none, and a floor on it whenever
    /// `inventory_is_complete` is false.
    pub production_lines: usize,
    pub categories: Vec<AuditCategory>,
    pub score: Option<AuditScore>,
    /// Whether `source_files` counted the workspace or is a floor on it.
    ///
    /// Rebuilding the block for a narrower scope needs this fact and nothing else about the
    /// original scan, and the block used to recover it from `score.authoritative`, which also
    /// carries the status and whether every diagnostic was catalogued. One uncatalogued rule
    /// anywhere therefore made every later scope non-authoritative, for a reason that had
    /// nothing to do with the inventory. It stays private because it is not published: the
    /// wire shape is the three members above.
    inventory_is_complete: bool,
}

/// Per-severity count of a single quantity.
///
/// The report publishes two distinct quantities: the number of distinct
/// diagnostics and the number of occurrences. Every surface exposes both under
/// explicit names, and `total` is always the sum of the four severities.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct SeverityCounts {
    pub errors: usize,
    pub warnings: usize,
    pub info: usize,
    pub unknown: usize,
    pub total: usize,
}

impl SeverityCounts {
    pub(crate) fn add(&mut self, severity: Severity, count: usize) {
        let bucket = match severity {
            Severity::Error => &mut self.errors,
            Severity::Warning => &mut self.warnings,
            Severity::Info => &mut self.info,
            Severity::Unknown => &mut self.unknown,
        };
        *bucket = bucket.saturating_add(count);
        self.total = self.total.saturating_add(count);
    }

    /// Adds every bucket of another count into this one.
    fn merge(&mut self, other: Self) {
        self.errors = self.errors.saturating_add(other.errors);
        self.warnings = self.warnings.saturating_add(other.warnings);
        self.info = self.info.saturating_add(other.info);
        self.unknown = self.unknown.saturating_add(other.unknown);
        self.total = self.total.saturating_add(other.total);
    }

    const fn is_coherent(self) -> bool {
        self.errors
            .saturating_add(self.warnings)
            .saturating_add(self.info)
            .saturating_add(self.unknown)
            == self.total
    }

    /// At least one occurrence per distinct diagnostic, never the reverse.
    const fn covers(self, distinct: Self) -> bool {
        self.errors >= distinct.errors
            && self.warnings >= distinct.warnings
            && self.info >= distinct.info
            && self.unknown >= distinct.unknown
            && self.total >= distinct.total
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuditCategory {
    pub name: AuditCategoryName,
    pub distinct: SeverityCounts,
    pub occurrences: SeverityCounts,
}

/// The categories the report publishes, and the order it publishes them in.
///
/// The declaration order is that order: `Ord` derives from it, the tally map is keyed by it, and
/// `Audit::is_valid` checks it. A second list restating the same sequence is a second place for
/// it to be wrong, which is why there is none.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub enum AuditCategoryName {
    Security,
    Bugs,
    Performance,
    Dependencies,
    Maintainability,
    /// Diagnostic with no catalog category, a compilation error for instance.
    /// The bucket exists so that no diagnostic disappears between `summary`
    /// and `audit.categories`.
    Other,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AuditScore {
    pub model: String,
    pub value: u8,
    pub label: ScoreLabel,
    pub authoritative: bool,
    pub dimensions: ScoreDimensions,
    /// Worst tier observed across all dimensions, or `null` when no scored
    /// rule is catalogued.
    pub worst_tier: Option<RuleTier>,
    /// Global cap effectively applied to `value`, or `null` when the worst tier
    /// imposes none. Published so that a score drop can be explained without
    /// recomputation.
    pub applied_ceiling: Option<u8>,
    pub projected_after_top_three: Option<u8>,
    pub projected_rule_ids: Vec<String>,
    /// Rules that fired here and that the corpus adjudicated at no true
    /// positive, in descending cost order, hence absent from
    /// `projected_rule_ids` whatever their volume.
    ///
    /// Published so the omission reads as a measurement rather than a defect:
    /// the rule with the most findings is often the one the corpus found most
    /// often wrong, and a list that silently drops it is impossible to trust.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub withheld_rule_ids: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct ScoreDimensions {
    pub security: u8,
    pub reliability: u8,
    pub maintainability: u8,
    pub performance: u8,
    pub dependencies: u8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ScoreLabel {
    Great,
    #[serde(rename = "Needs work")]
    NeedsWork,
    Critical,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShareError {
    ScoreUnavailable,
    NonAuthoritative,
    InvalidPayload,
}

impl fmt::Display for ShareError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::ScoreUnavailable => "the audit does not contain a score",
            Self::NonAuthoritative => "the audit score is not authoritative",
            Self::InvalidPayload => "the audit exceeds the public share bounds",
        })
    }
}

impl std::error::Error for ShareError {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum ScoreDimension {
    Security,
    Reliability,
    Maintainability,
    Performance,
    Dependencies,
}

impl ScoreDimension {
    /// Every dimension, in the order the report publishes them.
    ///
    /// This is the one list the scoring walks: `every_dimension_is_listed_once` stops compiling
    /// when a dimension is declared and forgotten here.
    const ALL: [Self; 5] = [
        Self::Security,
        Self::Reliability,
        Self::Maintainability,
        Self::Performance,
        Self::Dependencies,
    ];
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RuleAggregate {
    pub(crate) id: String,
    pub(crate) effective_severity: Severity,
    pub(crate) category: Option<AuditCategoryName>,
    dimension: Option<ScoreDimension>,
    /// What this rule's findings are counted against, read off the catalog's `producer` field.
    scope: DensityScope,
    tier: Option<RuleTier>,
    /// Every occurrence the report publishes for this rule, which is what the reader is shown.
    pub(crate) occurrences: usize,
    /// What this rule adds to its dimension's numerator: its distinct scored sites, each
    /// weighted by its severity. A site is one diagnostic, so a clone family naming K members
    /// through `related` weighs one and not K, and the diagnostics outside production code are
    /// counted in `occurrences` and weigh nothing here.
    numerator: u64,
    /// Smoothed false-positive rate the pinned corpus adjudicated, in basis
    /// points, or `None` for a rule it never adjudicated.
    /// It ranks what to repair first and enters no penalty: what a rule costs
    /// the score is what it reported here, whatever it costs elsewhere.
    noise: Option<u16>,
    /// Millionths of a point of weighted score that removing this rule's sites recovers.
    ///
    /// It is stored rather than computed on demand because under core-v3 it is not a property of
    /// the rule: the same numerator is worth different points depending on the whole set of rules
    /// around it, the tier ceilings they impose and the scale they are divided by. So it is
    /// computed once, where all three are known, and read from there by everything that ranks.
    contribution: u64,
}

#[derive(Debug)]
pub(crate) struct RuleAggregation {
    pub(crate) rules: Vec<RuleAggregate>,
    scale: Scale,
    diagnostics_are_authoritative: bool,
}

#[derive(Debug)]
struct PendingRule {
    id: String,
    severity: Severity,
    category: Option<AuditCategoryName>,
    dimension: Option<ScoreDimension>,
    scope: DensityScope,
    mapping_conflict: bool,
    tier: Option<RuleTier>,
    occurrences: usize,
    numerator: u64,
    noise: Option<u16>,
}

impl Audit {
    pub fn build(
        source_files: usize,
        production_lines: usize,
        status: Status,
        diagnostics: &[Diagnostic],
    ) -> Self {
        Self::build_with_inventory(
            SourceFileInventory {
                files: source_files,
                production_lines,
                complete: true,
            },
            status,
            diagnostics,
        )
    }

    pub(crate) fn build_from_inventory(
        inventory: SourceFileInventory,
        status: Status,
        diagnostics: &[Diagnostic],
    ) -> Self {
        Self::build_with_inventory(inventory, status, diagnostics)
    }

    fn build_with_inventory(
        inventory: SourceFileInventory,
        status: Status,
        diagnostics: &[Diagnostic],
    ) -> Self {
        let SourceFileInventory {
            files: source_files,
            production_lines,
            complete: inventory_is_complete,
        } = inventory;
        // Both quantities count everything the report publishes, FR-06 requires them to equal
        // `summary`. What the score sets aside is decided inside `aggregate_rules`, so the two
        // callers of it cannot disagree on the population.
        let categories = category_tallies(diagnostics);
        let aggregation = aggregate_rules(production_lines, diagnostics.iter());
        let scan_is_complete = status == Status::Complete && inventory_is_complete;
        // No source and no line are the same refusal: core-v3 scores a density, and a density
        // over nothing is not a hundred out of a hundred, it is an answer the scan cannot give.
        let score = is_scorable_workspace(source_files, production_lines)
            .then(|| score(&aggregation, scan_is_complete));
        Self {
            source_files,
            production_lines,
            categories,
            score,
            inventory_is_complete,
        }
    }

    pub(crate) fn rebuild_for_scope(&self, status: Status, diagnostics: &[Diagnostic]) -> Self {
        Self::build_with_inventory(
            SourceFileInventory {
                files: self.source_files,
                production_lines: self.production_lines,
                complete: self.inventory_is_complete,
            },
            status,
            diagnostics,
        )
    }

    pub fn share_url(&self) -> Result<String, ShareError> {
        let score = self.score.as_ref().ok_or(ShareError::ScoreUnavailable)?;
        if !score.authoritative {
            return Err(ShareError::NonAuthoritative);
        }
        if !self.is_valid() {
            return Err(ShareError::InvalidPayload);
        }

        // The counts the block already publishes, not a third summation over the same
        // categories that nothing kept in step with the first two.
        let (_, occurrences) = self.totals();
        build_share_url(
            score.value,
            occurrences.errors,
            occurrences.warnings,
            occurrences.info,
            self.source_files,
            self.production_lines,
        )
    }

    pub fn is_valid(&self) -> bool {
        let mut previous = None;
        let categories_are_valid = self.categories.iter().all(|category| {
            let ordered = previous.is_none_or(|previous| previous < category.name);
            previous = Some(category.name);
            ordered && category.is_valid()
        });
        categories_are_valid
            && is_scorable_workspace(self.source_files, self.production_lines)
                == self.score.is_some()
            && self.score.as_ref().is_none_or(AuditScore::is_valid)
    }

    /// Both quantities aggregated over every category of the block.
    pub fn totals(&self) -> (SeverityCounts, SeverityCounts) {
        self.categories.iter().fold(
            (SeverityCounts::default(), SeverityCounts::default()),
            |(mut distinct, mut occurrences), category| {
                distinct.merge(category.distinct);
                occurrences.merge(category.occurrences);
                (distinct, occurrences)
            },
        )
    }
}

impl Serialize for Audit {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if !self.is_valid() {
            return Err(S::Error::custom("invalid audit state"));
        }
        let mut state = serializer.serialize_struct("Audit", 4)?;
        state.serialize_field("source_files", &self.source_files)?;
        state.serialize_field("production_lines", &self.production_lines)?;
        state.serialize_field("categories", &self.categories)?;
        state.serialize_field("score", &self.score)?;
        state.end()
    }
}

impl AuditCategory {
    fn empty(name: AuditCategoryName) -> Self {
        Self {
            name,
            distinct: SeverityCounts::default(),
            occurrences: SeverityCounts::default(),
        }
    }

    fn is_valid(&self) -> bool {
        self.distinct.total > 0
            && self.distinct.is_coherent()
            && self.occurrences.is_coherent()
            && self.occurrences.covers(self.distinct)
    }
}

impl Serialize for AuditCategory {
    /// The four bare severity members are the schema-v7 spelling of `occurrences`, projected
    /// from it rather than stored beside it.
    ///
    /// Held as fields, they were a second copy of one fact: a recopy pass wrote them, four
    /// clauses of `is_valid` checked they still agreed, and `share_url` summed the copy while
    /// `totals` summed the original. A projection cannot disagree with its source.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("AuditCategory", 7)?;
        state.serialize_field("name", &self.name)?;
        state.serialize_field("errors", &self.occurrences.errors)?;
        state.serialize_field("warnings", &self.occurrences.warnings)?;
        state.serialize_field("info", &self.occurrences.info)?;
        state.serialize_field("unknown", &self.occurrences.unknown)?;
        state.serialize_field("distinct", &self.distinct)?;
        state.serialize_field("occurrences", &self.occurrences)?;
        state.end()
    }
}

impl AuditScore {
    /// Whether every published member still follows from the others.
    ///
    /// `Audit`'s `Serialize` refuses a block that fails this, so each clause is the one thing it
    /// names rather than a term in one long conjunction: when it refuses, the caller can be told
    /// which fact stopped holding.
    pub fn is_valid(&self) -> bool {
        if self.model != SCORE_MODEL || self.value > 100 {
            return false;
        }
        if self.applied_ceiling != self.worst_tier.and_then(tier_overall_ceiling) {
            return false;
        }
        if self.value != capped(weighted_score(self.dimensions), self.applied_ceiling) {
            return false;
        }
        if self.label != score_label(self.value) {
            return false;
        }
        if self.dimensions.values().into_iter().any(|value| value > 100) {
            return false;
        }
        // Repairing the top three can only raise the score, never lower it.
        if self
            .projected_after_top_three
            .is_some_and(|projected| projected > 100 || projected < self.value)
        {
            return false;
        }
        if !self.projected_rules_are_named_once() {
            return false;
        }
        // An authoritative score names what to fix and what fixing it would be worth, or names
        // neither. A non-authoritative one names nothing at all.
        if self.authoritative {
            matches!(
                (
                    self.projected_after_top_three,
                    self.projected_rule_ids.is_empty()
                ),
                (None, true) | (Some(_), false)
            )
        } else {
            self.projected_after_top_three.is_none() && self.projected_rule_ids.is_empty()
        }
    }

    /// At most three rules, none of them empty and none of them named twice.
    fn projected_rules_are_named_once(&self) -> bool {
        let distinct: BTreeSet<_> = self.projected_rule_ids.iter().collect();
        self.projected_rule_ids.len() <= 3
            && distinct.len() == self.projected_rule_ids.len()
            && self.projected_rule_ids.iter().all(|rule| !rule.is_empty())
    }
}

impl ScoreDimensions {
    /// The five dimensions, each scored by the same function.
    ///
    /// This and `value_for` are the two halves of the one mapping between a dimension and the
    /// field the report publishes it under. Everything else walks `ScoreDimension::ALL`.
    fn from_fn(mut score_for: impl FnMut(ScoreDimension) -> u8) -> Self {
        Self {
            security: score_for(ScoreDimension::Security),
            reliability: score_for(ScoreDimension::Reliability),
            maintainability: score_for(ScoreDimension::Maintainability),
            performance: score_for(ScoreDimension::Performance),
            dependencies: score_for(ScoreDimension::Dependencies),
        }
    }

    fn values(self) -> [u8; 5] {
        ScoreDimension::ALL.map(|dimension| self.value_for(dimension))
    }

    fn value_for(self, dimension: ScoreDimension) -> u8 {
        match dimension {
            ScoreDimension::Security => self.security,
            ScoreDimension::Reliability => self.reliability,
            ScoreDimension::Maintainability => self.maintainability,
            ScoreDimension::Performance => self.performance,
            ScoreDimension::Dependencies => self.dependencies,
        }
    }
}

impl ScoreLabel {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Great => "Great",
            Self::NeedsWork => "Needs work",
            Self::Critical => "Critical",
        }
    }
}

impl fmt::Display for ScoreLabel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl AuditCategoryName {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Security => "Security",
            Self::Bugs => "Bugs",
            Self::Performance => "Performance",
            Self::Dependencies => "Dependencies",
            Self::Maintainability => "Maintainability",
            Self::Other => "Other",
        }
    }
}

impl fmt::Display for AuditCategoryName {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

pub(crate) fn category_mapping(category: &str) -> Option<(AuditCategoryName, ScoreDimension)> {
    match category {
        "security" => Some((AuditCategoryName::Security, ScoreDimension::Security)),
        "correctness" | "reliability" => {
            Some((AuditCategoryName::Bugs, ScoreDimension::Reliability))
        }
        "performance" => Some((AuditCategoryName::Performance, ScoreDimension::Performance)),
        "cargo" | "dependencies" => Some((
            AuditCategoryName::Dependencies,
            ScoreDimension::Dependencies,
        )),
        "maintainability" => Some((
            AuditCategoryName::Maintainability,
            ScoreDimension::Maintainability,
        )),
        _ => None,
    }
}

/// Every diagnostic falls into exactly one bucket, including those no catalog
/// category covers. Without that, `summary` and `audit.categories` would count
/// different populations.
fn category_bucket(category: Option<&str>) -> AuditCategoryName {
    category
        .and_then(category_mapping)
        .map_or(AuditCategoryName::Other, |(name, _)| name)
}

/// Cap imposed on a dimension by the worst tier it contains.
const fn tier_dimension_ceiling(tier: RuleTier) -> Option<u8> {
    match tier {
        RuleTier::P0 => Some(20),
        RuleTier::P1 => Some(50),
        RuleTier::P2 => Some(75),
        RuleTier::P3 => None,
    }
}

/// Cap imposed on the overall score by the worst tier across all dimensions.
const fn tier_overall_ceiling(tier: RuleTier) -> Option<u8> {
    match tier {
        RuleTier::P0 => Some(40),
        RuleTier::P1 => Some(65),
        RuleTier::P2 | RuleTier::P3 => None,
    }
}

/// Full scale of an adjudicated rate, matching the basis points the corpus
/// publishes.
const BASIS_POINTS: u64 = 10_000;

/// Whether a workspace holds enough for a score to mean anything.
///
/// One condition rather than two places that have to agree: `build_with_inventory` gates the
/// score on it and `is_valid` checks the gate held, so a block carrying a score over no source
/// refuses to serialize instead of publishing a hundred over nothing.
const fn is_scorable_workspace(source_files: usize, production_lines: usize) -> bool {
    source_files > 0 && production_lines > 0
}

const fn capped(value: u8, ceiling: Option<u8>) -> u8 {
    match ceiling {
        Some(ceiling) if ceiling < value => ceiling,
        _ => value,
    }
}

/// The worst tier is the minimum, `P0` being declared first.
fn worse_tier(current: Option<RuleTier>, candidate: Option<RuleTier>) -> Option<RuleTier> {
    match (current, candidate) {
        (Some(current), Some(candidate)) => Some(current.min(candidate)),
        (current, candidate) => current.or(candidate),
    }
}

const fn dimension_weight_twice(dimension: ScoreDimension) -> u64 {
    match dimension {
        ScoreDimension::Security => 4,
        ScoreDimension::Reliability => 3,
        ScoreDimension::Maintainability
        | ScoreDimension::Performance
        | ScoreDimension::Dependencies => 2,
    }
}

impl RuleAggregate {
    /// What removing this rule's sites recovers, in millionths of a point of weighted score.
    ///
    /// Under core-v2 this was the rule's own penalty, which is why it could be recomputed from
    /// the rule alone. Under core-v3 the penalty is a density read through a curve, so what a
    /// rule costs depends on everything sharing its dimension: the same site is worth more on a
    /// clean dimension than on a saturated one. The number is therefore the answer to the
    /// reader's actual question, the points the score gives back, and it is computed once by
    /// `aggregate_rules` where the whole set and the scale are both known.
    pub(crate) const fn contribution(&self) -> u64 {
        self.contribution
    }

    /// What repairing this rule is expected to be worth, which is what it costs
    /// the score discounted by how often the corpus found it wrong.
    ///
    /// A rule the corpus adjudicated wrong on nearly every site it showed is
    /// expected to be worth little to repair, whatever its volume, and volume
    /// is exactly what the noisiest rules have the most of. Ranking by
    /// contribution alone told the user to fix the rule that fires most, which
    /// is not the same question and, on a rule measured wrong almost
    /// everywhere, is advice to change correct code.
    ///
    /// An unmeasured rule is discounted at `UNMEASURED_NOISE_BASIS_POINTS`, the
    /// middle of the interval, rather than kept whole. No measurement is not
    /// evidence of correctness either, and the corpus has adjudicated 24 of the
    /// 62 catalogued rules: keeping the other 38 undiscounted put every one of
    /// them ahead of every rule the corpus ever confirmed, so the ranking read
    /// as a list of what nobody has checked. It is the same smoothing at no
    /// observations rather than a threshold of its own, so the first site the
    /// corpus adjudicates moves the rule off the middle in whichever direction
    /// it was adjudicated.
    pub(crate) fn expected_repair_value(&self) -> u64 {
        let noise = self.noise.unwrap_or(UNMEASURED_NOISE_BASIS_POINTS);
        let kept = BASIS_POINTS.saturating_sub(noise as u64);
        self.contribution().saturating_mul(kept) / BASIS_POINTS
    }

    /// A non-scorable rule caps nothing: a tier cannot act without a retained
    /// dimension and severity.
    const fn scoring_tier(&self) -> Option<RuleTier> {
        if self.is_scorable() { self.tier } else { None }
    }

    const fn is_scorable(&self) -> bool {
        self.numerator > 0 && self.dimension.is_some()
    }
}

/// One tally per category that fired, in the order the report publishes them.
///
/// The map is keyed by the category itself, whose `Ord` is its declaration order, so it already
/// comes out ordered: sorting it a second time against a hand-written list is how the two orders
/// used to be able to disagree.
fn category_tallies(diagnostics: &[Diagnostic]) -> Vec<AuditCategory> {
    let mut tallies = BTreeMap::<AuditCategoryName, AuditCategory>::new();
    for diagnostic in diagnostics {
        let name = category_bucket(diagnostic.category.as_deref());
        let tally = tallies
            .entry(name)
            .or_insert_with(|| AuditCategory::empty(name));
        tally.distinct.add(diagnostic.severity, 1);
        tally
            .occurrences
            .add(diagnostic.severity, diagnostic.occurrences);
    }

    tallies.into_values().collect()
}

fn score(aggregation: &RuleAggregation, scan_complete: bool) -> AuditScore {
    let scored = ScoredState::of(&aggregation.rules, &BTreeSet::new(), aggregation.scale);
    let authoritative = scan_complete && aggregation.diagnostics_are_authoritative;
    let (projected, withheld) = rank_repairs(&aggregation.rules);
    let projected_rule_ids: Vec<String> = projected
        .iter()
        .take(3)
        .map(|rule| rule.id.clone())
        .collect();

    // A ranking is only worth as much as the set of diagnostics it ranked, and that set is
    // exactly what a scan that did not complete cannot vouch for. So the projection, the rules
    // it names and the rules it withheld are published together or not at all.
    let (projected_rule_ids, withheld_rule_ids, projected_after_top_three) = if authoritative {
        let after = (!projected_rule_ids.is_empty()).then(|| {
            let removed: BTreeSet<_> = projected_rule_ids.iter().cloned().collect();
            ScoredState::of(&aggregation.rules, &removed, aggregation.scale).value
        });
        let withheld_rule_ids = withheld.iter().map(|rule| rule.id.clone()).collect();
        (projected_rule_ids, withheld_rule_ids, after)
    } else {
        (Vec::new(), Vec::new(), None)
    };

    AuditScore {
        model: SCORE_MODEL.to_owned(),
        value: scored.value,
        label: score_label(scored.value),
        authoritative,
        dimensions: scored.dimensions,
        worst_tier: scored.worst_tier,
        applied_ceiling: scored.applied_ceiling,
        projected_after_top_three,
        projected_rule_ids,
        withheld_rule_ids,
    }
}

/// The rules that cost the score anything, split by whether repairing them is worth something,
/// each half ordered by what it is worth to the reader.
///
/// The two halves are one question asked once. A rule whose expected repair value rounds away is
/// expected to be worth nothing to repair, whatever its volume, and volume is exactly what the
/// noisiest rules have the most of: it is withheld rather than ranked last, because naming it
/// would still be telling the reader to go and change correct code. It is published all the same,
/// loudest first, so its absence from the projection reads as a measurement and not as a defect.
///
/// The threshold is unchanged by the smoothing and the partition it produces is not. No smoothed
/// rate reaches ten thousand basis points, so a rule adjudicated wrong on every site it showed
/// keeps a small share of its contribution and rejoins the projection unless that share rounds
/// away. That is the intended reading: forty sites all adjudicated wrong is strong evidence and
/// still not proof, and the sample it rests on is printed beside it wherever the report names it.
fn rank_repairs(rules: &[RuleAggregate]) -> (Vec<&RuleAggregate>, Vec<&RuleAggregate>) {
    let (mut projected, mut withheld): (Vec<_>, Vec<_>) = rules
        .iter()
        .filter(|rule| rule.is_scorable() && rule.contribution() > 0)
        .partition(|rule| rule.expected_repair_value() > 0);
    projected.sort_by(|left, right| {
        right
            .expected_repair_value()
            .cmp(&left.expected_repair_value())
            .then_with(|| right.contribution().cmp(&left.contribution()))
            .then_with(|| left.id.cmp(&right.id))
    });
    withheld.sort_by(|left, right| {
        right
            .contribution()
            .cmp(&left.contribution())
            .then_with(|| left.id.cmp(&right.id))
    });
    (projected, withheld)
}

/// Capped score and its cause, for a given set of rules.
struct ScoredState {
    dimensions: ScoreDimensions,
    worst_tier: Option<RuleTier>,
    applied_ceiling: Option<u8>,
    value: u8,
}

impl ScoredState {
    fn of(rules: &[RuleAggregate], removed: &BTreeSet<String>, scale: Scale) -> Self {
        let dimensions = calculate_dimensions(rules, removed, scale);
        let worst_tier = rules
            .iter()
            .filter(|rule| !removed.contains(&rule.id))
            .fold(None, |worst, rule| worse_tier(worst, rule.scoring_tier()));
        let applied_ceiling = worst_tier.and_then(tier_overall_ceiling);
        Self {
            dimensions,
            worst_tier,
            applied_ceiling,
            value: capped(weighted_score(dimensions), applied_ceiling),
        }
    }
}

/// One aggregate per rule that fired, over every diagnostic the report publishes.
///
/// The score and the report body read the same aggregates. What separates them is not the
/// population but which figures they use: a diagnostic outside production code is counted in
/// `occurrences` and weighs nothing in `numerator`, so it stays visible and costs no points,
/// and it never decides whether the diagnostics are authoritative. That set-aside used to live
/// at one of the two call sites, so the contribution the body ranked by was not the one the
/// score charged, and a rule that only ever fired in a test was ranked as if it had.
///
/// The scale comes in rather than out because a density needs a denominator: the same rule
/// firing at the same sites is worth different points in a two-kiloline crate and in a
/// seven-hundred-kiloline one, and that is the whole reason this model exists.
pub(crate) fn aggregate_rules<'a>(
    production_lines: usize,
    diagnostics: impl IntoIterator<Item = &'a Diagnostic>,
) -> RuleAggregation {
    let mut diagnostics_are_authoritative = true;
    let mut rules = BTreeMap::<String, PendingRule>::new();
    for diagnostic in diagnostics {
        let weighs = crate::report::DiagnosticContext::weighs(diagnostic);
        let Some(rule_id) = diagnostic.code.as_deref().filter(|code| !code.is_empty()) else {
            diagnostics_are_authoritative &= !weighs;
            continue;
        };
        let definition = crate::policy::find(rule_id);
        let mapping = diagnostic.category.as_deref().and_then(category_mapping);
        let weight = density::severity_weight(diagnostic.severity);
        let scores = weighs && weight.is_some() && mapping.is_some();
        if weighs && !scores {
            diagnostics_are_authoritative = false;
        }
        // A rule the catalog cannot resolve has no producer to read a denominator off, so it is
        // counted against the workspace and the flag says the score was computed over something
        // the catalog does not describe. The scope has to be total: there is no third answer a
        // density could be divided by.
        if weighs && definition.is_none() {
            diagnostics_are_authoritative = false;
        }
        let rule = rules.entry(rule_id.to_owned()).or_insert(PendingRule {
            id: rule_id.to_owned(),
            severity: diagnostic.severity,
            category: None,
            dimension: None,
            scope: definition.map_or(DensityScope::Workspace, |definition| {
                DensityScope::of(definition.producer)
            }),
            mapping_conflict: false,
            tier: definition.map(|definition| definition.tier),
            occurrences: 0,
            numerator: 0,
            noise: crate::policy::corpus_measurement(rule_id)
                .map(CorpusMeasurement::noise_basis_points),
        });
        rule.occurrences = rule.occurrences.saturating_add(diagnostic.occurrences);
        if diagnostic.severity.rank() < rule.severity.rank() {
            rule.severity = diagnostic.severity;
        }
        if scores
            && let Some((category, dimension)) = mapping
            && let Some(weight) = weight
        {
            // One diagnostic is one distinct site, whatever it counts in `occurrences`: a clone
            // family names its K members through `related` and is one place to go and look.
            rule.numerator = rule.numerator.saturating_add(weight);
            if rule.dimension.is_some_and(|current| current != dimension) {
                rule.mapping_conflict = true;
                diagnostics_are_authoritative = false;
            } else if rule.dimension.is_none() {
                rule.category = Some(category);
                rule.dimension = Some(dimension);
            }
        } else if rule.category.is_none()
            && let Some((category, _)) = mapping
        {
            rule.category = Some(category);
        }
    }
    let scale = Scale::of(production_lines);
    let mut rules: Vec<RuleAggregate> = rules
        .into_values()
        .map(|rule| RuleAggregate {
            id: rule.id,
            effective_severity: rule.severity,
            category: (!rule.mapping_conflict).then_some(rule.category).flatten(),
            dimension: (!rule.mapping_conflict && rule.numerator > 0)
                .then_some(rule.dimension)
                .flatten(),
            scope: rule.scope,
            tier: rule.tier,
            occurrences: rule.occurrences,
            numerator: rule.numerator,
            noise: rule.noise,
            contribution: 0,
        })
        .collect();
    let recovered = contributions(&rules, scale);
    for (rule, contribution) in rules.iter_mut().zip(recovered) {
        rule.contribution = contribution;
    }
    RuleAggregation {
        rules,
        scale,
        diagnostics_are_authoritative,
    }
}

fn weighted_score(dimensions: ScoreDimensions) -> u8 {
    let numerator = ScoreDimension::ALL.into_iter().fold(0u64, |sum, dimension| {
        sum.saturating_add(
            u64::from(dimensions.value_for(dimension)) * dimension_weight_twice(dimension),
        )
    });
    u8::try_from((numerator.saturating_add(6) / 13).min(100)).unwrap_or(100)
}

const fn score_label(score: u8) -> ScoreLabel {
    if score >= 75 {
        ScoreLabel::Great
    } else if score >= 50 {
        ScoreLabel::NeedsWork
    } else {
        ScoreLabel::Critical
    }
}

fn build_share_url(
    score: u8,
    errors: usize,
    warnings: usize,
    info: usize,
    source_files: usize,
    production_lines: usize,
) -> Result<String, ShareError> {
    if score > 100
        || [errors, warnings, info, source_files, production_lines]
            .into_iter()
            .any(|count| count > MAX_SHARED_COUNT)
    {
        return Err(ShareError::InvalidPayload);
    }

    // The model comes first and is never omitted: every number after it is a
    // reading of one scale, and a payload that names none is a score the page
    // it opens has to guess the meaning of. `core-v2` and `core-v3` publish
    // the same shape over the same rules for very different values.
    let mut url = format!("{SHARE_BASE_URL}?s={score}&m={SCORE_MODEL}");
    for (key, count) in [
        ("e", errors),
        ("w", warnings),
        ("i", info),
        ("f", source_files),
        ("l", production_lines),
    ] {
        if count > 0 {
            let _ = write!(url, "&{key}={count}");
        }
    }
    Ok(url)
}

#[cfg(test)]
mod tests;