rloc 0.2.0

A fast, modern Rust implementation of cloc (Count Lines of Code)
Documentation
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! # rloc
//!
//! A fast, modern library for counting lines of code and detecting programming languages.
//!
//! ## Quick Start
//!
//! ```no_run
//! use std::path::Path;
//!
//! // Get the top language for a directory
//! let top = rloc::top_language(Path::new(".")).unwrap();
//! println!("Top language: {} ({} lines of code)", top.name, top.code);
//!
//! // Fast mode: only look at extensions, don't read file contents
//! let top_fast = rloc::top_language_fast(Path::new(".")).unwrap();
//! println!("Top language (fast): {} ({} files)", top_fast.name, top_fast.files);
//!
//! // Full analysis with all languages
//! let analysis = rloc::analyze(Path::new(".")).unwrap();
//! for lang in &analysis.languages {
//!     println!("{}: {} files, {} code", lang.name, lang.files, lang.code);
//! }
//! ```
//!
//! ## Single File Detection
//!
//! ```no_run
//! use std::path::Path;
//!
//! if let Some(lang) = rloc::detect_language(Path::new("main.rs")) {
//!     println!("Detected: {}", lang.name);
//! }
//! ```

// Internal modules - exposed publicly for CLI binary
pub mod archive;
pub mod counter;
pub mod custom_langs;
mod languages;
pub mod stats;
pub mod walker;

#[cfg(feature = "cli")]
pub mod cli;
#[cfg(feature = "cli")]
pub mod diff;
#[cfg(feature = "cli")]
pub mod output;
#[cfg(feature = "cli")]
pub mod strip;

use dashmap::DashSet;
use rayon::prelude::*;
use std::path::Path;

pub use languages::{LANGUAGES, Language, detect_language, list_extensions, list_languages};

mod error;
pub use error::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, Default)]
pub struct LanguageBreakdown {
    pub name: &'static str,
    pub files: u64,
    pub code: u64,
    pub comments: u64,
    pub blanks: u64,
}

impl LanguageBreakdown {
    pub fn total_lines(&self) -> u64 {
        self.code + self.comments + self.blanks
    }
}

#[derive(Debug, Clone, Default)]
pub struct Analysis {
    pub languages: Vec<LanguageBreakdown>,
    pub total_files: u64,
    pub total_code: u64,
    pub total_comments: u64,
    pub total_blanks: u64,
}

impl Analysis {
    pub fn top_language(&self) -> Option<&LanguageBreakdown> {
        self.languages.first()
    }

    pub fn total_lines(&self) -> u64 {
        self.total_code + self.total_comments + self.total_blanks
    }
}

#[derive(Debug, Clone, Default)]
pub struct AnalyzeConfig {
    pub paths: Vec<std::path::PathBuf>,
    pub exclude_dirs: Vec<String>,
    pub exclude_exts: Vec<String>,
    pub exclude_langs: Vec<String>,
    pub include_exts: Vec<String>,
    pub include_langs: Vec<String>,
    pub follow_symlinks: bool,
    pub hidden: bool,
    pub max_depth: Option<usize>,
    pub skip_gitignore: bool,
    pub max_file_size: Option<u64>,
    pub threads: Option<usize>,
}

impl AnalyzeConfig {
    pub fn new(path: impl AsRef<Path>) -> Self {
        Self {
            paths: vec![path.as_ref().to_path_buf()],
            exclude_dirs: walker::WalkerConfig::default().exclude_dirs,
            ..Default::default()
        }
    }

    pub fn paths(mut self, paths: Vec<std::path::PathBuf>) -> Self {
        self.paths = paths;
        self
    }

    pub fn exclude_dirs(mut self, dirs: Vec<String>) -> Self {
        self.exclude_dirs = dirs;
        self
    }

    pub fn include_langs(mut self, langs: Vec<String>) -> Self {
        self.include_langs = langs;
        self
    }

    pub fn exclude_langs(mut self, langs: Vec<String>) -> Self {
        self.exclude_langs = langs;
        self
    }

    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = Some(depth);
        self
    }

    pub fn threads(mut self, threads: usize) -> Self {
        self.threads = Some(threads);
        self
    }
}

/// Get the top (most code) language in a directory.
///
/// This performs a full analysis, reading file contents to count lines of code,
/// comments, and blank lines.
///
/// # Example
///
/// ```no_run
/// use std::path::Path;
///
/// let top = rloc::top_language(Path::new(".")).unwrap();
/// println!("{}: {} lines of code", top.name, top.code);
/// ```
pub fn top_language(path: impl AsRef<Path>) -> Result<LanguageBreakdown> {
    let analysis = analyze(path)?;
    analysis.top_language().cloned().ok_or(Error::NoSourceFiles)
}

/// Get the top language quickly by only counting files (not reading contents).
///
/// This is much faster than `top_language()` but only gives file counts,
/// not lines of code.
///
/// # Example
///
/// ```no_run
/// use std::path::Path;
///
/// let top = rloc::top_language_fast(Path::new(".")).unwrap();
/// println!("{}: {} files", top.name, top.files);
/// ```
pub fn top_language_fast(path: impl AsRef<Path>) -> Result<LanguageBreakdown> {
    let analysis = analyze_fast(path)?;
    analysis.top_language().cloned().ok_or(Error::NoSourceFiles)
}

/// Analyze a directory and return full statistics for all languages.
///
/// This reads file contents to accurately count lines of code, comments,
/// and blank lines.
pub fn analyze(path: impl AsRef<Path>) -> Result<Analysis> {
    let config = AnalyzeConfig::new(path);
    analyze_with_config(config)
}

