|
| 1 | +mod languages; |
| 2 | +mod paths; |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::sync::LazyLock; |
| 7 | +use tokei::Config; |
| 8 | + |
| 9 | +pub use crate::paths::PathDetails; |
| 10 | + |
| 11 | +// Re-export LanguageType for use by other crates |
| 12 | +pub use tokei::LanguageType; |
| 13 | + |
| 14 | +/// Tokei configuration used for analysis (cached) |
| 15 | +static TOKEI_CONFIG: LazyLock<Config> = LazyLock::new(|| Config { |
| 16 | + no_ignore: Some(true), |
| 17 | + treat_doc_strings_as_comments: Some(true), |
| 18 | + ..Default::default() |
| 19 | +}); |
| 20 | + |
| 21 | +/// Statistics for a single programming language |
| 22 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)] |
| 23 | +pub struct LanguageStats { |
| 24 | + /// Number of lines of code (excluding comments and blank lines) |
| 25 | + pub code_lines: usize, |
| 26 | + /// Number of comment lines |
| 27 | + pub comment_lines: usize, |
| 28 | + /// Number of files of this language |
| 29 | + pub files: usize, |
| 30 | +} |
| 31 | + |
| 32 | +/// Complete line count statistics for a crate |
| 33 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)] |
| 34 | +pub struct LinecountStats { |
| 35 | + /// Per-language breakdown of line counts |
| 36 | + pub languages: HashMap<LanguageType, LanguageStats>, |
| 37 | + /// Total lines of code across all languages |
| 38 | + pub total_code_lines: usize, |
| 39 | + /// Total comment lines across all languages |
| 40 | + pub total_comment_lines: usize, |
| 41 | +} |
| 42 | + |
| 43 | +impl LinecountStats { |
| 44 | + /// Create a new empty statistics collection |
| 45 | + pub fn new() -> Self { |
| 46 | + Self::default() |
| 47 | + } |
| 48 | + |
| 49 | + /// Add a single file to the statistics |
| 50 | + /// |
| 51 | + /// The caller can use `should_count_path()` to check if a file should be processed |
| 52 | + /// before decompressing to avoid unnecessary work. |
| 53 | + pub fn add_file(&mut self, language_type: LanguageType, content: &[u8]) { |
| 54 | + let file_stats = language_type.parse_from_slice(content, &TOKEI_CONFIG); |
| 55 | + |
| 56 | + // Update language-specific stats |
| 57 | + let entry = self.languages.entry(language_type).or_default(); |
| 58 | + entry.code_lines += file_stats.code; |
| 59 | + entry.comment_lines += file_stats.comments; |
| 60 | + entry.files += 1; |
| 61 | + |
| 62 | + // Update totals |
| 63 | + self.total_code_lines += file_stats.code; |
| 64 | + self.total_comment_lines += file_stats.comments; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use super::*; |
| 71 | + use std::path::Path; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_empty() { |
| 75 | + let stats = LinecountStats::new(); |
| 76 | + insta::assert_json_snapshot!(stats, @r#" |
| 77 | + { |
| 78 | + "languages": {}, |
| 79 | + "total_code_lines": 0, |
| 80 | + "total_comment_lines": 0 |
| 81 | + } |
| 82 | + "#); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_add_file() { |
| 87 | + let mut stats = LinecountStats::new(); |
| 88 | + |
| 89 | + // Add a Rust file |
| 90 | + let rust_code = b"// This is a comment\nfn main() {\n println!(\"Hello\");\n}"; |
| 91 | + stats.add_file(LanguageType::Rust, rust_code); |
| 92 | + |
| 93 | + insta::assert_json_snapshot!(stats, @r#" |
| 94 | + { |
| 95 | + "languages": { |
| 96 | + "Rust": { |
| 97 | + "code_lines": 3, |
| 98 | + "comment_lines": 1, |
| 99 | + "files": 1 |
| 100 | + } |
| 101 | + }, |
| 102 | + "total_code_lines": 3, |
| 103 | + "total_comment_lines": 1 |
| 104 | + } |
| 105 | + "#); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_workflow() { |
| 110 | + let mut stats = LinecountStats::new(); |
| 111 | + |
| 112 | + let files = [ |
| 113 | + ("src/lib.rs", "pub fn hello() {}"), |
| 114 | + ("tests/test.rs", "fn test() {}"), // Should be skipped |
| 115 | + ("README.md", "# Hello"), // Should be skipped |
| 116 | + ]; |
| 117 | + |
| 118 | + for (path, content) in files { |
| 119 | + let path = Path::new(path); |
| 120 | + let path_details = PathDetails::from_path(path); |
| 121 | + |
| 122 | + if !path_details.should_ignore() |
| 123 | + && let Some(language_type) = path_details.language_type() |
| 124 | + { |
| 125 | + stats.add_file(language_type, content.as_bytes()) |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + insta::assert_json_snapshot!(stats, @r#" |
| 130 | + { |
| 131 | + "languages": { |
| 132 | + "Rust": { |
| 133 | + "code_lines": 1, |
| 134 | + "comment_lines": 0, |
| 135 | + "files": 1 |
| 136 | + } |
| 137 | + }, |
| 138 | + "total_code_lines": 1, |
| 139 | + "total_comment_lines": 0 |
| 140 | + } |
| 141 | + "#); |
| 142 | + } |
| 143 | +} |
0 commit comments