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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Normalized Score System (PMAT-454)
//!
//! All PMAT scoring systems MUST output values in the 0-100 range.
//! This module provides the trait and utilities to ensure consistent scoring.
//!
//! # Design Principles
//! - All scores are normalized to 0.0-100.0 range
//! - Raw scores can use any internal scale (106, 110, 200 points)
//! - `normalized()` method always returns 0-100
//! - Clamping ensures no out-of-range values
use std::fmt;
/// Trait for all scoring systems in PMAT.
///
/// Implementors MUST ensure `normalized()` returns values in [0.0, 100.0].
pub trait NormalizedScore: fmt::Display {
/// Returns the raw score value (internal scale).
fn raw(&self) -> f64;
/// Returns the maximum possible raw score.
fn max_raw(&self) -> f64;
/// Returns the normalized score in 0-100 range.
///
/// # Guarantees
/// - Always returns a value in [0.0, 100.0]
/// - Values are clamped if raw calculation exceeds bounds
fn normalized(&self) -> f64 {
let max = self.max_raw();
if max <= 0.0 {
return 0.0;
}
let normalized = (self.raw() / max) * 100.0;
normalized.clamp(0.0, 100.0)
}
/// Returns the letter grade based on normalized score.
fn grade(&self) -> Grade {
Grade::from_score(self.normalized())
}
/// Returns true if score meets the given threshold (0-100).
fn meets_threshold(&self, threshold: f64) -> bool {
self.normalized() >= threshold.clamp(0.0, 100.0)
}
}
/// Universal letter grades for all scoring systems.
/// Ordering: A > B > C > D > F (higher grade = better)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Grade {
/// 90-100: Excellent
A,
/// 80-89: Good
B,
/// 70-79: Satisfactory
C,
/// 60-69: Needs Improvement
D,
/// 0-59: Failing
F,
}
impl PartialOrd for Grade {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Grade {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.min_score()
.partial_cmp(&other.min_score())
.unwrap_or(std::cmp::Ordering::Equal)
}
}
impl Grade {
/// Convert a normalized score (0-100) to a grade.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
pub fn from_score(score: f64) -> Self {
match score {
s if s >= 90.0 => Grade::A,
s if s >= 80.0 => Grade::B,
s if s >= 70.0 => Grade::C,
s if s >= 60.0 => Grade::D,
_ => Grade::F,
}
}
/// Returns the minimum score for this grade.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
pub fn min_score(&self) -> f64 {
match self {
Grade::A => 90.0,
Grade::B => 80.0,
Grade::C => 70.0,
Grade::D => 60.0,
Grade::F => 0.0,
}
}
/// Returns the grade as a string with description.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn description(&self) -> &'static str {
match self {
Grade::A => "A (Excellent)",
Grade::B => "B (Good)",
Grade::C => "C (Satisfactory)",
Grade::D => "D (Needs Improvement)",
Grade::F => "F (Failing)",
}
}
}
impl fmt::Display for Grade {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Grade::A => write!(f, "A"),
Grade::B => write!(f, "B"),
Grade::C => write!(f, "C"),
Grade::D => write!(f, "D"),
Grade::F => write!(f, "F"),
}
}
}
/// Helper struct for creating normalized scores from raw values.
#[derive(Debug, Clone, Copy)]
pub struct SimpleScore {
raw: f64,
max: f64,
name: &'static str,
}
impl SimpleScore {
/// Create a new simple score.
///
/// # Panics
/// Panics if max <= 0.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(raw: f64, max: f64, name: &'static str) -> Self {
assert!(max > 0.0, "max must be positive");
Self {
raw: raw.max(0.0),
max,
name,
}
}
/// Create from a percentage (0-100).
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn from_percentage(pct: f64, name: &'static str) -> Self {
Self {
raw: pct.clamp(0.0, 100.0),
max: 100.0,
name,
}
}
}
impl NormalizedScore for SimpleScore {
fn raw(&self) -> f64 {
self.raw
}
fn max_raw(&self) -> f64 {
self.max
}
}
impl fmt::Display for SimpleScore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {:.1}/100 ({})",
self.name,
self.normalized(),
self.grade()
)
}
}
// Aggregate scoring: AggregateScore, NormalizedScoreClone trait
include!("normalized_score_aggregate.rs");
// Tests
include!("normalized_score_tests.rs");