1use serde::Serialize;
5
6#[derive(Debug, Clone, Serialize)]
12pub struct FunctionReport {
13 pub name: String,
14 pub kind: String,
16 pub line: u32,
18 pub cognitive: u32,
19 pub cyclomatic: u32,
20 #[serde(skip_serializing_if = "Vec::is_empty")]
21 pub children: Vec<FunctionReport>,
22}
23
24#[derive(Debug, Clone, Serialize)]
26pub struct FileReport {
27 pub path: String,
28 pub cognitive: u32,
30 pub cyclomatic: u32,
31 pub functions: Vec<FunctionReport>,
32 #[serde(skip_serializing_if = "Vec::is_empty")]
33 pub parse_errors: Vec<String>,
34}
35
36#[derive(Debug, Clone, Serialize)]
42pub struct MetricSummary {
43 pub sum: u32,
44 pub max: u32,
45 pub median: u32,
46 pub p90: u32,
47 pub p95: u32,
48}
49
50#[derive(Debug, Clone, Serialize)]
52pub struct Summary {
53 pub file_count: usize,
54 pub function_count: usize,
55 pub parse_error_count: usize,
59 pub parse_error_file_count: usize,
61 #[serde(skip_serializing_if = "Vec::is_empty")]
65 pub parse_error_files: Vec<String>,
66 pub cognitive: MetricSummary,
67 pub cyclomatic: MetricSummary,
68}
69
70#[derive(Debug, Clone, Serialize)]
72pub struct Report {
73 pub files: Vec<FileReport>,
74 pub summary: Summary,
75}
76
77#[derive(Debug, Clone, Copy)]
79pub enum Metric {
80 Cognitive,
81 Cyclomatic,
82}
83
84impl Metric {
85 fn as_str(self) -> &'static str {
86 match self {
87 Metric::Cognitive => "cognitive",
88 Metric::Cyclomatic => "cyclomatic",
89 }
90 }
91}
92
93#[derive(Debug, Clone, Serialize)]
96pub struct TopEntry {
97 pub path: String,
98 pub name: String,
99 pub kind: String,
100 pub line: u32,
101 pub cognitive: u32,
102 pub cyclomatic: u32,
103}
104
105#[derive(Debug, Clone, Serialize)]
107pub struct TopReport {
108 pub metric: String,
110 pub top: Vec<TopEntry>,
111 pub summary: Summary,
112}
113
114pub fn for_each_function(fns: &[FunctionReport], f: &mut impl FnMut(&FunctionReport)) {
116 for func in fns {
117 f(func);
118 for_each_function(&func.children, f);
119 }
120}
121
122pub fn compute_top(reports: &[FileReport], metric: Metric, n: usize) -> Vec<TopEntry> {
126 let mut entries = Vec::new();
127 for r in reports {
128 for_each_function(&r.functions, &mut |f| {
129 entries.push(TopEntry {
130 path: r.path.clone(),
131 name: f.name.clone(),
132 kind: f.kind.clone(),
133 line: f.line,
134 cognitive: f.cognitive,
135 cyclomatic: f.cyclomatic,
136 });
137 });
138 }
139 entries.sort_by(|a, b| {
140 let (av, bv) = match metric {
141 Metric::Cognitive => (a.cognitive, b.cognitive),
142 Metric::Cyclomatic => (a.cyclomatic, b.cyclomatic),
143 };
144 bv.cmp(&av)
145 .then_with(|| a.path.cmp(&b.path))
146 .then(a.line.cmp(&b.line))
147 });
148 entries.truncate(n);
149 entries
150}
151
152pub fn build_top_report(
154 reports: &[FileReport],
155 summary: Summary,
156 metric: Metric,
157 n: usize,
158) -> TopReport {
159 TopReport {
160 metric: metric.as_str().to_string(),
161 top: compute_top(reports, metric, n),
162 summary,
163 }
164}
165
166fn percentile(sorted: &[u32], p: f64) -> u32 {
169 if sorted.is_empty() {
170 return 0;
171 }
172 let n = sorted.len();
173 let rank = ((p / 100.0) * n as f64).ceil() as usize;
174 let idx = rank.saturating_sub(1).min(n - 1);
175 sorted[idx]
176}
177
178fn metric_summary(mut values: Vec<u32>) -> MetricSummary {
179 values.sort_unstable();
180 MetricSummary {
181 sum: values.iter().sum(),
182 max: values.last().copied().unwrap_or(0),
183 median: percentile(&values, 50.0),
184 p90: percentile(&values, 90.0),
185 p95: percentile(&values, 95.0),
186 }
187}
188
189pub fn compute_summary(reports: &[FileReport]) -> Summary {
193 let mut cog = Vec::new();
194 let mut cyc = Vec::new();
195 let mut parse_error_count = 0;
196 let mut parse_error_files = Vec::new();
197 for r in reports {
198 for_each_function(&r.functions, &mut |f| {
199 cog.push(f.cognitive);
200 cyc.push(f.cyclomatic);
201 });
202 if !r.parse_errors.is_empty() {
203 parse_error_count += r.parse_errors.len();
204 parse_error_files.push(r.path.clone());
205 }
206 }
207 Summary {
208 file_count: reports.len(),
209 function_count: cog.len(),
210 parse_error_count,
211 parse_error_file_count: parse_error_files.len(),
212 parse_error_files,
213 cognitive: metric_summary(cog),
214 cyclomatic: metric_summary(cyc),
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221
222 #[test]
223 fn percentile_nearest_rank() {
224 let v: Vec<u32> = (1..=10).collect();
225 assert_eq!(percentile(&v, 50.0), 5);
226 assert_eq!(percentile(&v, 90.0), 9);
227 assert_eq!(percentile(&v, 95.0), 10);
228 assert_eq!(percentile(&v, 100.0), 10);
229 }
230
231 #[test]
232 fn percentile_empty_is_zero() {
233 assert_eq!(percentile(&[], 50.0), 0);
234 }
235
236 #[test]
237 fn summary_aggregates_parse_errors() {
238 let file = |path: &str, errors: Vec<String>| FileReport {
239 path: path.to_string(),
240 cognitive: 0,
241 cyclomatic: 0,
242 functions: Vec::new(),
243 parse_errors: errors,
244 };
245 let reports = vec![
246 file("clean", Vec::new()),
247 file("one", vec!["syntax error at line 1".to_string()]),
248 file(
249 "two",
250 vec![
251 "syntax error at line 3".to_string(),
252 "syntax error at line 9".to_string(),
253 ],
254 ),
255 ];
256 let s = compute_summary(&reports);
257 assert_eq!(s.parse_error_count, 3);
258 assert_eq!(s.parse_error_file_count, 2);
259 assert_eq!(s.parse_error_files, vec!["one", "two"]);
260 }
261
262 #[test]
263 fn summary_without_parse_errors_is_zero() {
264 let s = compute_summary(&[]);
265 assert_eq!(s.parse_error_count, 0);
266 assert_eq!(s.parse_error_file_count, 0);
267 assert!(s.parse_error_files.is_empty());
268 }
269}