/// Fast analysis that only counts files by extension (no file reads).
///
/// This is useful when you only need to know the language distribution
/// by file count, not by lines of code.
pub fn analyze_fast(path: impl AsRef<Path>) -> Result<Analysis> {
    let config = AnalyzeConfig::new(path);
    analyze_fast_with_config(config)
}

/// Analyze with custom configuration.
pub fn analyze_with_config(config: AnalyzeConfig) -> Result<Analysis> {
    if let Some(threads) = config.threads {
        if threads > 0 {
            rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build_global()
                .ok();
        }
    }

    let walker_config = config_to_walker(&config);
    let files = walker::walk_files(&walker_config);

    if files.is_empty() {
        return Err(Error::NoSourceFiles);
    }

    let seen_hashes: DashSet<u64> = DashSet::new();

    let file_stats: Vec<_> = files
        .into_par_iter()
        .filter_map(|entry| {
            if let Ok(hash) = counter::compute_file_hash(&entry.path) {
                if !seen_hashes.insert(hash) {
                    return None;
                }
            }

            match counter::count_lines(&entry.path, entry.language) {
                Ok(stats) if stats.total() > 0 => Some(stats),
                _ => None,
            }
        })
        .collect();

    let summary = stats::Summary::from_file_stats(file_stats);
    Ok(summary_to_analysis(&summary))
}

/// Fast analysis with custom configuration (extension-only, no file reads).
pub fn analyze_fast_with_config(config: AnalyzeConfig) -> Result<Analysis> {
    let walker_config = config_to_walker(&config);
    let files = walker::walk_files(&walker_config);

    if files.is_empty() {
        return Err(Error::NoSourceFiles);
    }

    use ahash::AHashMap;
    let mut by_language: AHashMap<&'static str, u64> = AHashMap::new();

    for entry in &files {
        *by_language.entry(entry.language.name).or_insert(0) += 1;
    }

    let mut languages: Vec<_> = by_language
        .into_iter()
        .map(|(name, files)| LanguageBreakdown {
            name,
            files,
            code: 0,
            comments: 0,
            blanks: 0,
        })
        .collect();

    languages.sort_by(|a, b| b.files.cmp(&a.files));

    let total_files = languages.iter().map(|l| l.files).sum();

    Ok(Analysis {
        languages,
        total_files,
        total_code: 0,
        total_comments: 0,
        total_blanks: 0,
    })
}

fn config_to_walker(config: &AnalyzeConfig) -> walker::WalkerConfig {
    walker::WalkerConfig {
        paths: if config.paths.is_empty() {
            vec![std::path::PathBuf::from(".")]
        } else {
            config.paths.clone()
        },
        exclude_dirs: config.exclude_dirs.clone(),
        exclude_exts: config.exclude_exts.clone(),
        exclude_langs: config.exclude_langs.clone(),
        include_exts: config.include_exts.clone(),
        include_langs: config.include_langs.clone(),
        follow_symlinks: config.follow_symlinks,
        hidden: config.hidden,
        max_depth: config.max_depth,
        skip_gitignore: config.skip_gitignore,
        max_file_size: config.max_file_size,
        ..Default::default()
    }
}

fn summary_to_analysis(summary: &stats::Summary) -> Analysis {
    Analysis {
        languages: summary
            .languages
            .iter()
            .map(|l| LanguageBreakdown {
                name: languages::LANGUAGES
                    .get(&l.name)
                    .map(|lang| lang.name)
                    .unwrap_or_else(|| {
                        // For custom languages, we need to leak the string to get 'static
                        // This is acceptable since language names are bounded and reused
                        Box::leak(l.name.clone().into_boxed_str())
                    }),
                files: l.files,
                code: l.code,
                comments: l.comments,
                blanks: l.blanks,
            })
            .collect(),
        total_files: summary.total_files,
        total_code: summary.total_code,
        total_comments: summary.total_comments,
        total_blanks: summary.total_blanks,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_analyze_simple() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("main.rs"),
            "fn main() {\n    println!(\"Hello\");\n}\n",
        )
        .unwrap();
        fs::write(
            temp.path().join("lib.rs"),
            "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n",
        )
        .unwrap();

        let analysis = analyze(temp.path()).unwrap();
        assert_eq!(analysis.total_files, 2);
        assert!(analysis.total_code > 0);

        let top = analysis.top_language().unwrap();
        assert_eq!(top.name, "Rust");
    }

    #[test]
    fn test_analyze_fast() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join("main.rs"), "fn main() {}").unwrap();
        fs::write(temp.path().join("lib.rs"), "pub fn x() {}").unwrap();
        fs::write(temp.path().join("app.ts"), "const x = 1;").unwrap();

        let analysis = analyze_fast(temp.path()).unwrap();
        assert_eq!(analysis.total_files, 3);

        let top = analysis.top_language().unwrap();
        assert_eq!(top.name, "Rust");
        assert_eq!(top.files, 2);
    }

    #[test]
    fn test_top_language() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("main.py"),
            "print('hello')\nprint('world')\n",
        )
        .unwrap();
        fs::write(temp.path().join("lib.rs"), "fn x() {}\n").unwrap();

        let top = top_language(temp.path()).unwrap();
        assert_eq!(top.name, "Python");
    }

    #[test]
    fn test_detect_language() {
        let lang = detect_language(Path::new("test.rs")).unwrap();
        assert_eq!(lang.name, "Rust");

        let lang = detect_language(Path::new("test.ts")).unwrap();
        assert_eq!(lang.name, "TypeScript");

        assert!(detect_language(Path::new("test.unknown")).is_none());
    }

    #[test]
    fn test_no_source_files() {
        let temp = TempDir::new().unwrap();
        // Use an extension that's not recognized as any language
        fs::write(temp.path().join("readme.xyz"), "hello").unwrap();

        let result = analyze(temp.path());
        assert!(matches!(result, Err(Error::NoSourceFiles)));
    }
}