pub trait CodeParser: Send + Sync {
// Required methods
fn language(&self) -> &str;
fn file_extensions(&self) -> &[&str];
fn parse_file(
&self,
path: &Path,
graph: &mut CodeGraph,
) -> Result<FileInfo, ParserError>;
fn parse_source(
&self,
source: &str,
file_path: &Path,
graph: &mut CodeGraph,
) -> Result<FileInfo, ParserError>;
fn config(&self) -> &ParserConfig;
fn metrics(&self) -> ParserMetrics;
fn reset_metrics(&mut self);
// Provided methods
fn parse_files(
&self,
paths: &[PathBuf],
graph: &mut CodeGraph,
) -> Result<ProjectInfo, ParserError> { ... }
fn parse_directory(
&self,
dir: &Path,
graph: &mut CodeGraph,
) -> Result<ProjectInfo, ParserError> { ... }
fn discover_files(&self, dir: &Path) -> Result<Vec<PathBuf>, ParserError> { ... }
fn can_parse(&self, path: &Path) -> bool { ... }
}Expand description
Core trait that all language parsers must implement
This trait defines the contract for extracting code entities and relationships from source code and inserting them into a CodeGraph database.
§Thread Safety
Implementations must be Send + Sync to support parallel parsing.
§Example
use codegraph_parser_api::{CodeParser, ParserConfig};
use codegraph::CodeGraph;
struct MyParser {
config: ParserConfig,
}
impl CodeParser for MyParser {
fn language(&self) -> &str {
"mylang"
}
fn file_extensions(&self) -> &[&str] {
&[".my"]
}
// ... implement other required methods
}Required Methods§
Sourcefn file_extensions(&self) -> &[&str]
fn file_extensions(&self) -> &[&str]
Returns supported file extensions (e.g., [“.py”, “.pyw”])
Sourcefn parse_file(
&self,
path: &Path,
graph: &mut CodeGraph,
) -> Result<FileInfo, ParserError>
fn parse_file( &self, path: &Path, graph: &mut CodeGraph, ) -> Result<FileInfo, ParserError>
Parse a single file and insert entities/relationships into the graph
Note on Metrics: This method updates parser metrics
(files_attempted, files_succeeded, etc.). Use metrics() to retrieve
statistics after parsing operations.
§Arguments
path- Path to the source filegraph- Mutable reference to the CodeGraph database
§Returns
FileInfo containing metadata about parsed entities
§Errors
Returns ParserError if:
- File cannot be read
- Source code has syntax errors
- Graph insertion fails
Sourcefn parse_source(
&self,
source: &str,
file_path: &Path,
graph: &mut CodeGraph,
) -> Result<FileInfo, ParserError>
fn parse_source( &self, source: &str, file_path: &Path, graph: &mut CodeGraph, ) -> Result<FileInfo, ParserError>
Parse source code string and insert into graph
Useful for parsing code snippets or in-memory source.
Note on Metrics: This method does NOT update parser metrics
(files_attempted, files_succeeded, etc.). Only parse_file() updates
metrics to avoid double-counting when parse_source() is called
internally by parse_file().
§Arguments
source- Source code stringfile_path- Logical path for this source (used for graph nodes)graph- Mutable reference to the CodeGraph database
Sourcefn config(&self) -> &ParserConfig
fn config(&self) -> &ParserConfig
Get parser configuration
Sourcefn metrics(&self) -> ParserMetrics
fn metrics(&self) -> ParserMetrics
Get accumulated metrics
Returns current parsing metrics (files processed, time taken, etc.)
Sourcefn reset_metrics(&mut self)
fn reset_metrics(&mut self)
Reset metrics
Clears accumulated metrics. Useful for benchmarking.
Provided Methods§
Sourcefn parse_files(
&self,
paths: &[PathBuf],
graph: &mut CodeGraph,
) -> Result<ProjectInfo, ParserError>
fn parse_files( &self, paths: &[PathBuf], graph: &mut CodeGraph, ) -> Result<ProjectInfo, ParserError>
Parse multiple files (can be overridden for parallel parsing)
Default implementation parses files sequentially. Override this for parallel parsing implementation.
§Arguments
paths- List of file paths to parsegraph- Mutable reference to the CodeGraph database
§Returns
ProjectInfo containing aggregate statistics
Sourcefn parse_directory(
&self,
dir: &Path,
graph: &mut CodeGraph,
) -> Result<ProjectInfo, ParserError>
fn parse_directory( &self, dir: &Path, graph: &mut CodeGraph, ) -> Result<ProjectInfo, ParserError>
Parse a directory recursively
§Arguments
dir- Directory path to parsegraph- Mutable reference to the CodeGraph database
Sourcefn discover_files(&self, dir: &Path) -> Result<Vec<PathBuf>, ParserError>
fn discover_files(&self, dir: &Path) -> Result<Vec<PathBuf>, ParserError>
Discover parseable files in a directory
Default implementation walks the directory and filters by extension. Can be overridden for custom discovery logic.