Skip to content

Commit 52283f3

Browse files
authored
Merge pull request #91 from Muscraft/builder-pattern
feat!: Move to builder pattern for snippet creation
2 parents 86f2be9 + 5c566c0 commit 52283f3

20 files changed

+442
-493
lines changed

benches/simple.rs

+11-28
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ extern crate criterion;
44

55
use criterion::{black_box, Criterion};
66

7-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
7+
use annotate_snippets::{Label, Renderer, Slice, Snippet};
88

99
fn create_snippet(renderer: Renderer) {
10-
let snippet = Snippet {
11-
slices: vec![Slice {
12-
source: r#") -> Option<String> {
10+
let source = r#") -> Option<String> {
1311
for ann in annotations {
1412
match (ann.range.0, ann.range.1) {
1513
(None, None) => continue,
@@ -30,30 +28,15 @@ fn create_snippet(renderer: Renderer) {
3028
}
3129
_ => continue,
3230
}
33-
}"#,
34-
line_start: 51,
35-
origin: Some("src/format.rs"),
36-
fold: false,
37-
annotations: vec![
38-
SourceAnnotation {
39-
label: "expected `Option<String>` because of return type",
40-
annotation_type: AnnotationType::Warning,
41-
range: 5..19,
42-
},
43-
SourceAnnotation {
44-
label: "expected enum `std::option::Option`",
45-
annotation_type: AnnotationType::Error,
46-
range: 26..724,
47-
},
48-
],
49-
}],
50-
title: Some(Annotation {
51-
label: Some("mismatched types"),
52-
id: Some("E0308"),
53-
annotation_type: AnnotationType::Error,
54-
}),
55-
footer: vec![],
56-
};
31+
}"#;
32+
let snippet = Snippet::error("mismatched types").id("E0308").slice(
33+
Slice::new(source, 51)
34+
.origin("src/format.rs")
35+
.annotation(
36+
Label::warning("expected `Option<String>` because of return type").span(5..19),
37+
)
38+
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
39+
);
5740

5841
let _result = renderer.render(snippet).to_string();
5942
}

examples/expected_type.rs

+15-28
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
1-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{Label, Renderer, Slice, Snippet};
22

