Skip to content

fix: Force group levels to be explicitly given #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ fn simple() -> String {
_ => continue,
}
}"#;
let message = &[Group::new()
.element(Level::ERROR.title("mismatched types").id("E0308"))
.element(
let message = &[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
Snippet::source(source)
.line_start(51)
.path("src/format.rs")
Expand All @@ -40,7 +39,8 @@ fn simple() -> String {
.span(26..724)
.label("expected enum `std::option::Option`"),
),
)];
),
];

let renderer = Renderer::plain();
let rendered = renderer.render(message);
Expand Down Expand Up @@ -69,9 +69,8 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
(input, span)
})
.bench_values(|(input, span)| {
let message = &[Group::new()
.element(Level::ERROR.title("mismatched types").id("E0308"))
.element(
let message = &[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
Snippet::source(&input)
.fold(true)
.path("src/format.rs")
Expand All @@ -80,7 +79,8 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
.span(span)
.label("expected `Option<String>` because of return type"),
),
)];
),
];

let renderer = Renderer::plain();
let rendered = renderer.render(message);
Expand Down
33 changes: 16 additions & 17 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ fn main() {
pub static C: u32 = 0 - 1;
//~^ ERROR could not evaluate static initializer
"#;
let message = &[Group::new()
.element(
Level::ERROR
.text(Some("error: internal compiler error"))
.title("could not evaluate static initializer")
.id("E0080"),
)
.element(
Snippet::source(source)
.path("$DIR/err.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(386..391)
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
),
)];
let message = &[Group::with_title(
Level::ERROR
.text(Some("error: internal compiler error"))
.title("could not evaluate static initializer")
.id("E0080"),
)
.element(
Snippet::source(source)
.path("$DIR/err.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(386..391)
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
),
)];

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
Expand Down
68 changes: 33 additions & 35 deletions examples/custom_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,39 @@ fn main() {
}
"#;
let message = &[
Group::new()
.element(
Level::ERROR
.title("`break` with value from a `while` loop")
.id("E0571"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(483..581)
.label("can only break with a value inside `loop` or breakable block"),
)
.annotation(
AnnotationKind::Context
.span(462..472)
.label("you can't `break` with a value in a `while` loop"),
),
),
Group::new()
.element(
Level::HELP
.text(Some("suggestion"))
.title("use `break` on its own without a value inside this `while` loop"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.patch(Patch::new(483..581, "break")),
),
Group::with_title(
Level::ERROR
.title("`break` with value from a `while` loop")
.id("E0571"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(483..581)
.label("can only break with a value inside `loop` or breakable block"),
)
.annotation(
AnnotationKind::Context
.span(462..472)
.label("you can't `break` with a value in a `while` loop"),
),
),
Group::with_title(
Level::HELP
.text(Some("suggestion"))
.title("use `break` on its own without a value inside this `while` loop"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.patch(Patch::new(483..581, "break")),
),
];

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
Expand Down
3 changes: 1 addition & 2 deletions examples/elide_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def foobar(door, bar={}):
"""
"#;

let message = &[Group::new()
.primary_level(Level::NOTE)
let message = &[Group::with_level(Level::NOTE)
.element(
Snippet::source(source)
.fold(false)
Expand Down
8 changes: 4 additions & 4 deletions examples/expected_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn main() {
,
range: <22, 25>,"#;
let message =
&[Group::new()
.element(Level::ERROR.title("expected type, found `22`"))
.element(
&[
Group::with_title(Level::ERROR.title("expected type, found `22`")).element(
Snippet::source(source)
.line_start(26)
.path("examples/footer.rs")
Expand All @@ -21,7 +20,8 @@ fn main() {
.span(34..50)
.label("while parsing this struct"),
),
)];
),
];

let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
Expand Down
15 changes: 7 additions & 8 deletions examples/footer.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
let message = &[
Group::new()
.element(Level::ERROR.title("mismatched types").id("E0308"))
.element(
let message =
&[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
Snippet::source(" slices: vec![\"A\",")
.line_start(13)
.path("src/multislice.rs")
.annotation(AnnotationKind::Primary.span(21..24).label(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
)),
),
Group::new().element(Level::NOTE.title(
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
)),
];
Group::with_title(Level::NOTE.title(
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
)),
];

let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
Expand Down
8 changes: 4 additions & 4 deletions examples/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ fn main() {
_ => continue,
}
}"#;
let message = &[Group::new()
.element(Level::ERROR.title("mismatched types").id("E0308"))
.element(
let message = &[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308")).element(
Snippet::source(source)
.line_start(51)
.path("src/format.rs")
Expand All @@ -39,7 +38,8 @@ fn main() {
.span(26..724)
.label("expected enum `std::option::Option`"),
),
)];
),
];

let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(message));
Expand Down
2 changes: 1 addition & 1 deletion examples/highlight_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
//~| ERROR cannot call non-const method
fn main() {}
"#;
let message = &[Group::new().element(Level::ERROR.title("allocations are not allowed in constants")
let message = &[Group::with_title(Level::ERROR.title("allocations are not allowed in constants")
.id("E0010"))
.element(
Snippet::source(source)
Expand Down
19 changes: 8 additions & 11 deletions examples/highlight_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ fn main() {
);

let message = &[
Group::new()
.element(Level::ERROR.title("mismatched types").id("E0308"))
Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
.element(
Snippet::source(source)
.fold(true)
Expand All @@ -60,15 +59,13 @@ fn main() {
),
)
.element(Level::NOTE.pre_styled_title(&title)),
Group::new()
.element(Level::NOTE.title("function defined here"))
.element(
Snippet::source(source)
.fold(true)
.path("$DIR/highlighting.rs")
.annotation(AnnotationKind::Context.span(200..333).label(""))
.annotation(AnnotationKind::Primary.span(194..199)),
),
Group::with_title(Level::NOTE.title("function defined here")).element(
Snippet::source(source)
.fold(true)
.path("$DIR/highlighting.rs")
.annotation(AnnotationKind::Context.span(200..333).label(""))
.annotation(AnnotationKind::Primary.span(194..199)),
),
];

let renderer = Renderer::styled().anonymized_line_numbers(true);
Expand Down
45 changes: 22 additions & 23 deletions examples/id_hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ fn main() {
let () = 4; //~ ERROR
}
"#;
let message = &[Group::new()
.element(
Level::ERROR
.title("mismatched types")
.id("E0308")
.id_url("https://doc.rust-lang.org/error_codes/E0308.html"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/terminal_urls.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(59..61)
.label("expected integer, found `()`"),
)
.annotation(
AnnotationKind::Context
.span(64..65)
.label("this expression has type `{integer}`"),
),
)];
let message = &[Group::with_title(
Level::ERROR
.title("mismatched types")
.id("E0308")
.id_url("https://doc.rust-lang.org/error_codes/E0308.html"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/terminal_urls.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(59..61)
.label("expected integer, found `()`"),
)
.annotation(
AnnotationKind::Context
.span(64..65)
.label("this expression has type `{integer}`"),
),
)];

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
Expand Down
3 changes: 1 addition & 2 deletions examples/multislice.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use annotate_snippets::{Annotation, Group, Level, Renderer, Snippet};

fn main() {
let message = &[Group::new()
.element(Level::ERROR.title("mismatched types"))
let message = &[Group::with_title(Level::ERROR.title("mismatched types"))
.element(
Snippet::<Annotation<'_>>::source("Foo")
.line_start(51)
Expand Down
Loading