herolib-code
Code analysis and parsing utilities for Rust source files.
Overview
This crate provides tools for analyzing Rust codebases, extracting structured information about code elements such as:
- Enumerators: With variants, documentation, and attributes
- Structs: With fields, documentation, and attributes
- Methods: As implemented on structs, with parameters and documentation
- AI Analysis (optional): AI-powered code analysis and documentation generation
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
# With AI analysis support
= { = "0.1.0", = ["ai"] }
Usage
Parse a Directory
use CodebaseParser;
let parser = new;
let codebase = parser.parse_directory.unwrap;
// Iterate over structs
for struct_info in &codebase.structs
// Iterate over enums
for enum_info in &codebase.enums
Parse a Single File
use CodebaseParser;
let parser = new;
let codebase = parser.parse_file.unwrap;
Parse Source Code String
use CodebaseParser;
let source = r#"
/// A point in 2D space.
pub struct Point {
/// X coordinate.
pub x: f64,
/// Y coordinate.
pub y: f64,
}
impl Point {
/// Creates a new point.
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
"#;
let parser = new;
let codebase = parser.parse_source.unwrap;
Configuration
use ;
// Configure directory walking
let walker_config = new
.skip_dir
.skip_dir
.max_depth;
// Configure parsing
let parser = new
.walker_config
.include_private // Only public items
.continue_on_error; // Skip files with syntax errors
Data Structures
CodeBase
The main container for parsed code:
StructInfo
Information about a parsed struct:
EnumInfo
Information about a parsed enum:
MethodInfo
Information about a method:
AI-Powered Analysis
Enable the ai feature for AI-powered code analysis:
use ;
// Parse the codebase
let parser = new;
let codebase = parser.parse_directory.unwrap;
// Create analyzer (uses environment variables for API keys)
let analyzer = from_env;
// Generate documentation for a struct
if let Some = codebase.structs.first
// Summarize the entire codebase
let summary = analyzer.summarize_codebase.unwrap;
println!;
Required environment variables (at least one):
GROQ_API_KEYOPENROUTER_API_KEYSAMBANOVA_API_KEY
Building
Testing
License
Apache-2.0