Skip to main content

normalize_languages/
capnp.rs

1//! Cap'n Proto schema support.
2
3use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Cap'n Proto language support.
7pub struct Capnp;
8
9impl Language for Capnp {
10    fn name(&self) -> &'static str {
11        "Cap'n Proto"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["capnp"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "capnp"
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() != "import" {
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        // Cap'n Proto: using import "file.capnp"
42        format!("using import \"{}\"", 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
50impl LanguageSymbols for Capnp {}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::validate_unused_kinds_audit;
56
57    #[test]
58    fn unused_node_kinds_audit() {
59        #[rustfmt::skip]
60        let documented_unused: &[&str] = &[
61            // Type-related
62            "type_identifier", "type_definition", "primitive_type", "list_type",
63            "custom_type", "field_type", "extend_type",
64            // Method-related
65            "method_identifier", "method_parameters", "return_type", "return_identifier",
66            "named_return_type", "named_return_types", "unnamed_return_type", "param_identifier",
67            // Struct/enum-related
68            "nested_struct", "nested_enum", "enum_field", "enum_member", "enum_identifier",
69            "field_identifier", "struct_shorthand",
70            // Import-related
71            "import_path", "import_using",
72            // Other
73            "const_identifier", "generic_identifier", "annotation_identifier",
74            "annotation_definition_identifier", "unique_id_statement",
75            "top_level_annotation_body", "block_text",
76            // covered by tags.scm
77            "enum",
78            "interface",
79            "method",
80            "struct",
81            "import",
82        ];
83        validate_unused_kinds_audit(&Capnp, documented_unused)
84            .expect("Cap'n Proto unused node kinds audit failed");
85    }
86}