Skip to main content

deagle_parse/
lib.rs

1//! deagle-parse — tree-sitter based code parser.
2//!
3//! Extracts code entities (functions, structs, traits, impls, imports)
4//! from source files using tree-sitter grammars.
5//!
6//! ## Feature Flags
7//!
8//! - `pattern` — structural pattern matching via [ast-grep-core](https://crates.io/crates/ast-grep-core)
9
10pub mod rust_parser;
11pub mod python_parser;
12
13#[cfg(feature = "pattern")]
14pub mod pattern;
15
16#[cfg(feature = "text-search")]
17pub mod text_search;
18
19use deagle_core::{Language, Node, Result};
20use std::path::Path;
21
22pub use rust_parser::ParseResult;
23
24/// Parse a source file and extract code entities.
25pub fn parse_file(path: &Path, content: &str, language: Language) -> Result<Vec<Node>> {
26    match language {
27        Language::Rust => rust_parser::parse(path, content),
28        Language::Python => python_parser::parse(path, content),
29        _ => Ok(Vec::new()),
30    }
31}
32
33/// Parse with edge extraction — returns nodes and relationship tuples.
34pub fn parse_file_with_edges(path: &Path, content: &str, language: Language) -> Result<ParseResult> {
35    match language {
36        Language::Rust => rust_parser::parse_with_edges(path, content),
37        Language::Python => python_parser::parse_with_edges(path, content),
38        _ => Ok(ParseResult { nodes: Vec::new(), edges: Vec::new() }),
39    }
40}
41
42/// Detect language from file path and parse.
43pub fn parse_auto(path: &Path, content: &str) -> Result<Vec<Node>> {
44    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
45    let lang = Language::from_extension(ext);
46    parse_file(path, content, lang)
47}