Skip to main content

renderer_selection/
renderer_selection.rs

1//! Render the same diagnostic with each available source-context renderer.
2//!
3//! Build with both renderers to compare side by side:
4//!
5//! ```sh
6//! cargo run --example renderer_selection --features annotate-snippets
7//! ```
8//!
9//! `ariadne` is on by default; `annotate-snippets` is opt-in. The
10//! structured data (title, code, problem, location) is identical — only
11//! the source-excerpt block differs.
12
13#[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
14use quarto_error_reporting::SourceRenderer;
15use quarto_error_reporting::{DiagnosticMessageBuilder, TextRenderOptions};
16use quarto_source_map::{SourceContext, SourceInfo};
17
18fn main() {
19    let mut ctx = SourceContext::new();
20    let source = "title: My Document\nformat:\n  html:\n    theme: nosuchtheme\n";
21    let file_id = ctx.add_file("_quarto.yml".to_string(), Some(source.to_string()));
22
23    // Point at `nosuchtheme` on line 4 (byte offsets into `source`).
24    let start = source.find("nosuchtheme").unwrap();
25    let location = SourceInfo::original(file_id, start, start + "nosuchtheme".len());
26
27    let diag = DiagnosticMessageBuilder::error("Unknown theme")
28        .with_code("Q-14-1")
29        .with_location(location)
30        .problem("`nosuchtheme` is not a known Quarto theme")
31        .add_hint("Did you mean `cosmo`, `darkly`, or `flatly`?")
32        .build();
33
34    // Disable hyperlinks so the output is path-stable in a demo.
35    let opts = TextRenderOptions {
36        enable_hyperlinks: false,
37    };
38
39    // `None` uses the default renderer for the enabled features.
40    println!("=== default renderer (None) ===\n");
41    println!("{}", diag.to_text_with_renderer(Some(&ctx), &opts, None));
42
43    #[cfg(feature = "ariadne")]
44    {
45        println!("=== SourceRenderer::Ariadne ===\n");
46        println!(
47            "{}",
48            diag.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne))
49        );
50    }
51
52    #[cfg(feature = "annotate-snippets")]
53    {
54        println!("=== SourceRenderer::AnnotateSnippets ===\n");
55        println!(
56            "{}",
57            diag.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::AnnotateSnippets),)
58        );
59    }
60}