Skip to main content

garbage_code_hunter/common/
mod.rs

1//! Shared types and utilities used across multiple analysis modules.
2
3pub mod i18n_ext;
4pub mod severity;
5pub use severity::Severity;
6
7use serde::{Deserialize, Serialize};
8
9/// Output format for all analysis reports.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub enum OutputFormat {
12    Terminal,
13    Json,
14}
15
16/// Grade label for a numeric score.
17pub fn score_to_grade(score: f64) -> String {
18    match score as u32 {
19        90..=100 => "S \u{2728}".to_string(),
20        80..=89 => "A \u{1f44d}".to_string(),
21        70..=79 => "B \u{1f44d}".to_string(),
22        60..=69 => "C \u{1f610}".to_string(),
23        50..=59 => "D \u{1f615}".to_string(),
24        _ => "F \u{1f4a9}".to_string(),
25    }
26}
27
28/// Format a score as a colored string (green >= 80, yellow >= 60, red < 60).
29pub fn format_score_color(score: f64) -> colored::ColoredString {
30    use colored::Colorize;
31    if score >= 80.0 {
32        format!("{:.0}", score).green()
33    } else if score >= 60.0 {
34        format!("{:.0}", score).yellow()
35    } else {
36        format!("{:.0}", score).red()
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_score_to_grade() {
46        assert_eq!(score_to_grade(95.0), "S \u{2728}");
47        assert_eq!(score_to_grade(85.0), "A \u{1f44d}");
48        assert_eq!(score_to_grade(75.0), "B \u{1f44d}");
49        assert_eq!(score_to_grade(65.0), "C \u{1f610}");
50        assert_eq!(score_to_grade(55.0), "D \u{1f615}");
51        assert_eq!(score_to_grade(30.0), "F \u{1f4a9}");
52    }
53
54    #[test]
55    fn test_output_format_serde() {
56        let f = OutputFormat::Json;
57        let s = serde_json::to_string(&f).unwrap();
58        let d: OutputFormat = serde_json::from_str(&s).unwrap();
59        assert_eq!(d, OutputFormat::Json);
60    }
61}