use ::boxen::{BoxenBuilder, BoxenError};
fn main() {
println!("=== Boxen Error Handling and Validation Demo ===\n");
println!("1. Valid configuration:");
match BoxenBuilder::new()
.width(50)
.height(8)
.padding(2)
.title("Success!")
.border_color("green")
.render("This configuration is valid and should work perfectly.")
{
Ok(result) => println!("{result}\n"),
Err(e) => println!("Error: {}\n", e.detailed_message()),
}
println!("2. Invalid configuration with detailed error messages:");
match BoxenBuilder::new()
.width(5) .padding(10) .title("a".repeat(250)) .border_color("invalid_color") .render("This will fail validation")
{
Ok(result) => println!("{result}\n"),
Err(e) => {
println!("Error occurred:");
println!("{}\n", e.detailed_message());
let recommendations = e.recommendations();
if !recommendations.is_empty() {
println!("Available recommendations:");
for (i, rec) in recommendations.iter().enumerate() {
println!("{}. {}: {}", i + 1, rec.issue, rec.suggestion);
if let Some(auto_fix) = &rec.auto_fix {
println!(" Auto-fix: {auto_fix}");
}
}
println!();
}
}
}
println!("3. Auto-recovery demonstration:");
match BoxenBuilder::new()
.width(8) .padding(3) .render_or_adjust("This should auto-adjust to work")
{
Ok(result) => {
println!("Auto-recovery succeeded:");
println!("{result}\n");
}
Err(e) => println!("Auto-recovery failed: {}\n", e.detailed_message()),
}
println!("4. Configuration validation without rendering:");
let builder = BoxenBuilder::new()
.width(30)
.height(10)
.padding(1)
.title("Validation Test");
match builder.validate() {
Ok(()) => println!("✓ Configuration is valid\n"),
Err(e) => println!("✗ Configuration is invalid: {}\n", e.detailed_message()),
}
println!("5. Detailed configuration analysis:");
let analysis_builder = BoxenBuilder::new()
.width(40)
.height(6)
.padding(2)
.title("Analysis Demo");
let analysis = analysis_builder.check_configuration("Sample text for analysis");
println!("{analysis}");
println!("6. Demonstrating different error types:");
println!("a) Input validation error:");
match BoxenBuilder::new().render("a".repeat(1_000_001)) {
Ok(_) => println!("Unexpectedly succeeded"),
Err(e) => match e {
BoxenError::InputValidationError { field, .. } => {
println!("Input validation failed for field: {field}");
}
BoxenError::RenderingError { .. } => {
println!("Rendering error (wrapping input validation)");
}
_ => println!("Other error type: {e}"),
},
}
println!("\nb) Dimension validation error:");
match BoxenBuilder::new().width(0).render("Test") {
Ok(_) => println!("Unexpectedly succeeded"),
Err(e) => match e {
BoxenError::InputValidationError { field, value, .. } => {
println!("Dimension validation failed: {field} = {value}");
}
BoxenError::RenderingError { .. } => {
println!("Rendering error (wrapping dimension validation)");
}
_ => println!("Other error type: {e}"),
},
}
println!("\nc) Color validation error:");
match BoxenBuilder::new()
.border_color("nonexistent_color")
.render("Test")
{
Ok(_) => println!("Unexpectedly succeeded"),
Err(e) => match e {
BoxenError::InvalidColor { color_value, .. } => {
println!("Color validation failed for: {color_value}");
}
BoxenError::InputValidationError { .. } => {
println!("Input validation error (wrapping color validation)");
}
BoxenError::RenderingError { .. } => {
println!("Rendering error (wrapping color validation)");
}
_ => println!("Other error type: {e}"),
},
}
println!("\n=== Demo Complete ===");
}