aprender_qa_certify/
lib_scoring.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3pub enum CertificationTier {
4 #[default]
7 Mvp,
8 Full,
11}
12
13pub const MVP_PASS_THRESHOLD: f64 = 0.90;
15
16pub const MVP_PASS_SCORE: u32 = 800;
18
19pub const FULL_PASS_THRESHOLD: f64 = 0.95;
21
22pub const FULL_PASS_SCORE: u32 = 950;
24
25#[must_use]
30pub const fn status_from_score(mqs_score: u32, has_p0_failure: bool) -> CertificationStatus {
31 if has_p0_failure {
32 CertificationStatus::Blocked
33 } else if mqs_score >= MVP_PASS_SCORE {
34 CertificationStatus::Certified
35 } else if mqs_score >= 700 {
36 CertificationStatus::Provisional
37 } else {
38 CertificationStatus::Blocked
39 }
40}
41
42#[must_use]
49pub fn status_from_tier(
50 tier: CertificationTier,
51 pass_rate: f64,
52 has_p0_failure: bool,
53) -> CertificationStatus {
54 if has_p0_failure {
55 return CertificationStatus::Blocked;
56 }
57
58 match tier {
59 CertificationTier::Mvp => {
60 if pass_rate >= MVP_PASS_THRESHOLD {
61 CertificationStatus::Provisional
62 } else {
63 CertificationStatus::Blocked
64 }
65 }
66 CertificationTier::Full => {
67 if pass_rate >= FULL_PASS_THRESHOLD {
68 CertificationStatus::Certified
69 } else if pass_rate >= MVP_PASS_THRESHOLD {
70 CertificationStatus::Provisional
71 } else {
72 CertificationStatus::Blocked
73 }
74 }
75 }
76}
77
78#[inline]
80fn scale_to_f_grade(pass_rate: f64) -> u32 {
81 let clamped = pass_rate.clamp(0.0, 1.0);
83 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
84 let score = (clamped * 699.0) as u32;
85 score.min(699)
86}
87
88#[must_use]
95pub fn score_from_tier(tier: CertificationTier, pass_rate: f64, has_p0_failure: bool) -> u32 {
96 if has_p0_failure {
97 return scale_to_f_grade(pass_rate);
99 }
100
101 match tier {
102 CertificationTier::Mvp => {
103 if pass_rate >= MVP_PASS_THRESHOLD {
104 MVP_PASS_SCORE
106 } else {
107 scale_to_f_grade(pass_rate)
109 }
110 }
111 CertificationTier::Full => {
112 if pass_rate >= FULL_PASS_THRESHOLD {
113 let bonus = ((pass_rate - FULL_PASS_THRESHOLD) * 1000.0).clamp(0.0, 50.0);
115 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
116 let bonus_u32 = bonus as u32;
117 FULL_PASS_SCORE + bonus_u32
118 } else if pass_rate >= MVP_PASS_THRESHOLD {
119 let ratio =
121 (pass_rate - MVP_PASS_THRESHOLD) / (FULL_PASS_THRESHOLD - MVP_PASS_THRESHOLD);
122 let bonus = (ratio * 99.0).clamp(0.0, 99.0);
123 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
124 let bonus_u32 = bonus as u32;
125 800 + bonus_u32
126 } else {
127 scale_to_f_grade(pass_rate)
129 }
130 }
131 }
132}
133
134#[must_use]
136pub const fn grade_from_score(mqs_score: u32) -> &'static str {
137 match mqs_score {
138 950..=1000 => "A+",
139 900..=949 => "A",
140 850..=899 => "B+",
141 800..=849 => "B",
142 700..=799 => "C",
143 _ => "F",
144 }
145}
146
147#[must_use]
154pub fn grade_from_tier(
155 tier: CertificationTier,
156 pass_rate: f64,
157 has_p0_failure: bool,
158) -> &'static str {
159 let score = score_from_tier(tier, pass_rate, has_p0_failure);
160 grade_from_score(score)
161}
162
163pub fn update_readme(readme: &str, table_content: &str) -> Result<String> {
169 let start_idx = readme
170 .find(START_MARKER)
171 .ok_or_else(|| CertifyError::MarkerNotFound(START_MARKER.to_string()))?;
172 let end_idx = readme
173 .find(END_MARKER)
174 .ok_or_else(|| CertifyError::MarkerNotFound(END_MARKER.to_string()))?;
175
176 if start_idx >= end_idx {
177 return Err(CertifyError::MarkerNotFound(format!(
178 "START marker (pos {start_idx}) must appear before END marker (pos {end_idx})"
179 )));
180 }
181
182 let before = &readme[..start_idx + START_MARKER.len()];
183 let after = &readme[end_idx..];
184
185 Ok(format!("{before}\n{table_content}\n{after}"))
186}