A Rust library and binary for analyzing Rust source code files, extracting functions and structs with their line counts.
This project is structured as a library plus binary:
src/lib.rs- Core library containing the AST parsing functionalitysrc/main.rs- Binary that provides a command-line interfaceexamples/usage.rs- Example demonstrating library usage
- Parse Rust source code using the
syncrate - Extract all functions and structs from Rust files
- Count lines of code for each function and struct
- Provide both high-level and low-level APIs
# Analyze a Rust source file
cargo run <rust_file.rs>
# Example
cargo run src/main.rsuse loc::{parse_rust_file, analyze_rust_file};
use std::path::Path;
// High-level analysis (returns formatted string)
let output = analyze_rust_file(Path::new("src/main.rs"))?;
println!("{}", output);
// Low-level parsing (returns AstVisitor for custom processing)
let visitor = parse_rust_file(Path::new("src/lib.rs"))?;
// Access functions and structs
for (name, start_line, end_line) in visitor.get_functions() {
println!("Function: {} (lines {}-{})", name, start_line, end_line);
}
for (name, start_line, end_line) in visitor.get_structs() {
println!("Struct: {} (lines {}-{})", name, start_line, end_line);
}# Run the usage example
cargo run --example usageParses a Rust source file and returns an AstVisitor containing the parsed information.
High-level function that analyzes a Rust file and returns formatted output as a string.
The main struct containing parsed function and struct information:
get_functions() -> &[(String, usize, usize)]- Returns functions with (name, start_line, end_line)get_structs() -> &[(String, usize, usize)]- Returns structs with (name, start_line, end_line)
# Build the library and binary
cargo build
# Build in release mode
cargo build --release# Run tests
cargo test
# Run with output
cargo test -- --nocapture