Skip to main content

normalize_languages/
ron.rs

1//! RON (Rusty Object Notation) 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/// RON language support.
9pub struct Ron;
10
11impl Language for Ron {
12    fn name(&self) -> &'static str {
13        "RON"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["ron"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "ron"
20    }
21
22    fn has_symbols(&self) -> bool {
23        true
24    }
25
26    fn container_kinds(&self) -> &'static [&'static str] {
27        &["struct", "map"]
28    }
29
30    fn function_kinds(&self) -> &'static [&'static str] {
31        &[]
32    }
33
34    fn type_kinds(&self) -> &'static [&'static str] {
35        &["struct"]
36    }
37
38    fn import_kinds(&self) -> &'static [&'static str] {
39        &[]
40    }
41
42    fn public_symbol_kinds(&self) -> &'static [&'static str] {
43        &["struct", "struct_entry"]
44    }
45
46    fn visibility_mechanism(&self) -> VisibilityMechanism {
47        VisibilityMechanism::AllPublic
48    }
49
50    fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
51        let kind = match node.kind() {
52            "struct" => SymbolKind::Struct,
53            "struct_entry" => SymbolKind::Variable,
54            _ => return Vec::new(),
55        };
56
57        if let Some(name) = self.node_name(node, content) {
58            return vec![Export {
59                name: name.to_string(),
60                kind,
61                line: node.start_position().row + 1,
62            }];
63        }
64        Vec::new()
65    }
66
67    fn scope_creating_kinds(&self) -> &'static [&'static str] {
68        &["struct", "map"]
69    }
70
71    fn control_flow_kinds(&self) -> &'static [&'static str] {
72        &[]
73    }
74    fn complexity_nodes(&self) -> &'static [&'static str] {
75        &[]
76    }
77    fn nesting_nodes(&self) -> &'static [&'static str] {
78        &["struct", "map"]
79    }
80
81    fn signature_suffix(&self) -> &'static str {
82        ""
83    }
84
85    fn extract_function(
86        &self,
87        _node: &Node,
88        _content: &str,
89        _in_container: bool,
90    ) -> Option<Symbol> {
91        None
92    }
93
94    fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
95        if node.kind() != "struct" && node.kind() != "map" {
96            return None;
97        }
98
99        let name = self.node_name(node, content)?;
100        let text = &content[node.byte_range()];
101        let first_line = text.lines().next().unwrap_or(text);
102
103        Some(Symbol {
104            name: name.to_string(),
105            kind: SymbolKind::Struct,
106            signature: first_line.trim().to_string(),
107            docstring: None,
108            attributes: Vec::new(),
109            start_line: node.start_position().row + 1,
110            end_line: node.end_position().row + 1,
111            visibility: Visibility::Public,
112            children: Vec::new(),
113            is_interface_impl: false,
114            implements: Vec::new(),
115        })
116    }
117
118    fn extract_type(&self, node: &Node, content: &str) -> Option<Symbol> {
119        if node.kind() != "struct" {
120            return None;
121        }
122        self.extract_container(node, content)
123    }
124
125    fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
126        None
127    }
128
129    fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
130        Vec::new()
131    }
132
133    fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
134        Vec::new()
135    }
136
137    fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
138        // RON has no imports
139        String::new()
140    }
141
142    fn is_public(&self, _node: &Node, _content: &str) -> bool {
143        true
144    }
145    fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
146        Visibility::Public
147    }
148
149    fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
150        false
151    }
152
153    fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
154        None
155    }
156
157    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
158        node.child_by_field_name("body")
159    }
160
161    fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
162        false
163    }
164
165    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
166        node.child_by_field_name("name")
167            .map(|n| &content[n.byte_range()])
168    }
169
170    fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
171        let ext = path.extension()?.to_str()?;
172        if ext != "ron" {
173            return None;
174        }
175        let stem = path.file_stem()?.to_str()?;
176        Some(stem.to_string())
177    }
178
179    fn module_name_to_paths(&self, module: &str) -> Vec<String> {
180        vec![format!("{}.ron", module)]
181    }
182
183    fn lang_key(&self) -> &'static str {
184        "ron"
185    }
186
187    fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
188        false
189    }
190    fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
191        None
192    }
193    fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
194        None
195    }
196    fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
197        None
198    }
199    fn get_version(&self, _: &Path) -> Option<String> {
200        None
201    }
202    fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
203        None
204    }
205    fn indexable_extensions(&self) -> &'static [&'static str] {
206        &["ron"]
207    }
208    fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
209        Vec::new()
210    }
211
212    fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
213        use crate::traits::{has_extension, skip_dotfiles};
214        if skip_dotfiles(name) {
215            return true;
216        }
217        !is_dir && !has_extension(name, self.indexable_extensions())
218    }
219
220    fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
221        Vec::new()
222    }
223
224    fn package_module_name(&self, entry_name: &str) -> String {
225        entry_name
226            .strip_suffix(".ron")
227            .unwrap_or(entry_name)
228            .to_string()
229    }
230
231    fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
232        if path.is_file() {
233            Some(path.to_path_buf())
234        } else {
235            None
236        }
237    }
238}
239
240#[cfg(test)]
241mod tests {
242    use super::*;
243    use crate::validate_unused_kinds_audit;
244
245    #[test]
246    fn unused_node_kinds_audit() {
247        #[rustfmt::skip]
248        let documented_unused: &[&str] = &[
249            "identifier", "struct_name", "unit_struct", "enum_variant",
250            "map_entry", "block_comment",
251        ];
252        validate_unused_kinds_audit(&Ron, documented_unused)
253            .expect("RON unused node kinds audit failed");
254    }
255}