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));
}
§Visual overview
§Primary group
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 {};
│ ━━━
│
╰ 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
╭╴
21 - let _ = Box {};
21 + let _ = Box::new(_);
├╴
├╴
17 - let _ = Box {};
17 + let _ = Box::new_uninit();
├╴
etc for Options 3 and 4
╰ and 12 other candidates
§Secondary group: alternative suggested fix
Title
(proposed solution)
help: consider using the `Default` trait
╭╴
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§
Structs§
- Annotation
- Highlight and describe a span of text within a
Snippet
- Group
- A
Title
with supporting context within aReport
- Level
- Severity level for
Title
s andMessage
s - Message
- A text
Element
in aGroup
- Option
Cow - Origin
- A source location
Element
in aGroup
- Padding
- A whitespace
Element
in aGroup
- Patch
- Suggested edit to the
Snippet
- Renderer
- The Renderer for a
Report
- Snippet
- A source view
Element
in aGroup
- Title
- A title that introduces a
Group
, describing the main point
Enums§
- Annotation
Kind - The type of
Annotation
being applied to aSnippet
- 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