Skip to content

sebkur/loc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LOC - Rust Code Analyzer

A Rust library and binary for analyzing Rust source code files, extracting functions and structs with their line counts.

Project Structure

This project is structured as a library plus binary:

  • src/lib.rs - Core library containing the AST parsing functionality
  • src/main.rs - Binary that provides a command-line interface
  • examples/usage.rs - Example demonstrating library usage

Features

  • Parse Rust source code using the syn crate
  • 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

Usage

As a Binary

# Analyze a Rust source file
cargo run <rust_file.rs>

# Example
cargo run src/main.rs

As a Library

use 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 Examples

# Run the usage example
cargo run --example usage

API

parse_rust_file(path: &Path) -> Result<AstVisitor>

Parses a Rust source file and returns an AstVisitor containing the parsed information.

analyze_rust_file(path: &Path) -> Result<String>

High-level function that analyzes a Rust file and returns formatted output as a string.

AstVisitor

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)

Building

# Build the library and binary
cargo build

# Build in release mode
cargo build --release

Testing

# Run tests
cargo test

# Run with output
cargo test -- --nocapture

About

CLI tool for analyzing rust source code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages