codstts/core/
stats.rs

1use std::collections::HashMap;
2
3#[derive(Debug, Default, Clone)]
4pub struct FileStats {
5    pub bytes: usize,
6    pub lines: LineStats,
7}
8
9#[derive(Debug, Default, Clone)]
10pub struct LineStats {
11    pub total: usize,
12    pub code: usize,
13    pub comment: usize,
14    pub blank: usize,
15}
16
17#[derive(Debug)]
18pub struct LanguageStats {
19    pub stats: HashMap<String, FileStats>,
20    pub total_files: usize,
21}
22
23impl LanguageStats {
24    pub fn new() -> Self {
25        Self {
26            stats: HashMap::new(),
27            total_files: 0,
28        }
29    }
30
31    pub fn update(&mut self, language: &str, stats: FileStats) {
32        let entry = self.stats.entry(language.to_string()).or_default();
33        entry.bytes += stats.bytes;
34        entry.lines.total += stats.lines.total;
35        entry.lines.code += stats.lines.code;
36        entry.lines.comment += stats.lines.comment;
37        entry.lines.blank += stats.lines.blank;
38        self.total_files += 1;
39    }
40}
41
42pub trait RoundToDecimals {
43    fn round_to_decimal(self, decimal_places: i32) -> Self;
44}
45
46impl RoundToDecimals for f64 {
47    fn round_to_decimal(self, decimal_places: i32) -> Self {
48        let factor = 10.0f64.powi(decimal_places);
49        (self * factor).round() / factor
50    }
51}