1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
10pub struct UnitScore(f32);
11
12impl UnitScore {
13 pub fn new(value: f32) -> Self {
15 if value.is_nan() {
16 Self(0.0)
17 } else {
18 Self(value.clamp(0.0, 1.0))
19 }
20 }
21
22 pub fn value(&self) -> f32 {
24 self.0
25 }
26}
27
28impl Default for UnitScore {
29 fn default() -> Self {
30 Self(0.5)
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn default_is_midpoint() {
40 assert!((UnitScore::default().value() - 0.5).abs() < f32::EPSILON);
41 }
42}