Skip to content

Commit 34475c7

Browse files
authored
Merge pull request #305 from epage/shortcut
feat: Add convenience methods for Group::with_title
2 parents 7b71709 + 9f44731 commit 34475c7

36 files changed

+1850
-1961
lines changed

benches/bench.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
1+
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet};
22

33
#[divan::bench]
44
fn simple() -> String {
@@ -24,8 +24,10 @@ fn simple() -> String {
2424
_ => continue,
2525
}
2626
}"#;
27-
let message = &[
28-
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308")).element(
27+
let message = &[Level::ERROR
28+
.primary_title("mismatched types")
29+
.id("E0308")
30+
.element(
2931
Snippet::source(source)
3032
.line_start(51)
3133
.path("src/format.rs")
@@ -39,8 +41,7 @@ fn simple() -> String {
3941
.span(26..724)
4042
.label("expected enum `std::option::Option`"),
4143
),
42-
),
43-
];
44+
)];
4445

4546
let renderer = Renderer::plain();
4647
let rendered = renderer.render(message);
@@ -69,17 +70,16 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
6970
(input, span)
7071
})
7172
.bench_values(|(input, span)| {
72-
let message =
73-
&[
74-
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308"))
75-
.element(
76-
Snippet::source(&input).path("src/format.rs").annotation(
77-
AnnotationKind::Context
78-
.span(span)
79-
.label("expected `Option<String>` because of return type"),
80-
),
81-
),
82-
];
73+
let message = &[Level::ERROR
74+
.primary_title("mismatched types")
75+
.id("E0308")
76+
.element(
77+
Snippet::source(&input).path("src/format.rs").annotation(
78+
AnnotationKind::Context
79+
.span(span)
80+
.label("expected `Option<String>` because of return type"),
81+
),
82+
)];
8383

8484
let renderer = Renderer::plain();
8585
let rendered = renderer.render(message);

examples/custom_error.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use annotate_snippets::renderer::DecorStyle;
2-
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
2+
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet};
33

44
fn main() {
55
let source = r#"//@ compile-flags: -Ztreat-err-as-bug
@@ -15,19 +15,17 @@ fn main() {
1515
pub static C: u32 = 0 - 1;
1616
//~^ ERROR could not evaluate static initializer
1717
"#;
18-
let report = &[Group::with_title(
19-
Level::ERROR
20-
.with_name(Some("error: internal compiler error"))
21-
.primary_title("could not evaluate static initializer")
22-
.id("E0080"),
23-
)
24-
.element(
25-
Snippet::source(source).path("$DIR/err.rs").annotation(
26-
AnnotationKind::Primary
27-
.span(386..391)
28-
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
29-
),
30-
)];
18+
let report = &[Level::ERROR
19+
.with_name(Some("error: internal compiler error"))
20+
.primary_title("could not evaluate static initializer")
21+
.id("E0080")
22+
.element(
23+
Snippet::source(source).path("$DIR/err.rs").annotation(
24+
AnnotationKind::Primary
25+
.span(386..391)
26+
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
27+
),
28+
)];
3129

3230
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
3331
anstream::println!("{}", renderer.render(report));

examples/custom_level.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use annotate_snippets::renderer::DecorStyle;
2-
use annotate_snippets::{AnnotationKind, Group, Level, Patch, Renderer, Snippet};
2+
use annotate_snippets::{AnnotationKind, Level, Patch, Renderer, Snippet};
33

44
fn main() {
55
let source = r#"// Regression test for issue #114529
@@ -29,13 +29,10 @@ fn main() {
2929
}
3030
}
3131
"#;
32-
let report =
33-
&[
34-
Group::with_title(
35-
Level::ERROR
36-
.primary_title("`break` with value from a `while` loop")
37-
.id("E0571"),
38-
)
32+
let report = &[
33+
Level::ERROR
34+
.primary_title("`break` with value from a `while` loop")
35+
.id("E0571")
3936
.element(
4037
Snippet::source(source)
4138
.line_start(1)
@@ -51,16 +48,16 @@ fn main() {
5148
.label("you can't `break` with a value in a `while` loop"),
5249
),
5350
),
54-
Group::with_title(Level::HELP.with_name(Some("suggestion")).secondary_title(
55-
"use `break` on its own without a value inside this `while` loop",
56-
))
51+
Level::HELP
52+
.with_name(Some("suggestion"))
53+
.secondary_title("use `break` on its own without a value inside this `while` loop")
5754
.element(
5855
Snippet::source(source)
5956
.line_start(1)
6057
.path("$DIR/issue-114529-illegal-break-with-value.rs")
6158
.patch(Patch::new(483..581, "break")),
6259
),
63-
];
60+
];
6461

6562
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
6663
anstream::println!("{}", renderer.render(report));

examples/expected_type.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Group, Level, Renderer, Snippet};
1+
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Renderer, Snippet};
22

33
fn main() {
44
let source = r#" annotations: vec![SourceAnnotation {
55
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
66
,
77
range: <22, 25>,"#;
88
let report =
9-
&[
10-
Group::with_title(Level::ERROR.primary_title("expected type, found `22`")).element(
9+
&[Level::ERROR
10+
.primary_title("expected type, found `22`")
11+
.element(
1112
Snippet::source(source)
1213
.line_start(26)
1314
.path("examples/footer.rs")
@@ -19,8 +20,7 @@ fn main() {
1920
.span(34..50)
2021
.label("while parsing this struct"),
2122
),
22-
),
23-
];
23+
)];
2424

2525
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
2626
anstream::println!("{}", renderer.render(report));

examples/footer.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Group, Level, Renderer, Snippet};
22

33
fn main() {
4-
let report =
5-
&[
6-
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308")).element(
4+
let report = &[
5+
Level::ERROR
6+
.primary_title("mismatched types")
7+
.id("E0308")
8+
.element(
79
Snippet::source(" slices: vec![\"A\",")
810
.line_start(13)
911
.path("src/multislice.rs")
1012
.annotation(AnnotationKind::Primary.span(21..24).label(
1113
"expected struct `annotate_snippets::snippet::Slice`, found reference",
1214
)),
1315
),
14-
Group::with_title(Level::NOTE.secondary_title(
15-
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
16-
)),
17-
];
16+
Group::with_title(Level::NOTE.secondary_title(
17+
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
18+
)),
19+
];
1820

1921
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
2022
anstream::println!("{}", renderer.render(report));

examples/format.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Group, Level, Renderer, Snippet};
1+
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Renderer, Snippet};
22

