rustloclib
A Rust-aware lines of code counter library that separates code, tests, comments, and blank lines.
Overview
Unlike generic LOC counters, this library understands Rust's unique structure where tests live alongside production code. It uses syntax-aware parsing to distinguish:
- Main: Production code lines
- Tests: Code within
#[test]or#[cfg(test)]blocks, or intests/directories - Examples: Code in
examples/directories - Docs: Documentation comments (
///,//!,/** */,/*! */) - Comments: Regular comments (
//,/* */) - Blank: Whitespace-only lines
Features
- Rust-aware parsing: Properly handles
#[cfg(test)],#[test]attributes - Cargo workspace support: Discover and filter crates within a workspace
- Glob filtering: Include/exclude files with glob patterns
- Pure Rust data types: Returns structured data for easy integration
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage
Count a workspace
use ;
let result = count_workspace?;
println!;
println!;
println!;
println!;
Count specific crates
use ;
let result = count_workspace?;
Filter files with globs
use ;
let filter = new
.exclude?
.exclude?;
let result = count_workspace?;
Count a single file
use count_file;
let stats = count_file?;
println!;
Parse from a string (for testing)
use ;
let source = r#"
fn main() {
println!("Hello");
}
#[test]
fn test_main() {
assert!(true);
}
"#;
let stats = parse_string;
assert_eq!; // fn, println, }
assert_eq!; // #[test], fn, assert, }
Data Structures
LocStats
Aggregated statistics for a collection of files:
Locs
Line counts for a single context:
CountResult
Result from counting operations:
Acknowledgments
The parsing logic is adapted from cargo-warloc by Maxim Gritsenko (MIT licensed).
License
MIT License