bronzite/
lib.rs

1//! # 🔮 Bronzite - Compile-time Type Reflection for Rust
2//!
3//! Bronzite provides powerful compile-time access to type information, trait implementations,
4//! method bodies, and more. It enables proc-macros to introspect Rust code using a daemon
5//! that leverages rustc internals.
6//!
7//! ## ✨ Quick Start
8//!
9//! ### High-Level Reflection API (Recommended)
10//!
11//! The v0.2 API provides an ergonomic, type-safe interface for exploring types:
12//!
13//! ```ignore
14//! use bronzite_client::Crate;
15//!
16//! #[proc_macro]
17//! pub fn my_macro(input: TokenStream) -> TokenStream {
18//!     // Connect to daemon and reflect on a crate
19//!     let krate = Crate::reflect("my_crate").unwrap();
20//!
21//!     // Get a struct and explore it
22//!     let user = krate.get_struct("User").unwrap();
23//!
24//!     // Navigate to fields
25//!     for field in user.fields().unwrap() {
26//!         println!("{}: {}", field.name.unwrap(), field.ty);
27//!
28//!         // Navigate to field's type definition
29//!         if let Some(field_type) = field.type_def().unwrap() {
30//!             println!("  -> defined in: {}", field_type.path());
31//!         }
32//!     }
33//!
34//!     // Check trait implementations
35//!     if user.implements("Debug").unwrap() {
36//!         println!("User implements Debug!");
37//!     }
38//!
39//!     // Get methods
40//!     for method in user.methods().unwrap() {
41//!         println!("Method: {}", method.name);
42//!         if let Some(body) = &method.body_source {
43//!             println!("  Body: {}", body);
44//!         }
45//!     }
46//!
47//!     // Generate code based on discoveries
48//!     quote! { /* ... */ }.into()
49//! }
50//! ```
51//!
52//! ### Pattern Matching
53//!
54//! Query types using intuitive glob patterns:
55//!
56//! ```ignore
57//! use bronzite_client::Crate;
58//!
59//! let krate = Crate::reflect("my_crate")?;
60//!
61//! // Exact match
62//! let user = krate.get_struct("User")?;
63//!
64//! // Single-level glob: matches "foo::Bar" but not "foo::bar::Baz"
65//! let items = krate.items("mymod::*")?;
66//!
67//! // Recursive glob: matches all descendants
68//! let all_items = krate.items("mymod::**")?;
69//!
70//! // Get only specific types
71//! let structs = krate.structs("*")?;
72//! let enums = krate.enums("*")?;
73//! let traits = krate.traits("*")?;
74//! ```
75//!
76//! ### Built-in Proc-Macros
77//!
78//! For simple use cases, use the built-in macros:
79//!
80//! ```ignore
81//! use bronzite::bronzite_trait_names;
82//!
83//! // Get trait implementations as a const array
84//! const USER_TRAITS: &[&str] = bronzite_trait_names!("my_crate", "User");
85//! // Expands to: &["Debug", "Clone", "Serialize", ...]
86//! ```
87//!
88//! ## 🏗️ Architecture
89//!
90//! Bronzite consists of several components working together:
91//!
92//! - **[`bronzite_client`]**: High-level reflection API and low-level RPC client
93//! - **`bronzite-daemon`**: Background daemon that caches rustc compilation results
94//! - **[`bronzite_macros`]**: Ready-to-use proc-macros for common reflection tasks
95//! - **`bronzite-query`**: Rustc plugin that extracts type information
96//! - **[`bronzite_types`]**: Shared types for the IPC protocol
97//!
98//! ## 📦 Installation
99//!
100//! Add to your `Cargo.toml`:
101//!
102//! ```toml
103//! [dependencies]
104//! bronzite = "0.2"
105//! bronzite-client = "0.2"  # For proc-macro development
106//! ```
107//!
108//! Install the daemon:
109//!
110//! ```bash
111//! cargo install bronzite
112//! rustup toolchain install nightly-2025-08-20
113//! ```
114//!
115//! The daemon auto-starts when you use the reflection API.
116//!
117//! ## 🔍 API Overview
118//!
119//! ### Navigation Methods
120//!
121//! The high-level API allows fluent navigation between related types:
122//!
123//! - **Struct → Fields → Field Types**: `struct.fields()` → `field.type_def()`
124//! - **Struct → Methods → Return Types**: `struct.methods()` → `method.return_type_def()`
125//! - **Trait → Implementors**: `trait.implementors()`
126//! - **Type Alias → Concrete Type**: `alias.resolve()`
127//!
128//! ### Type Information
129//!
130//! Access comprehensive type metadata:
131//!
132//! - Struct fields with types, visibility, and layout
133//! - Enum variants with discriminants
134//! - Trait methods with signatures and default implementations
135//! - Method bodies as source code
136//! - Generic parameters and where clauses
137//! - Documentation comments
138//!
139//! ## 📚 Learn More
140//!
141//! - [High-level API documentation](bronzite_client::reflection)
142//! - [Built-in macros](bronzite_macros)
143//! - [GitHub Repository](https://github.com/drewridley/bronzite)
144//!
145//! ## 🎯 Use Cases
146//!
147//! - **Derive macro helpers**: Generate implementations based on field types
148//! - **Validation**: Check trait bounds at compile time
149//! - **Code generation**: Generate boilerplate based on type structure
150//! - **Static analysis**: Analyze type relationships in proc-macros
151//! - **Documentation tools**: Extract and process doc comments
152
153// Re-export the high-level reflection API
154pub use bronzite_client::reflection::{
155    Crate, EnumDef, Field, Item, Method, StructDef, TraitDef, TraitImpl, TraitMethod, TypeAliasDef,
156    UnionDef,
157};
158
159// Re-export the low-level client for advanced use
160pub use bronzite_client::{
161    BronziteClient, Error, Result, connect, connect_for_workspace, connect_or_start,
162    ensure_daemon_running, ensure_daemon_running_with_timeout, is_daemon_running,
163};
164
165// Re-export the built-in proc-macros
166pub use bronzite_macros::{
167    bronzite_crate_traits, bronzite_field_names, bronzite_implementors, bronzite_implements,
168    bronzite_method_names, bronzite_resolve_alias, bronzite_trait_names,
169};
170
171// Re-export common types for working with query results
172pub use bronzite_types::{
173    AssocConstInfo, AssocTypeInfo, EnumVariantInfo, FieldInfo, FunctionSignature, GenericParam,
174    GenericParamKind, InherentImplDetails, ItemInfo, ItemKind, LayoutInfo, MethodDetails,
175    MethodSummary, TraitDetails, TraitImplDetails, TraitInfo, TraitMethodInfo, TypeAliasInfo,
176    TypeDetails, TypeKind, TypeSummary, Visibility,
177};