33
fn main() {
44
let source = r#") -> Option<String> {
@@ -23,8 +23,10 @@ fn main() {
2323
_ => continue,
2424
}
2525
}"#;
26-
let report = &[
27-
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308")).element(
26+
let report = &[Level::ERROR
27+
.primary_title("mismatched types")
28+
.id("E0308")
29+
.element(
2830
Snippet::source(source)
2931
.line_start(51)
3032
.path("src/format.rs")
@@ -39,8 +41,7 @@ fn main() {
3941
.span(26..724)
4042
.label("expected enum `std::option::Option`"),
4143
),
42-
),
43-
];
44+
)];
4445

4546
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
4647
anstream::println!("{}", renderer.render(report));

examples/highlight_message.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Group, Level, Renderer, Snippet};
1+
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Renderer, Snippet};
22
use anstyle::AnsiColor;
33
use anstyle::Effects;
44
use anstyle::Style;
@@ -34,7 +34,9 @@ fn main() {
3434
);
3535

3636
let report = &[
37-
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308"))
37+
Level::ERROR
38+
.primary_title("mismatched types")
39+
.id("E0308")
3840
.element(
3941
Snippet::source(source)
4042
.path("$DIR/highlighting.rs")
@@ -50,12 +52,14 @@ fn main() {
5052
),
5153
)
5254
.element(Level::NOTE.message(&message)),
53-
Group::with_title(Level::NOTE.secondary_title("function defined here")).element(
54-
Snippet::source(source)
55-
.path("$DIR/highlighting.rs")
56-
.annotation(AnnotationKind::Context.span(200..333).label(""))
57-
.annotation(AnnotationKind::Primary.span(194..199)),
58-
),
55+
Level::NOTE
56+
.secondary_title("function defined here")
57+
.element(
58+
Snippet::source(source)
59+
.path("$DIR/highlighting.rs")
60+
.annotation(AnnotationKind::Context.span(200..333).label(""))
61+
.annotation(AnnotationKind::Primary.span(194..199)),
62+
),
5963
];
6064

6165
let renderer = Renderer::styled()

examples/highlight_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Group, Level, Renderer, Snippet};
1+
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Renderer, Snippet};
22

33
fn main() {
44
let source = r#"//@ compile-flags: -Z teach
@@ -9,8 +9,8 @@ const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
99
//~| ERROR cannot call non-const method
1010
fn main() {}
1111
"#;
12-
let report = &[Group::with_title(Level::ERROR.primary_title("allocations are not allowed in constants")
13-
.id("E0010"))
12+
let report = &[Level::ERROR.primary_title("allocations are not allowed in constants")
13+
.id("E0010")
1414
.element(
1515
Snippet::source(source)
1616
.path("$DIR/E0010-teach.rs")

examples/id_hyperlink.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
use annotate_snippets::renderer::DecorStyle;
2-
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
2+
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet};
33

44
fn main() {
55
let source = r#"//@ compile-flags: -Zterminal-urls=yes
66
fn main() {
77
let () = 4; //~ ERROR
88
}
99
"#;
10-
let report = &[Group::with_title(
11-
Level::ERROR
12-
.primary_title("mismatched types")
13-
.id("E0308")
14-
.id_url("https://doc.rust-lang.org/error_codes/E0308.html"),
15-
)
16-
.element(
17-
Snippet::source(source)
18-
.line_start(1)
19-
.path("$DIR/terminal_urls.rs")
20-
.annotation(
21-
AnnotationKind::Primary
22-
.span(59..61)
23-
.label("expected integer, found `()`"),
24-
)
25-
.annotation(
26-
AnnotationKind::Context
27-
.span(64..65)
28-
.label("this expression has type `{integer}`"),
29-
),
30-
)];
10+
let report = &[Level::ERROR
11+
.primary_title("mismatched types")
12+
.id("E0308")
13+
.id_url("https://doc.rust-lang.org/error_codes/E0308.html")
14+
.element(
15+
Snippet::source(source)
16+
.line_start(1)
17+
.path("$DIR/terminal_urls.rs")
18+
.annotation(
19+
AnnotationKind::Primary
20+
.span(59..61)
21+
.label("expected integer, found `()`"),
22+
)
23+
.annotation(
24+
AnnotationKind::Context
25+
.span(64..65)
26+
.label("this expression has type `{integer}`"),
27+
),
28+
)];
3129

3230
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
3331
anstream::println!("{}", renderer.render(report));

0 commit comments

Comments
 (0)