tree_sitter_symbols_rust/
lib.rs

1//! Generated node type enums and metadata from tree-sitter-rust.
2//!
3//! `tss-rust` provides type-safe Rust enums for every node type in the tree-sitter-rust grammar.
4//! This eliminates error-prone string comparisons like `node.kind() == "function_item"` in favor
5//! of `NodeType::FunctionItem`, enabling compile-time verification and better IDE support.
6//!
7//! # Quick Start
8//!
9//! ```toml
10//! [dependencies]
11//! tss-rust = "0.2"
12//! tree-sitter = "0.24"
13//! tree-sitter-rust = "0.24"
14//! ```
15//!
16//! ```
17//! use tree_sitter_symbols_rust::NodeType;
18//! use std::str::FromStr;
19//!
20//! let node_type = NodeType::from_str("function_item")?;
21//! assert_eq!(node_type, NodeType::FunctionItem);
22//! assert_eq!(node_type.to_string(), "function_item");
23//! # Ok::<(), String>(())
24//! ```
25//!
26//! # Features
27//!
28//! **By default, this crate has no features enabled.** You must explicitly enable the node types
29//! and metadata you need. This keeps compile times fast and binary sizes small.
30//!
31//! The crate uses Cargo features to control which node types and metadata are available:
32//!
33//! ## Node Type Features
34//!
35//! Each tree-sitter node type corresponds to a feature. Enable specific nodes:
36//!
37//! ```toml
38//! tss-rust = { version = "0.2", features = ["function_item", "struct_item"] }
39//! ```
40//!
41//! Or enable all node types at once:
42//!
43//! ```toml
44//! tss-rust = { version = "0.2", features = ["node_full"] }
45//! ```
46//!
47//! The `node` feature is a tracking feature automatically enabled when any node type is active.
48//! You don't need to enable it manually - it's used internally for conditional compilation.
49//!
50//! ## Metadata Features
51//!
52//! Control which metadata from tree-sitter's `NODE_TYPES` JSON is included:
53//!
54//! - `meta_named` - whether nodes are named in the grammar
55//! - `meta_subtypes` - possible subtypes for each node
56//! - `meta_fields` - named fields nodes can have
57//! - `meta_children` - anonymous children nodes can have
58//! - `meta_extra` - extra node markers
59//! - `meta_root` - root node markers
60//!
61//! Enable all metadata:
62//!
63//! ```toml
64//! tss-rust = { version = "0.2", features = ["meta_full"] }
65//! ```
66//!
67//! Or select specific metadata categories:
68//!
69//! ```toml
70//! tss-rust = { version = "0.2", features = ["meta_named", "meta_fields"] }
71//! ```
72//!
73//! The `meta` feature is a tracking feature automatically enabled when any metadata feature is active.
74//! You don't need to enable it manually - it's used internally for conditional compilation.
75//!
76//! ## Complete Feature Matrix
77//!
78//! ```toml
79//! # Default: empty - no features enabled
80//! tss-rust = "0.2"
81//!
82//! # Everything: all nodes + all metadata
83//! tss-rust = { version = "0.2", features = ["full"] }
84//!
85//! # Just nodes, no metadata
86//! tss-rust = { version = "0.2", features = ["node_full"] }
87//!
88//! # Just metadata, no nodes
89//! tss-rust = { version = "0.2", features = ["meta_full"] }
90//!
91//! # Specific nodes with selected metadata
92//! tss-rust = { version = "0.2", features = [
93//!     "function_item",
94//!     "struct_item",
95//!     "meta_named"
96//! ]}
97//! ```
98//!
99//! # How It Works
100//!
101//! At build time, `tss-rust` reads the `NODE_TYPES` constant from the `tree-sitter-rust` crate
102//! and generates:
103//!
104//! - A `NodeType` enum with variants for all 280+ node types
105//! - `FromStr` implementation for parsing node type strings
106//! - `Display` implementation for converting back to strings  
107//! - Documentation linking to Rust language reference where applicable
108//! - Feature-gated compilation so you only pay for what you use
109//!
110//! All generation happens at compile time with zero runtime dependencies. The generated code
111//! is feature-gated, so with no features enabled, the crate compiles to almost nothing.
112//!
113//! # Design Philosophy
114//!
115//! **Type Safety**: String comparisons are error-prone and not checked at compile time. Using
116//! enum variants catches typos early and enables exhaustive pattern matching.
117//!
118//! **Minimal Overhead**: With no default features, you explicitly choose what to include. Add
119//! only the node types you actually use in your tree-sitter traversals.
120//!
121//! **Build-Time Generation**: By generating at build time rather than using macros, we get
122//! better IDE support, faster compile times, and the ability to inspect generated code.
123
124#![allow(clippy::negative_feature_names)]
125#![allow(clippy::redundant_feature_names)]
126
127// The generated code is included from the build script output
128include!(concat!(env!("OUT_DIR"), "/generated.rs"));
129
130#[cfg(any(feature = "function_item", feature = "node_full"))]
131#[cfg(test)]
132mod tests {
133    use super::*;
134    use std::str::FromStr;
135
136    #[cfg(any(feature = "function_item", feature = "node_full"))]
137    #[test]
138    fn test_from_str() {
139        assert_eq!(
140            NodeType::from_str("function_item").unwrap(),
141            NodeType::FunctionItem
142        );
143    }
144
145    #[cfg(any(feature = "function_item", feature = "node_full"))]
146    #[test]
147    fn test_display() {
148        assert_eq!(NodeType::FunctionItem.to_string(), "function_item");
149    }
150}