Skip to main content

dk_engine/parser/langs/
rust.rs

1//! Rust language configuration for the query-driven parser.
2
3use crate::parser::lang_config::{CommentStyle, LanguageConfig};
4use dk_core::{Symbol, Visibility};
5use tree_sitter::Language;
6
7/// Rust language configuration for [`QueryDrivenParser`](crate::parser::engine::QueryDrivenParser).
8pub struct RustConfig;
9
10impl LanguageConfig for RustConfig {
11    fn language(&self) -> Language {
12        tree_sitter_rust::LANGUAGE.into()
13    }
14
15    fn extensions(&self) -> &'static [&'static str] {
16        &["rs"]
17    }
18
19    fn symbols_query(&self) -> &'static str {
20        include_str!("../queries/rust_symbols.scm")
21    }
22
23    fn calls_query(&self) -> &'static str {
24        include_str!("../queries/rust_calls.scm")
25    }
26
27    fn imports_query(&self) -> &'static str {
28        include_str!("../queries/rust_imports.scm")
29    }
30
31    fn comment_style(&self) -> CommentStyle {
32        CommentStyle::TripleSlash
33    }
34
35    fn resolve_visibility(&self, modifiers: Option<&str>, _name: &str) -> Visibility {
36        match modifiers {
37            Some(m) if m.contains("crate") => Visibility::Crate,
38            Some(m) if m.contains("super") => Visibility::Super,
39            Some(_) => Visibility::Public,
40            None => Visibility::Private,
41        }
42    }
43
44    fn adjust_symbol(&self, sym: &mut Symbol, node: &tree_sitter::Node, source: &[u8]) {
45        // For impl_item nodes, construct the full name from trait + type fields.
46        if node.kind() == "impl_item" {
47            let type_name = node.child_by_field_name("type").and_then(|ty| {
48                let text = &source[ty.start_byte()..ty.end_byte()];
49                std::str::from_utf8(text).ok().map(|s| s.to_string())
50            });
51
52            let trait_name = node.child_by_field_name("trait").and_then(|tr| {
53                let text = &source[tr.start_byte()..tr.end_byte()];
54                std::str::from_utf8(text).ok().map(|s| s.to_string())
55            });
56
57            let name = match (trait_name, type_name) {
58                (Some(tr), Some(ty)) => format!("impl {tr} for {ty}"),
59                (None, Some(ty)) => format!("impl {ty}"),
60                _ => "impl".to_string(),
61            };
62
63            sym.name = name.clone();
64            sym.qualified_name = name;
65        }
66    }
67
68    fn is_external_import(&self, module_path: &str) -> bool {
69        !module_path.starts_with("crate")
70            && !module_path.starts_with("super")
71            && !module_path.starts_with("self")
72    }
73}