33
fn main() {
4-
let snippet = Snippet {
5-
title: Some(Annotation {
6-
label: Some("expected type, found `22`"),
7-
id: None,
8-
annotation_type: AnnotationType::Error,
9-
}),
10-
footer: vec![],
11-
slices: vec![Slice {
12-
source: r#" annotations: vec![SourceAnnotation {
4+
let source = r#" annotations: vec![SourceAnnotation {
135
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
146
,
15-
range: <22, 25>,"#,
16-
line_start: 26,
17-
origin: Some("examples/footer.rs"),
18-
fold: true,
19-
annotations: vec![
20-
SourceAnnotation {
21-
label: "",
22-
annotation_type: AnnotationType::Error,
23-
range: 193..195,
24-
},
25-
SourceAnnotation {
26-
label: "while parsing this struct",
27-
annotation_type: AnnotationType::Info,
28-
range: 34..50,
29-
},
30-
],
31-
}],
32-
};
7+
range: <22, 25>,"#;
8+
let snippet = Snippet::error("expected type, found `22`").slice(
9+
Slice::new(source, 26)
10+
.origin("examples/footer.rs")
11+
.fold(true)
12+
.annotation(
13+
Label::error(
14+
"expected struct `annotate_snippets::snippet::Slice`, found reference",
15+
)
16+
.span(193..195),
17+
)
18+
.annotation(Label::info("while parsing this struct").span(34..50)),
19+
);
3320

3421
let renderer = Renderer::plain();
3522
println!("{}", renderer.render(snippet));

examples/footer.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{Label, Renderer, Slice, Snippet};
22

33
fn main() {
4-
let snippet = Snippet {
5-
title: Some(Annotation {
6-
label: Some("mismatched types"),
7-
id: Some("E0308"),
8-
annotation_type: AnnotationType::Error,
9-
}),
10-
footer: vec![Annotation {
11-
label: Some(
12-
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
13-
),
14-
id: None,
15-
annotation_type: AnnotationType::Note,
16-
}],
17-
slices: vec![Slice {
18-
source: " slices: vec![\"A\",",
19-
line_start: 13,
20-
origin: Some("src/multislice.rs"),
21-
fold: false,
22-
annotations: vec![SourceAnnotation {
23-
label: "expected struct `annotate_snippets::snippet::Slice`, found reference",
24-
range: 21..24,
25-
annotation_type: AnnotationType::Error,
26-
}],
27-
}],
28-
};
4+
let snippet = Snippet::error("mismatched types")
5+
.id("E0308")
6+
.slice(
7+
Slice::new(" slices: vec![\"A\",", 13)
8+
.origin("src/multislice.rs")
9+
.annotation(
10+
Label::error(
11+
"expected struct `annotate_snippets::snippet::Slice`, found reference",
12+
)
13+
.span(21..24),
14+
),
15+
)
16+
.footer(Label::note(
17+
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
18+
));
2919

3020
let renderer = Renderer::plain();
3121
println!("{}", renderer.render(snippet));

examples/format.rs

+11-28
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{Label, Renderer, Slice, Snippet};
22

33
fn main() {
4-
let snippet = Snippet {
5-
slices: vec![Slice {
6-
source: r#") -> Option<String> {
4+
let source = r#") -> Option<String> {
75
for ann in annotations {
86
match (ann.range.0, ann.range.1) {
97
(None, None) => continue,
@@ -24,30 +22,15 @@ fn main() {
2422
}
2523
_ => continue,
2624
}
27-
}"#,
28-
line_start: 51,
29-
origin: Some("src/format.rs"),
30-
fold: false,
31-
annotations: vec![
32-
SourceAnnotation {
33-
label: "expected `Option<String>` because of return type",
34-
annotation_type: AnnotationType::Warning,
35-
range: 5..19,
36-
},
37-
SourceAnnotation {
38-
label: "expected enum `std::option::Option`",
39-
annotation_type: AnnotationType::Error,
40-
range: 26..724,
41-
},
42-
],
43-
}],
44-
title: Some(Annotation {
45-
label: Some("mismatched types"),
46-
id: Some("E0308"),
47-
annotation_type: AnnotationType::Error,
48-
}),
49-
footer: vec![],
50-
};
25+
}"#;
26+
let snippet = Snippet::error("mismatched types").id("E0308").slice(
27+
Slice::new(source, 51)
28+
.origin("src/format.rs")
29+
.annotation(
30+
Label::warning("expected `Option<String>` because of return type").span(5..19),
31+
)
32+
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
33+
);
5134

5235
let renderer = Renderer::plain();
5336
println!("{}", renderer.render(snippet));

examples/multislice.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
1-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet};
1+
use annotate_snippets::{Renderer, Slice, Snippet};
22

33
fn main() {
4-
let snippet = Snippet {
5-
title: Some(Annotation {
6-
label: Some("mismatched types"),
7-
id: None,
8-
annotation_type: AnnotationType::Error,
9-
}),
10-
footer: vec![],
11-
slices: vec![
12-
Slice {
13-
source: "Foo",
14-
line_start: 51,
15-
origin: Some("src/format.rs"),
16-
fold: false,
17-
annotations: vec![],
18-
},
19-
Slice {
20-
source: "Faa",
21-
line_start: 129,
22-
origin: Some("src/display.rs"),
23-
fold: false,
24-
annotations: vec![],
25-
},
26-
],
27-
};
4+
let snippet = Snippet::error("mismatched types")
5+
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
6+
.slice(Slice::new("Faa", 129).origin("src/display.rs"));
287

298
let renderer = Renderer::plain();
309
println!("{}", renderer.render(snippet));

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
//! ```text
4949
//! cargo add annotate-snippets --dev --feature testing-colors
5050
//! ```
51-
//!
5251
5352
pub mod renderer;
5453
mod snippet;

0 commit comments

Comments
 (0)