Crate annotate_snippets

Crate annotate_snippets 

Source
Expand description

Format diagnostic reports, including highlighting snippets of text

§Example

use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
    let source = r#"                annotations: vec![SourceAnnotation {
                label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
                    ,
                range: <22, 25>,"#;
    let report =
        &[
            Group::with_title(Level::ERROR.primary_title("expected type, found `22`")).element(
                Snippet::source(source)
                    .line_start(26)
                    .path("examples/footer.rs")
                    .annotation(AnnotationKind::Primary.span(193..195).label(
                        "expected struct `annotate_snippets::snippet::Slice`, found reference",
                    ))
                    .annotation(
                        AnnotationKind::Context
                            .span(34..50)
                            .label("while parsing this struct"),
                    ),
            ),
        ];

    let renderer = Renderer::styled();
    anstream::println!("{}", renderer.render(report));
}
error: expected type, found `22` --> examples/footer.rs:29:25 | 26 | annotations: vec![SourceAnnotation { | ---------------- while parsing this struct ... 29 | range: <22, 25>, | ^^ expected struct `annotate_snippets::snippet::Slice`, found reference

§Visual overview

Report

error: cannot construct `Box<_, _>` with struct literal syntax due to private fields ╭▸ $DIR/multi-suggestion.rs:17:13 17 let _ = Box {}; ━━━ note: private fields `0` and `1` that were not provided help: you might have meant to use an associated function to build this type ╭╴ 17 - let _ = Box {}; 17 + let _ = Box::new(_); ├╴ 17 - let _ = Box {}; 17 + let _ = Box::new_uninit(); ├╴ 17 - let _ = Box {}; 17 + let _ = Box::new_zeroed(); ├╴ 17 - let _ = Box {}; 17 + let _ = Box::new_in(_, _); and 12 other candidates help: consider using the `Default` trait ╭╴ 17 - let _ = Box {}; 17 + let _ = <Box as std::default::Default>::default(); ╰╴

§Primary group

Title

error: cannot construct `Box<_, _>` with struct literal syntax due to private fields

Annotation on a Snippet

   ╭▸ $DIR/multi-suggestion.rs:17:13
   │
17 │     let _ = Box {};
   │             ━━━
   │

Message

   ╰ note: private fields `0` and `1` that were not provided

§Secondary group: suggested fix

Title (proposed solution)

help: you might have meant to use an associated function to build this type

Patch Option 1 on a Snippet

   ╭╴
21 -     let _ = Box {};
21 +     let _ = Box::new(_);
   ├╴

Patch Option 2 on a Snippet

   ├╴
17 -     let _ = Box {};
17 +     let _ = Box::new_uninit();
   ├╴

etc for Options 3 and 4

Message

   ╰ and 12 other candidates

§Secondary group: alternative suggested fix

Title (proposed solution)

help: consider using the `Default` trait

Only Patch on a Snippet

   ╭╴
17 -     let _ = Box {};
17 +     let _ = <Box as std::default::Default>::default();
   ╰╴

§Cargo features

  • simd - Speeds up folding

  • testing-colors - Makes Renderer::styled colors OS independent, which allows for easier testing when testing colored output. It should be added as a feature in [dev-dependencies], which can be done with the following command:

cargo add annotate-snippets --dev --feature testing-colors

Modules§

level
Level constants for easy importing
renderer
The Renderer and its settings

Structs§

Annotation
Highlight and describe a span of text within a Snippet
Group
A Title with supporting context within a Report
Level
Severity level for Titles and Messages
Message
A text Element in a Group
OptionCow
Origin
A source location Element in a Group
Padding
A whitespace Element in a Group
Patch
Suggested edit to the Snippet
Renderer
The Renderer for a Report
Snippet
A source view Element in a Group
Title
A title that introduces a Group, describing the main point

Enums§

AnnotationKind
The type of Annotation being applied to a Snippet
Element
A section of content within a Group

Functions§

normalize_untrusted_str
Normalize the string to avoid any unicode control characters.

Type Aliases§

Report
A diagnostic message and any associated context to help users understand it