codeprism_lang_python/
adapter.rs1use crate::parser::{ParseContext as PyParseContext, PythonParser};
4use crate::types as py_types;
5
6pub struct PythonLanguageParser {
8 parser: std::sync::Mutex<PythonParser>,
9}
10
11impl PythonLanguageParser {
12 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
26pub 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
42pub 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}