Skip to main content

normalize_languages/
thrift.rs

1//! Apache Thrift IDL support.
2
3use crate::{ContainerBody, Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Thrift language support.
7pub struct Thrift;
8
9impl Language for Thrift {
10    fn name(&self) -> &'static str {
11        "Thrift"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["thrift"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "thrift"
18    }
19
20    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21        Some(self)
22    }
23
24    fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
25        if node.kind() != "include_statement" {
26            return Vec::new();
27        }
28
29        let text = &content[node.byte_range()];
30        vec![Import {
31            module: text.trim().to_string(),
32            names: Vec::new(),
33            alias: None,
34            is_wildcard: false,
35            is_relative: false,
36            line: node.start_position().row + 1,
37        }]
38    }
39
40    fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
41        // Thrift: include "file.thrift"
42        format!("include \"{}\"", import.module)
43    }
44
45    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
46        node.child_by_field_name("body")
47    }
48
49    fn analyze_container_body(
50        &self,
51        body_node: &Node,
52        content: &str,
53        inner_indent: &str,
54    ) -> Option<ContainerBody> {
55        crate::body::analyze_brace_body(body_node, content, inner_indent)
56    }
57}
58
59impl LanguageSymbols for Thrift {}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use crate::validate_unused_kinds_audit;
65
66    #[test]
67    fn unused_node_kinds_audit() {
68        #[rustfmt::skip]
69        let documented_unused: &[&str] = &[
70            // Type-related
71            "type", "container_type", "definition_type",
72            // Identifiers
73            "annotation_identifier",
74            // Definitions
75            "senum_definition", "interaction_definition", "annotation_definition",
76            "fb_annotation_definition",
77            // Declarations
78            "namespace_declaration", "package_declaration",
79            // Modifiers
80            "function_modifier", "field_modifier", "exception_modifier",
81            // Other
82            "throws", "struct_literal",
83            // covered by imports.scm
84            "include_statement",
85        ];
86        validate_unused_kinds_audit(&Thrift, documented_unused)
87            .expect("Thrift unused node kinds audit failed");
88    }
89}