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
/// Format a ranked list of files as a table
/// Formats ranked files as a table for display
///
/// # Examples
///
/// ```rust,no_run
/// use pmat::services::ranking_utils::format_ranked_files_table;
/// use pmat::services::defect_analyzer::RankedFile;
/// use std::path::PathBuf;
///
/// let files = vec![
/// RankedFile {
/// path: PathBuf::from("src/main.rs"),
/// rank: 1,
/// score: 8.5,
/// defects: vec![],
/// }
/// ];
///
/// let table = format_ranked_files_table(&files);
/// assert!(table.contains("RANK"));
/// assert!(table.contains("src/main.rs"));
/// ```
#[must_use]
pub fn format_ranked_files_table(ranked_files: &[RankedFile]) -> String {
let mut output = String::new();
// Header
output.push_str("RANK SCORE FILE DEFECTS CRITICAL HIGH MEDIUM LOW\n");
output.push_str("---- ------ ------------------------------------------------ ------- -------- ---- ------ ---\n");
for file in ranked_files {
let mut critical = 0;
let mut high = 0;
let mut medium = 0;
let mut low = 0;
for defect in &file.defects {
match defect.severity {
Severity::Critical => critical += 1,
Severity::High => high += 1,
Severity::Medium => medium += 1,
Severity::Low => low += 1,
}
}
output.push_str(&format!(
"{:<4} {:>6.1} {:<48} {:>7} {:>8} {:>4} {:>6} {:>3}\n",
file.rank,
file.score,
file.path
.display()
.to_string()
.chars()
.take(48)
.collect::<String>(),
file.defects.len(),
critical,
high,
medium,
low
));
}
output
}