Skip to main content

code_analyze_mcp/languages/
rust.rs

1use tree_sitter::Node;
2
3/// Tree-sitter query for extracting Rust elements (functions and structs/enums/traits).
4pub const ELEMENT_QUERY: &str = r"
5(function_item
6  name: (identifier) @func_name
7  parameters: (parameters) @params) @function
8(struct_item) @class
9(enum_item) @class
10(trait_item) @class
11";
12
13/// Tree-sitter query for extracting function calls.
14pub const CALL_QUERY: &str = r"
15(call_expression function: (identifier) @call)
16(call_expression function: (field_expression field: (field_identifier) @call))
17(call_expression function: (scoped_identifier name: (identifier) @call))
18";
19
20/// Tree-sitter query for extracting type references.
21pub const REFERENCE_QUERY: &str = r"
22(type_identifier) @type_ref
23";
24
25/// Tree-sitter query for extracting imports.
26pub const IMPORT_QUERY: &str = r"
27(use_declaration argument: (_) @import_path) @import
28";
29
30/// Tree-sitter query for extracting `impl Trait for Type` blocks.
31/// Captures the trait name and the concrete implementor type.
32// Note: matches only simple trait names (type_identifier). Scoped traits
33// (e.g. `impl io::Sink for T`) are not matched; scoped coverage is out of scope for v1.
34pub const IMPL_TRAIT_QUERY: &str = r"
35(impl_item
36  trait: (type_identifier) @trait_name
37  type: (type_identifier) @impl_type)
38";
39
40/// Tree-sitter query for extracting impl blocks and methods.
41pub const IMPL_QUERY: &str = r"
42(impl_item
43  type: (type_identifier) @impl_type
44  body: (declaration_list
45    (function_item
46      name: (identifier) @method_name
47      parameters: (parameters) @method_params) @method))
48";
49
50/// Extract function name from a function node.
51#[must_use]
52pub fn extract_function_name(node: &Node, source: &str, _query_name: &str) -> Option<String> {
53    if node.kind() != "function_item" {
54        return None;
55    }
56    node.child_by_field_name("name").and_then(|n| {
57        let start = n.start_byte();
58        let end = n.end_byte();
59        if end <= source.len() {
60            Some(source[start..end].to_string())
61        } else {
62            None
63        }
64    })
65}
66
67/// Find method name for a receiver type.
68#[must_use]
69pub fn find_method_for_receiver(
70    node: &Node,
71    source: &str,
72    _depth: Option<usize>,
73) -> Option<String> {
74    if node.kind() != "method_item" && node.kind() != "function_item" {
75        return None;
76    }
77    node.child_by_field_name("name").and_then(|n| {
78        let start = n.start_byte();
79        let end = n.end_byte();
80        if end <= source.len() {
81            Some(source[start..end].to_string())
82        } else {
83            None
84        }
85    })
86}
87
88/// Find receiver type for a method.
89#[must_use]
90pub fn find_receiver_type(node: &Node, source: &str) -> Option<String> {
91    if node.kind() != "impl_item" {
92        return None;
93    }
94    node.child_by_field_name("type").and_then(|n| {
95        let start = n.start_byte();
96        let end = n.end_byte();
97        if end <= source.len() {
98            Some(source[start..end].to_string())
99        } else {
100            None
101        }
102    })
103}
104
105/// Extract inheritance information from a Rust class node.
106/// Rust class nodes (`struct_item`, `enum_item`, `trait_item`) have no syntactic inheritance.
107/// Inheritance is via `impl` blocks, not on the type declaration itself.
108#[must_use]
109pub fn extract_inheritance(_node: &Node, _source: &str) -> Vec<String> {
110    Vec::new()
111}