1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! # Quarry
//!
//! Quarry is a Rust library for mining type information from the Rust standard library.
//! It provides access to struct field information, including private fields, by analyzing
//! the actual standard library installed on your system.
//!
//! ## Scope and Limitations
//!
//! **Current Focus**: Quarry currently analyzes **structs** only. Popular types like `Option<T>`
//! and `Result<T, E>` (which are enums) cannot be analyzed yet.
//!
//! **Planned Features**: Support for enums, traits, and other types is planned for future releases.
//! If you need enum analysis immediately, consider using `rustdoc` directly.
//!
//! ## Requirements
//!
//! - **Nightly Rust Toolchain**: Required for rustdoc JSON generation
//! - **rust-src Component**: Install with `rustup component add rust-src --toolchain nightly`
//!
//! ## Usage Philosophy
//!
//! Quarry requires explicit, full module paths to ensure unambiguous type resolution.
//! Instead of accepting short names like "String", you must specify "alloc::string::String".
//! This design choice eliminates ambiguity and makes your code more explicit about which
//! specific type you're analyzing.
//!
//! ## Example
//!
//! ```rust,no_run
//! use quarry::mine_struct_info;
//!
//! // Analyze the String type from the alloc crate
//! let result = mine_struct_info("alloc::string::String")?;
//! println!("Struct: {}", result.name);
//! println!("Simple name: {}", result.simple_name);
//! println!("Module path: {}", result.module_path);
//!
//! // Access field information (including private fields)
//! for field in result.fields {
//! println!(" Field: {} -> {} (public: {})",
//! field.name, field.type_name, field.is_public);
//! }
//!
//! // List all available types
//! let all_types = quarry::list_stdlib_structs()?;
//! println!("Found {} standard library struct types", all_types.len());
//! # Ok::<(), quarry::QuarryError>(())
//! ```
//!
//! ## Debug Logging
//!
//! Quarry provides detailed debug logging to help understand the standard library analysis process.
//! To enable debug output:
//!
//! 1. **Add a logger to your Cargo.toml**:
//! ```toml
//! [dependencies]
//! env_logger = "0.11"
//! ```
//!
//! 2. **Initialize the logger in your code**:
//! ```rust,no_run
//! fn main() {
//! env_logger::init();
//! // ... your code using quarry
//! }
//! ```
//!
//! 3. **Run with debug environment variables**:
//! - `RUST_LOG=debug` - Show all debug messages
//! - `RUST_LOG=quarry=debug` - Show only Quarry debug messages
//! - `RUST_LOG=quarry::stdlib=debug` - Show only stdlib module debug messages
//!
//! Example: `RUST_LOG=quarry=debug cargo run`
use debug;
use ;
use Error;
/// Errors that can occur when mining standard library type information
pub type Result<T> = Result;
/// Complete information about a struct
/// Information about a struct field
/// Mine struct information from the Rust standard library
///
/// This function queries the standard library cache for information about a specific struct.
/// It requires the full module path to ensure unambiguous type resolution (e.g.,
/// "alloc::string::String" rather than just "String").
///
/// # Arguments
///
/// * `name` - The full module path of the struct (e.g., "alloc::string::String")
///
/// # Examples
///
/// ```rust,no_run
/// use quarry::mine_struct_info;
///
/// // Standard library struct with full path
/// let string_info = mine_struct_info("alloc::string::String")?;
/// println!("Struct: {}", string_info.name);
/// println!("Fields: {}", string_info.fields.len());
///
/// // Vec from alloc crate
/// let vec_info = mine_struct_info("alloc::vec::Vec")?;
/// println!("Is tuple struct: {}", vec_info.is_tuple_struct);
///
/// // HashMap from std collections
/// let map_info = mine_struct_info("std::collections::HashMap")?;
/// for field in &map_info.fields {
/// println!(" Field: {} -> {}", field.name, field.type_name);
/// }
/// # Ok::<(), quarry::QuarryError>(())
/// ```
///
/// # Errors
///
/// Returns `QuarryError::TypeNotFound` if the specified struct is not found in the
/// standard library cache. Make sure you're using the complete module path.
/// Initialize the standard library cache
///
/// This function forces initialization of the standard library type cache.
/// Normally, the cache is initialized lazily on first use, but this can be
/// called explicitly if you want to handle any initialization errors upfront
/// or warm up the cache for better performance.
///
/// The initialization process analyzes the actual standard library installed
/// on your system using rustdoc JSON generation, which requires the nightly
/// toolchain and rust-src component.
///
/// # Examples
///
/// ```rust,no_run
/// use quarry::init_stdlib_cache;
///
/// // Initialize the cache upfront to handle any errors early
/// init_stdlib_cache()?;
///
/// // Now subsequent calls will be faster
/// let result = quarry::mine_struct_info("alloc::string::String")?;
/// # Ok::<(), quarry::QuarryError>(())
/// ```
///
/// # Errors
///
/// May return errors related to rustdoc JSON generation or standard library
/// analysis. Common issues include missing nightly toolchain or rust-src component.
/// Clear the standard library cache
///
/// This function clears the cached standard library type information.
/// The cache will be rebuilt on the next call to any function that requires it.
/// This can be useful for testing or if you want to refresh the cache
/// after updating your Rust installation.
///
/// # Examples
///
/// ```rust
/// use quarry::clear_stdlib_cache;
///
/// // Clear the cache to force rebuilding
/// clear_stdlib_cache();
///
/// // The next call will rebuild the cache from scratch
/// let result = quarry::mine_struct_info("alloc::string::String");
/// ```
/// Get statistics about the standard library cache
///
/// Returns a tuple of (number_of_cached_types, is_initialized).
///
/// # Examples
///
/// ```rust,no_run
/// use quarry::cache_stats;
///
/// let (count, initialized) = cache_stats()?;
/// println!("Cache contains {} types, initialized: {}", count, initialized);
/// # Ok::<(), quarry::QuarryError>(())
/// ```
/// List all available standard library struct types
///
/// Returns a sorted list of all struct types found in the standard library.
///
/// # Examples
///
/// ```rust,no_run
/// use quarry::list_stdlib_structs;
///
/// let structs = list_stdlib_structs()?;
/// for struct_name in structs.iter().take(10) {
/// println!(" {}", struct_name);
/// }
/// # Ok::<(), quarry::QuarryError>(())
/// ```
/// Check if a type name refers to a standard library struct
///
/// This is a lightweight check that returns true if the given name
/// corresponds to a struct in the standard library. Requires the full
/// module path for accurate results.
///
/// # Examples
///
/// ```rust,no_run
/// use quarry::is_stdlib_struct;
///
/// // These will return true if the types exist in the standard library
/// assert!(is_stdlib_struct("alloc::string::String"));
/// assert!(is_stdlib_struct("alloc::vec::Vec"));
/// assert!(is_stdlib_struct("std::collections::HashMap"));
///
/// // These will return false
/// assert!(!is_stdlib_struct("MyCustomStruct"));
/// assert!(!is_stdlib_struct("some::external::Type"));
/// ```
///
/// # Performance
///
/// This is a fast lookup operation that checks the cache without
/// triggering expensive initialization if the cache is not ready.