12 releases
Uses new Rust 2024
| 0.3.13 | Jan 24, 2026 |
|---|---|
| 0.3.12 | Jan 22, 2026 |
| 0.3.4 | Dec 31, 2025 |
| 0.1.0 | Dec 27, 2025 |
#2035 in Parser implementations
Used in herolib-do
235KB
4.5K
SLoC
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:
[dependencies]
herolib-code = "0.1.0"
# With AI analysis support
herolib-code = { version = "0.1.0", features = ["ai"] }
Usage
Parse a Directory
use herolib_code::parser::CodebaseParser;
let parser = CodebaseParser::new();
let codebase = parser.parse_directory("./src").unwrap();
// Iterate over structs
for struct_info in &codebase.structs {
println!("Struct: {}", struct_info.name);
if let Some(doc) = &struct_info.doc_comment {
println!(" Doc: {}", doc);
}
for field in &struct_info.fields {
if let Some(name) = &field.name {
println!(" Field: {} ({})", name, field.ty);
}
}
for method in &struct_info.methods {
println!(" Method: {}", method.name);
}
}
// Iterate over enums
for enum_info in &codebase.enums {
println!("Enum: {}", enum_info.name);
for variant in &enum_info.variants {
println!(" Variant: {}", variant.name);
}
}
Parse a Single File
use herolib_code::parser::CodebaseParser;
let parser = CodebaseParser::new();
let codebase = parser.parse_file("./src/lib.rs").unwrap();
Parse Source Code String
use herolib_code::parser::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 = CodebaseParser::new();
let codebase = parser.parse_source(source, "point.rs").unwrap();
Configuration
use herolib_code::parser::{CodebaseParser, WalkerConfig};
// Configure directory walking
let walker_config = WalkerConfig::new()
.skip_dir("tests")
.skip_dir("examples")
.max_depth(Some(5));
// Configure parsing
let parser = CodebaseParser::new()
.walker_config(walker_config)
.include_private(false) // Only public items
.continue_on_error(true); // Skip files with syntax errors
Data Structures
CodeBase
The main container for parsed code:
pub struct CodeBase {
pub enums: Vec<EnumInfo>,
pub structs: Vec<StructInfo>,
pub files: Vec<FileInfo>,
}
StructInfo
Information about a parsed struct:
pub struct StructInfo {
pub name: String,
pub doc_comment: Option<String>,
pub file_path: String,
pub line_number: usize,
pub visibility: Visibility,
pub generics: Vec<String>,
pub derives: Vec<String>,
pub attributes: Vec<String>,
pub fields: Vec<FieldInfo>,
pub methods: Vec<MethodInfo>,
}
EnumInfo
Information about a parsed enum:
pub struct EnumInfo {
pub name: String,
pub doc_comment: Option<String>,
pub file_path: String,
pub line_number: usize,
pub visibility: Visibility,
pub generics: Vec<String>,
pub derives: Vec<String>,
pub attributes: Vec<String>,
pub variants: Vec<EnumVariant>,
}
MethodInfo
Information about a method:
pub struct MethodInfo {
pub name: String,
pub doc_comment: Option<String>,
pub file_path: String,
pub line_number: usize,
pub visibility: Visibility,
pub is_async: bool,
pub is_const: bool,
pub is_unsafe: bool,
pub generics: Vec<String>,
pub parameters: Vec<ParameterInfo>,
pub return_type: Option<String>,
pub receiver: Option<Receiver>,
}
AI-Powered Analysis
Enable the ai feature for AI-powered code analysis:
use herolib_code::{CodeAnalyzer, CodebaseParser};
// Parse the codebase
let parser = CodebaseParser::new();
let codebase = parser.parse_directory("./src").unwrap();
// Create analyzer (uses environment variables for API keys)
let analyzer = CodeAnalyzer::from_env();
// Generate documentation for a struct
if let Some(struct_info) = codebase.structs.first() {
let docs = analyzer.document_struct(struct_info).unwrap();
println!("{}", docs);
}
// Summarize the entire codebase
let summary = analyzer.summarize_codebase(&codebase).unwrap();
println!("{}", summary);
Required environment variables (at least one):
GROQ_API_KEYOPENROUTER_API_KEYSAMBANOVA_API_KEY
Building
./build.sh
Testing
./run.sh
License
Apache-2.0
Dependencies
~29–46MB
~772K SLoC