Skip to main content

migration_helpers/
migration_helpers.rs

1//! Migration helpers example.
2//!
3//! This example shows the generic_error! and generic_warning! macros used during
4//! migration from old error systems. These macros create errors with code Q-0-99
5//! and include file/line tracking for easier debugging during the transition period.
6
7use quarto_error_reporting::{generic_error, generic_warning};
8
9fn main() {
10    println!("=== Example 1: Using generic_error! macro ===\n");
11
12    // The generic_error! macro creates a DiagnosticMessage with:
13    // - Code: Q-0-99 (generic migration error)
14    // - File and line number where the macro was invoked
15    // - The provided message
16    let error = generic_error!("Something went wrong during migration");
17
18    println!("{}", error.to_text(None));
19    println!();
20
21    // Check the error code
22    println!("Error code: {:?}", error.code);
23    println!();
24
25    println!("=== Example 2: Using generic_warning! macro ===\n");
26
27    let warning = generic_warning!("This feature is not yet fully migrated");
28
29    println!("{}", warning.to_text(None));
30    println!();
31
32    println!("=== Example 3: Migration pattern in practice ===\n");
33
34    // During migration, you might replace old error handling like this:
35    //
36    // OLD CODE:
37    //   eprintln!("Error: File not found: {}", path);
38    //   return Err(...);
39    //
40    // NEW CODE (migration phase):
41    //   let error = generic_error!(format!("File not found: {}", path));
42    //   eprintln!("{}", error.to_text(None));
43    //   return Err(...);
44    //
45    // FINAL CODE:
46    //   let error = DiagnosticMessageBuilder::error("File not found")
47    //       .with_code("Q-X-Y")  // Proper error code
48    //       .problem(format!("Could not open file: {}", path))
49    //       .add_hint("Check that the file exists and you have permission")
50    //       .build();
51
52    let path = "/nonexistent/file.qmd";
53    let migration_error = generic_error!(format!("File not found: {}", path));
54
55    println!("Migration-style error:");
56    println!("{}", migration_error.to_text(None));
57    println!();
58
59    println!("=== Example 4: JSON output shows file/line info ===\n");
60
61    let error_with_location = generic_error!("Error with source tracking");
62    let json = error_with_location.to_json();
63
64    println!("{}", serde_json::to_string_pretty(&json).unwrap());
65    println!();
66
67    println!("Note: The generic_error! and generic_warning! macros are intended");
68    println!("for migration purposes only. New code should use DiagnosticMessageBuilder");
69    println!("with proper error codes (Q-X-Y) instead of Q-0-99.");
70}