use quarry::{QuarryError, mine_struct_info};
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
println!("π Quarry - Basic Usage Example");
println!("=====================================\n");
println!("π Example 1: Analyzing alloc::string::String");
analyze_struct("alloc::string::String")?;
println!("\n{}\n", "β".repeat(50));
println!("π Example 2: Analyzing alloc::vec::Vec");
analyze_struct("alloc::vec::Vec")?;
println!("\n{}\n", "β".repeat(50));
println!("πΊοΈ Example 3: Analyzing std::collections::HashMap");
analyze_struct("std::collections::HashMap")?;
println!("\n{}\n", "β".repeat(50));
println!("π― Example 4: Analyzing std::mem::ManuallyDrop");
analyze_struct("std::mem::ManuallyDrop")?;
println!("\n{}\n", "β".repeat(50));
println!("β Example 5: Error Handling");
demonstrate_error_handling();
println!("\nβ
Basic usage examples completed!");
Ok(())
}
fn analyze_struct(struct_name: &str) -> Result<(), QuarryError> {
println!("Analyzing: {}", struct_name);
match mine_struct_info(struct_name) {
Ok(info) => {
println!(" β Found struct successfully!");
println!(" π Full name: {}", info.name);
println!(" π·οΈ Simple name: {}", info.simple_name);
println!(" π Module path: {}", info.module_path);
println!(" π§ Struct type:");
if info.is_unit_struct {
println!(" β’ Unit struct (no fields)");
} else if info.is_tuple_struct {
println!(" β’ Tuple struct (positional fields)");
} else {
println!(" β’ Named struct (named fields)");
}
println!(" π Fields: {} total", info.fields.len());
if !info.fields.is_empty() {
println!(" Field details:");
for (i, field) in info.fields.iter().enumerate() {
let visibility = if field.is_public {
"π public"
} else {
"π private"
};
println!(
" {}. {} : {} ({})",
i + 1,
field.name,
field.type_name,
visibility
);
}
} else {
println!(
" No fields accessible (may be opaque or have complex internal structure)"
);
}
}
Err(e) => {
println!(" β Error: {}", e);
return Err(e);
}
}
Ok(())
}
fn demonstrate_error_handling() {
println!("Trying to analyze invalid struct names...\n");
let invalid_names = vec![
"String", "Vec", "core::option::Option", "NonExistent", "my::custom::Type", ];
for name in invalid_names {
println!(" Trying: {}", name);
match mine_struct_info(name) {
Ok(_) => println!(" β Unexpectedly succeeded"),
Err(e) => match e {
QuarryError::TypeNotFound(_) => {
println!(" β Type not found (expected)");
if !name.contains("::") {
println!(" π‘ Tip: Use full module path like 'alloc::string::String'");
} else if name.contains("Option") {
println!(" π‘ Note: Option is an enum, not a struct. Enum support is planned for future releases.");
}
}
other => println!(" β Other error: {}", other),
},
}
println!();
}
}