Skip to main content

diff_risk_core/
scoring.rs

1//! Scoring function — turns a flat list of findings into a 0.0–10.0 score.
2//!
3//! The rule is deliberately simple and auditable: sum severity weights,
4//! apply a diminishing-returns curve, and clamp to `[0.0, 10.0]`.
5
6use crate::report::Finding;
7
8/// Maximum score the scorer can output.
9pub const MAX_SCORE: f32 = 10.0;
10
11/// Score a list of findings.
12///
13/// Empty list → `0.0`. A single `Critical` finding already lands above the
14/// `--threshold 7` gate described in the README. Additional findings push
15/// the score up with diminishing returns so that ten medium issues don't
16/// outrank one critical.
17#[must_use]
18pub fn score_findings(findings: &[Finding]) -> f32 {
19    if findings.is_empty() {
20        return 0.0;
21    }
22
23    let total_weight: f32 = findings.iter().map(|f| f.severity.weight()).sum();
24    let max_severity_weight = findings
25        .iter()
26        .map(|f| f.severity.weight())
27        .fold(0.0_f32, f32::max);
28
29    // Diminishing-returns curve: max-severity floor + saturating bonus.
30    // floor = weight of the worst finding (keeps "one Critical" visible).
31    // bonus = scaled log of total weight for pile-up of lesser findings.
32    let bonus = (1.0 + total_weight - max_severity_weight).ln().max(0.0);
33    let raw = max_severity_weight + bonus;
34
35    raw.clamp(0.0, MAX_SCORE)
36}
37
38/// Convenience: does this report's score meet or exceed `threshold`?
39#[must_use]
40pub fn exceeds(score: f32, threshold: f32) -> bool {
41    score >= threshold
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::report::{Finding, RiskCategory, Severity};
48
49    fn finding(sev: Severity) -> Finding {
50        Finding {
51            category: RiskCategory::Other,
52            severity: sev,
53            file: "x.rs".to_string(),
54            line: Some(1),
55            message: "test".to_string(),
56        }
57    }
58
59    #[test]
60    fn empty_is_zero() {
61        assert_eq!(score_findings(&[]), 0.0);
62    }
63
64    #[test]
65    fn single_critical_exceeds_threshold_seven() {
66        let s = score_findings(&[finding(Severity::Critical)]);
67        assert!(s >= 7.0, "critical must exceed threshold 7, got {s}");
68    }
69
70    #[test]
71    fn single_low_below_threshold_seven() {
72        let s = score_findings(&[finding(Severity::Low)]);
73        assert!(s < 7.0, "single low must be below threshold 7, got {s}");
74    }
75
76    #[test]
77    fn one_critical_beats_many_mediums() {
78        let crit = score_findings(&[finding(Severity::Critical)]);
79        let mediums = score_findings(&[
80            finding(Severity::Medium),
81            finding(Severity::Medium),
82            finding(Severity::Medium),
83        ]);
84        assert!(crit > mediums);
85    }
86
87    #[test]
88    fn score_clamped_to_max() {
89        let flood: Vec<Finding> = (0..50).map(|_| finding(Severity::Critical)).collect();
90        assert!(score_findings(&flood) <= MAX_SCORE);
91    }
92
93    #[test]
94    fn more_findings_never_lowers_score() {
95        let one = score_findings(&[finding(Severity::High)]);
96        let two = score_findings(&[finding(Severity::High), finding(Severity::Low)]);
97        assert!(two >= one);
98    }
99}