Skip to main content

normalize_languages/
toml.rs

1//! TOML language support.
2
3use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8/// TOML language support.
9pub struct Toml;
10
11impl Language for Toml {
12    fn name(&self) -> &'static str {
13        "TOML"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["toml"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "toml"
20    }
21
22    fn has_symbols(&self) -> bool {
23        false
24    }
25
26    // TOML is config, not code - no functions/types/control flow
27    fn container_kinds(&self) -> &'static [&'static str] {
28        &["table", "table_array_element"]
29    }
30    fn function_kinds(&self) -> &'static [&'static str] {
31        &[]
32    }
33    fn type_kinds(&self) -> &'static [&'static str] {
34        &[]
35    }
36    fn import_kinds(&self) -> &'static [&'static str] {
37        &[]
38    }
39    fn public_symbol_kinds(&self) -> &'static [&'static str] {
40        &[]
41    }
42    fn visibility_mechanism(&self) -> VisibilityMechanism {
43        VisibilityMechanism::NotApplicable
44    }
45    fn scope_creating_kinds(&self) -> &'static [&'static str] {
46        &[]
47    }
48    fn control_flow_kinds(&self) -> &'static [&'static str] {
49        &[]
50    }
51    fn complexity_nodes(&self) -> &'static [&'static str] {
52        &[]
53    }
54    fn nesting_nodes(&self) -> &'static [&'static str] {
55        &[]
56    }
57
58    fn signature_suffix(&self) -> &'static str {
59        ""
60    }
61
62    fn extract_function(
63        &self,
64        _node: &Node,
65        _content: &str,
66        _in_container: bool,
67    ) -> Option<Symbol> {
68        None
69    }
70
71    fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
72        let mut cursor = node.walk();
73        for child in node.children(&mut cursor) {
74            if child.kind() == "bare_key"
75                || child.kind() == "dotted_key"
76                || child.kind() == "quoted_key"
77            {
78                let name = content[child.byte_range()].to_string();
79                return Some(Symbol {
80                    name: name.clone(),
81                    kind: SymbolKind::Module,
82                    signature: format!("[{}]", name),
83                    docstring: None,
84                    attributes: Vec::new(),
85                    start_line: node.start_position().row + 1,
86                    end_line: node.end_position().row + 1,
87                    visibility: Visibility::Public,
88                    children: Vec::new(),
89                    is_interface_impl: false,
90                    implements: Vec::new(),
91                });
92            }
93        }
94        None
95    }
96
97    fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
98        None
99    }
100    fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
101        None
102    }
103
104    fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
105        Vec::new()
106    }
107    fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
108        Vec::new()
109    }
110
111    fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
112        // TOML has no imports
113        String::new()
114    }
115    fn extract_public_symbols(&self, _node: &Node, _content: &str) -> Vec<Export> {
116        Vec::new()
117    }
118
119    fn is_public(&self, _node: &Node, _content: &str) -> bool {
120        true
121    }
122    fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
123        Visibility::Public
124    }
125
126    fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
127        false
128    }
129
130    fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
131        None
132    }
133
134    fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
135        None
136    }
137    fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
138        false
139    }
140    fn node_name<'a>(&self, _node: &Node, _content: &'a str) -> Option<&'a str> {
141        None
142    }
143
144    fn file_path_to_module_name(&self, _: &Path) -> Option<String> {
145        None
146    }
147    fn module_name_to_paths(&self, _: &str) -> Vec<String> {
148        Vec::new()
149    }
150
151    fn lang_key(&self) -> &'static str {
152        ""
153    }
154    fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
155        None
156    }
157    fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
158        None
159    }
160    fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
161        false
162    }
163    fn get_version(&self, _: &Path) -> Option<String> {
164        None
165    }
166    fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
167        None
168    }
169    fn indexable_extensions(&self) -> &'static [&'static str] {
170        &[]
171    }
172    fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
173        None
174    }
175    fn package_module_name(&self, name: &str) -> String {
176        name.to_string()
177    }
178    fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
179        Vec::new()
180    }
181    fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
182        Vec::new()
183    }
184    fn find_package_entry(&self, _: &Path) -> Option<PathBuf> {
185        None
186    }
187
188    fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
189        use crate::traits::{has_extension, skip_dotfiles};
190        if skip_dotfiles(name) {
191            return true;
192        }
193        !is_dir && !has_extension(name, self.indexable_extensions())
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200    use crate::validate_unused_kinds_audit;
201
202    #[test]
203    fn unused_node_kinds_audit() {
204        // TOML has no "interesting" unused kinds matching our patterns
205        let documented_unused: &[&str] = &[];
206        validate_unused_kinds_audit(&Toml, documented_unused)
207            .expect("TOML unused node kinds audit failed");
208    }
209}