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.
Note: The API is currently unstable.
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.
Overview
Quarry dynamically analyzes the Rust standard library installed on your system to extract detailed information about structs, including:
- Field names and types (including private fields)
- Visibility (public/private)
- Struct type (named, tuple, or unit struct)
- Full module path resolution
Requirements
To use the full functionality of Quarry, you need:
-
Nightly Rust toolchain (for rustdoc JSON generation):
-
Rust source code (for analyzing the standard library):
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use mine_struct_info;
Full Module Paths Required
Quarry requires explicit, full module paths to ensure unambiguous type resolution:
// ✅ Correct usage - full module paths
let string_info = mine_struct_info?;
let vec_info = mine_struct_info?;
let hashmap_info = mine_struct_info?;
// ❌ Incorrect usage - will fail
let result = mine_struct_info; // Error: requires full path
let result = mine_struct_info; // Error: requires full path
Cache Management
Quarry caches the analyzed standard library information for performance:
use ;
// Initialize cache explicitly (optional)
init_stdlib_cache?;
// Check cache statistics
let = cache_stats?;
println!;
// Clear cache if needed
clear_stdlib_cache;
Listing Available Types
use list_stdlib_structs;
// Get all available standard library struct types
let structs = list_stdlib_structs?;
for struct_name in structs
Checking Type Availability
use is_stdlib_struct;
// Quick check if a type exists in the standard library
if is_stdlib_struct
Debugging and Logging
Quarry includes comprehensive debug logging throughout the analysis pipeline. This is especially useful for understanding what's happening during cache initialization, type lookup, and rustdoc generation.
Enabling Debug Logs
Quarry uses the standard log crate for logging. To see debug output, add env_logger to your dependencies and initialize it:
[]
= "0.11"
Then run your application with the RUST_LOG environment variable:
# See all debug logs
RUST_LOG=debug
# See only Quarry debug logs
RUST_LOG=quarry=debug
# See only standard library module logs
RUST_LOG=quarry::stdlib=debug
Examples
Quarry includes comprehensive examples that demonstrate its capabilities:
Basic Usage Example
Shows fundamental usage including:
- Simple struct analysis
- Field information extraction
- Error handling
- Basic output formatting
Advanced Usage Example
Demonstrates advanced features including:
- Cache management and statistics
- Bulk type analysis
- Type discovery and filtering
- Performance testing
Both examples can be run with debug logging:
RUST_LOG=quarry=debug
RUST_LOG=quarry=debug
How It Works
Quarry uses the following approach:
- Dynamic Analysis: Uses
rustdocto analyze the actual standard library source code installed on your system - Direct JSON Parsing: Parses the generated
rustdocJSON output directly to extract struct information including private fields - In-Memory Caching: Stores the parsed struct information in a lookup table for fast subsequent queries
- Exact Path Matching: Takes user input as exact module paths (e.g., "alloc::string::String") and looks them up directly in the cache
Architecture
┌─────────────────┐
│ Your Code │ mine_struct_info("alloc::string::String")
└─────────────────┘
│
▼
┌─────────────────┐
│ Quarry │ First call: run cargo doc → parse JSON → populate cache
│ (Public API) │ Subsequent calls: direct lookup in cache
└─────────────────┘
│
▼
┌─────────────────┐ ┌─────────────────┐
│ Memory Cache │ │ Standard Library│
│ (HashMap) │ │ Source Code │
│ "alloc::string │ │ (rust-src) │
│ ::String" → ... │ └─────────────────┘
└─────────────────┘ │
▼
┌─────────────────┐
│ cargo doc │
│ --output-format │
│ json │
└─────────────────┘
Error Handling
Quarry provides detailed error information:
TypeNotFound: The requested type was not found in the standard libraryNotAStruct: The requested type exists but is not a structStdlibAnalysis: Failed to generate or parse rustdoc JSON (usually due to missing nightly toolchain or rust-src)Io: File system or process execution errors
Limitations
- Standard Library Only: Currently only supports types from std, alloc, and core crates
- Nightly Rust Required: Requires nightly Rust toolchain for rustdoc JSON generation
- rust-src Component Required: Requires rust-src component for standard library analysis
- Struct Types Only: Currently focuses on struct types (enum and trait support will be added in future versions)
- Performance: Initial cache initialization depends on rustdoc generation speed
Contributing
Contributions are welcome! Please feel free to submit an issue or a pull request.
License
MIT License - see LICENSE file for details.