treesitter_types_rust/lib.rs
1//! Strongly-typed AST types for Rust, auto-generated from
2//! [`tree-sitter-rust`](https://docs.rs/tree-sitter-rust)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [Rust compiler](https://github.com/rust-lang/rust) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_rust::*;
17//!
18//! // A minimal Rust hello-world program.
19//! let src = b"\
20//! fn main() {
21//! println!(\"Hello, World!\");
22//! }
23//! ";
24//!
25//! // Parse the source with tree-sitter and convert into typed AST.
26//! let mut parser = tree_sitter::Parser::new();
27//! parser.set_language(&tree_sitter_rust::LANGUAGE.into()).unwrap();
28//! let tree = parser.parse(src, None).unwrap();
29//! let source_file = SourceFile::from_node(tree.root_node(), src).unwrap();
30//!
31//! // The source file has one top-level child: a function definition.
32//! assert_eq!(source_file.children.len(), 1);
33//!
34//! // Unwrap the function item through the declaration statement wrapper.
35//! let SourceFileChildren::DeclarationStatement(decl) = &source_file.children[0] else {
36//! panic!("expected a declaration statement");
37//! };
38//! let DeclarationStatement::FunctionItem(func) = decl.as_ref() else {
39//! panic!("expected a function item");
40//! };
41//!
42//! // Check function metadata.
43//! let FunctionItemName::Identifier(name) = &func.name else {
44//! panic!("expected an identifier");
45//! };
46//! assert_eq!(name.text(), "main");
47//! assert!(func.parameters.children.is_empty()); // no parameters
48//! assert!(func.return_type.is_none()); // no return type
49//!
50//! // The body contains one statement: the `println!` macro invocation.
51//! assert_eq!(func.body.children.len(), 1);
52//! ```
53
54pub use tree_sitter_rust;
55pub use treesitter_types::tree_sitter;
56pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
57
58mod generated;
59pub use generated::*;