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