codeprism_lang_python/
adapter.rs

1//! Adapter to integrate Python parser with codeprism
2
3use crate::parser::{ParseContext as PyParseContext, PythonParser};
4use crate::types as py_types;
5
6/// Adapter that implements codeprism's LanguageParser trait
7pub struct PythonLanguageParser {
8    parser: std::sync::Mutex<PythonParser>,
9}
10
11impl PythonLanguageParser {
12    /// Create a new Python language parser adapter
13    pub fn new() -> Self {
14        Self {
15            parser: std::sync::Mutex::new(PythonParser::new()),
16        }
17    }
18}
19
20impl Default for PythonLanguageParser {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26// Since we can't import codeprism types directly, we'll need to define a conversion
27// trait that the caller can implement
28pub trait ParseResultConverter {
29    type Node;
30    type Edge;
31    type ParseResult;
32
33    fn convert_node(node: py_types::Node) -> Self::Node;
34    fn convert_edge(edge: py_types::Edge) -> Self::Edge;
35    fn create_parse_result(
36        tree: tree_sitter::Tree,
37        nodes: Vec<Self::Node>,
38        edges: Vec<Self::Edge>,
39    ) -> Self::ParseResult;
40}
41
42/// Parse a file and return the result in our internal types
43pub fn parse_file(
44    parser: &PythonLanguageParser,
45    repo_id: &str,
46    file_path: std::path::PathBuf,
47    content: String,
48    old_tree: Option<tree_sitter::Tree>,
49) -> Result<(tree_sitter::Tree, Vec<py_types::Node>, Vec<py_types::Edge>), crate::error::Error> {
50    let context = PyParseContext {
51        repo_id: repo_id.to_string(),
52        file_path,
53        old_tree,
54        content,
55    };
56
57    let mut parser = parser.parser.lock().unwrap();
58    let result = parser.parse(&context)?;
59
60    Ok((result.tree, result.nodes, result.edges))
61}