Skip to main content

builder_api/
builder_api.rs

1//! Builder API example.
2//!
3//! This example demonstrates the builder API which encodes tidyverse guidelines
4//! for error messages: title, problem, details, and hints.
5
6use quarto_error_reporting::DiagnosticMessageBuilder;
7
8fn main() {
9    println!("=== Example 1: Simple builder usage ===\n");
10
11    let error1 = DiagnosticMessageBuilder::error("Invalid input")
12        .problem("Value must be numeric")
13        .add_detail("Found text in column 3")
14        .add_hint("Check the input file format")
15        .build();
16
17    println!("{}", error1.to_text(None));
18
19    println!("\n=== Example 2: Tidyverse four-part structure ===\n");
20
21    let error2 = DiagnosticMessageBuilder::error("Incompatible types")
22        .problem("Cannot combine date and datetime types")
23        .add_detail("`x` has type `date`")
24        .add_detail("`y` has type `datetime`")
25        .add_info("Both values come from the same data source")
26        .add_hint("Convert both to the same type first?")
27        .build();
28
29    println!("{}", error2.to_text(None));
30
31    println!("\n=== Example 3: Multiple details and hints ===\n");
32
33    let error3 = DiagnosticMessageBuilder::error("Schema validation failed")
34        .problem("Configuration does not match expected schema")
35        .add_detail("Property `title` has type `number`")
36        .add_detail("Expected type is `string`")
37        .add_detail("Property `author` is missing")
38        .add_info("Schema is defined in `_quarto.yml`")
39        .add_hint("Did you forget quotes around the title?")
40        .add_hint("Add an `author` field to the configuration")
41        .build();
42
43    println!("{}", error3.to_text(None));
44
45    println!("\n=== Example 4: Builder validation ===\n");
46
47    // This will trigger validation warnings
48    let (msg, warnings) = DiagnosticMessageBuilder::error("Validation test")
49        .add_detail("Detail 1")
50        .add_detail("Detail 2")
51        .add_detail("Detail 3")
52        .add_detail("Detail 4")
53        .add_detail("Detail 5")
54        .add_detail("Detail 6") // Too many!
55        .build_with_validation();
56
57    println!("{}", msg.to_text(None));
58
59    if !warnings.is_empty() {
60        println!("\nValidation warnings:");
61        for warning in warnings {
62            println!("  ⚠ {}", warning);
63        }
64    }
65}