codegraph-rust
Rust parser for CodeGraph - extracts code entities and relationships from Rust source files.
Features
- ✅ Parse Rust source files and extract:
- Functions (including async functions)
- Structs and enums
- Traits and trait implementations
- Modules
- Generic types
- Use statements (imports)
- ✅ Track relationships:
- Function calls
- Trait implementations
- Trait inheritance
- Module imports
- ✅ Full integration with
codegraph-parser-api - ✅ Support for Rust-specific constructs (impl blocks, associated functions, generics)
- ✅ Configurable behavior via
ParserConfig
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.1"
Basic Usage
use CodeGraph;
use CodeParser;
use RustParser;
use Path;
Parse from String
use CodeGraph;
use CodeParser;
use RustParser;
use Path;
let source = r#"
pub struct Person {
pub name: String,
age: u32,
}
impl Person {
pub fn new(name: String, age: u32) -> Self {
Self { name, age }
}
}
"#;
let mut graph = in_memory?;
let parser = new;
let info = parser.parse_source?;
# Ok::
Custom Configuration
use ParserConfig;
use RustParser;
let config = default
.with_max_file_size // 5 MB
.with_parallel;
let parser = with_config;
Parse Multiple Files
use CodeGraph;
use CodeParser;
use RustParser;
use PathBuf;
let mut graph = in_memory?;
let parser = new;
let files = vec!;
let project_info = parser.parse_files?;
println!;
println!;
println!;
# Ok::
Parse Directory
use CodeGraph;
use CodeParser;
use RustParser;
use Path;
let mut graph = in_memory?;
let parser = new;
// Parse all .rs files in src/ directory recursively
let project_info = parser.parse_directory?;
println!;
# Ok::
Supported Rust Constructs
Functions
- ✅ Free functions
- ✅ Associated functions (static methods)
- ✅ Methods (with self parameter)
- ✅ Async functions
- ✅ Generic functions
- ✅ Function parameters with types
- ✅ Return types
Structs and Enums
- ✅ Structs with named fields
- ✅ Tuple structs
- ✅ Unit structs
- ✅ Enums with variants
- ✅ Generic structs and enums
- ✅ Field visibility
Traits
- ✅ Trait definitions
- ✅ Required methods
- ✅ Trait inheritance
- ✅ Generic traits
Implementations
- ✅ Inherent impl blocks
- ✅ Trait impl blocks
- ✅ Generic implementations
Imports
- ✅ Use statements
- ✅ Nested imports
- ✅ Wildcard imports
- ✅ Import aliases
Documentation
- ✅ Doc comments (
///and//!) - ✅ Module-level documentation
Configuration Options
The parser respects all ParserConfig options:
Graph Structure
The parser creates the following node types in the graph:
- File: Represents the Rust source file/module
- Function: Represents functions and methods
- Class: Represents structs and enums
- Trait: Represents trait definitions
- Import: Represents use statements
And the following edge types:
- Contains: File/Class → Function/Field
- Calls: Function → Function
- Implements: Struct/Enum → Trait
- Inherits: Trait → Trait (trait inheritance)
- Imports: File → Import
Performance
The parser uses the syn crate for Rust parsing, which is fast and battle-tested:
- Single file (100 LOC): ~5-10ms
- 1K files: ~5-10 seconds
- 10K files: ~50-100 seconds
Enable parallel parsing for large projects:
let config = default.with_parallel;
let parser = with_config;
Examples
See the tests/ directory for comprehensive examples.
License
Apache-2.0
Contributing
Contributions are welcome! Please see the main CodeGraph repository for guidelines.