Function parse_file

Source
pub async fn parse_file(
    file_path: &str,
    language: Language,
) -> Result<ParsedFile, Error>
Expand description

Parse a single source code file and extract code constructs

This function reads a source code file, parses it using tree-sitter, and extracts all identifiable code constructs (functions, classes, etc.).

§Arguments

  • file_path - Path to the source code file to parse
  • language - The programming language of the file

§Returns

Returns a ParsedFile containing all extracted constructs and metadata, or an Error if parsing fails.

§Examples

use tree_parser::{parse_file, Language};
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let result = parse_file("src/main.rs", Language::Rust).await?;
    println!("Found {} constructs", result.constructs.len());
    Ok(())
}

§Errors

This function will return an error if:

  • The file cannot be read (I/O error)
  • The file content cannot be parsed (syntax error)
  • The specified language is not supported