Skip to content

Commit

Permalink
Merge pull request #91 from Muscraft/builder-pattern
Browse files Browse the repository at this point in the history
feat!: Move to builder pattern for snippet creation
  • Loading branch information
epage authored Mar 9, 2024
2 parents 86f2be9 + 5c566c0 commit 52283f3
Show file tree
Hide file tree
Showing 20 changed files with 442 additions and 493 deletions.
39 changes: 11 additions & 28 deletions benches/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ extern crate criterion;

use criterion::{black_box, Criterion};

use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use annotate_snippets::{Label, Renderer, Slice, Snippet};

fn create_snippet(renderer: Renderer) {
let snippet = Snippet {
slices: vec![Slice {
source: r#") -> Option<String> {
let source = r#") -> Option<String> {
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
Expand All @@ -30,30 +28,15 @@ fn create_snippet(renderer: Renderer) {
}
_ => continue,
}
}"#,
line_start: 51,
origin: Some("src/format.rs"),
fold: false,
annotations: vec![
SourceAnnotation {
label: "expected `Option<String>` because of return type",
annotation_type: AnnotationType::Warning,
range: 5..19,
},
SourceAnnotation {
label: "expected enum `std::option::Option`",
annotation_type: AnnotationType::Error,
range: 26..724,
},
],
}],
title: Some(Annotation {
label: Some("mismatched types"),
id: Some("E0308"),
annotation_type: AnnotationType::Error,
}),
footer: vec![],
};
}"#;
let snippet = Snippet::error("mismatched types").id("E0308").slice(
Slice::new(source, 51)
.origin("src/format.rs")
.annotation(
Label::warning("expected `Option<String>` because of return type").span(5..19),
)
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
);

let _result = renderer.render(snippet).to_string();
}
Expand Down
43 changes: 15 additions & 28 deletions examples/expected_type.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use annotate_snippets::{Label, Renderer, Slice, Snippet};

fn main() {
let snippet = Snippet {
title: Some(Annotation {
label: Some("expected type, found `22`"),
id: None,
annotation_type: AnnotationType::Error,
}),
footer: vec![],
slices: vec![Slice {
source: r#" annotations: vec![SourceAnnotation {
let source = r#" annotations: vec![SourceAnnotation {
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
,
range: <22, 25>,"#,
line_start: 26,
origin: Some("examples/footer.rs"),
fold: true,
annotations: vec![
SourceAnnotation {
label: "",
annotation_type: AnnotationType::Error,
range: 193..195,
},
SourceAnnotation {
label: "while parsing this struct",
annotation_type: AnnotationType::Info,
range: 34..50,
},
],
}],
};
range: <22, 25>,"#;
let snippet = Snippet::error("expected type, found `22`").slice(
Slice::new(source, 26)
.origin("examples/footer.rs")
.fold(true)
.annotation(
Label::error(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
)
.span(193..195),
)
.annotation(Label::info("while parsing this struct").span(34..50)),
);

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
Expand Down
42 changes: 16 additions & 26 deletions examples/footer.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use annotate_snippets::{Label, Renderer, Slice, Snippet};

fn main() {
let snippet = Snippet {
title: Some(Annotation {
label: Some("mismatched types"),
id: Some("E0308"),
annotation_type: AnnotationType::Error,
}),
footer: vec![Annotation {
label: Some(
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
),
id: None,
annotation_type: AnnotationType::Note,
}],
slices: vec![Slice {
source: " slices: vec![\"A\",",
line_start: 13,
origin: Some("src/multislice.rs"),
fold: false,
annotations: vec![SourceAnnotation {
label: "expected struct `annotate_snippets::snippet::Slice`, found reference",
range: 21..24,
annotation_type: AnnotationType::Error,
}],
}],
};
let snippet = Snippet::error("mismatched types")
.id("E0308")
.slice(
Slice::new(" slices: vec![\"A\",", 13)
.origin("src/multislice.rs")
.annotation(
Label::error(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
)
.span(21..24),
),
)
.footer(Label::note(
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
));

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
Expand Down
39 changes: 11 additions & 28 deletions examples/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
use annotate_snippets::{Label, Renderer, Slice, Snippet};

fn main() {
let snippet = Snippet {
slices: vec![Slice {
source: r#") -> Option<String> {
let source = r#") -> Option<String> {
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
Expand All @@ -24,30 +22,15 @@ fn main() {
}
_ => continue,
}
}"#,
line_start: 51,
origin: Some("src/format.rs"),
fold: false,
annotations: vec![
SourceAnnotation {
label: "expected `Option<String>` because of return type",
annotation_type: AnnotationType::Warning,
range: 5..19,
},
SourceAnnotation {
label: "expected enum `std::option::Option`",
annotation_type: AnnotationType::Error,
range: 26..724,
},
],
}],
title: Some(Annotation {
label: Some("mismatched types"),
id: Some("E0308"),
annotation_type: AnnotationType::Error,
}),
footer: vec![],
};
}"#;
let snippet = Snippet::error("mismatched types").id("E0308").slice(
Slice::new(source, 51)
.origin("src/format.rs")
.annotation(
Label::warning("expected `Option<String>` because of return type").span(5..19),
)
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
);

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
Expand Down
29 changes: 4 additions & 25 deletions examples/multislice.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet};
use annotate_snippets::{Renderer, Slice, Snippet};

fn main() {
let snippet = Snippet {
title: Some(Annotation {
label: Some("mismatched types"),
id: None,
annotation_type: AnnotationType::Error,
}),
footer: vec![],
slices: vec![
Slice {
source: "Foo",
line_start: 51,
origin: Some("src/format.rs"),
fold: false,
annotations: vec![],
},
Slice {
source: "Faa",
line_start: 129,
origin: Some("src/display.rs"),
fold: false,
annotations: vec![],
},
],
};
let snippet = Snippet::error("mismatched types")
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
.slice(Slice::new("Faa", 129).origin("src/display.rs"));

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
//! ```text
//! cargo add annotate-snippets --dev --feature testing-colors
//! ```
//!

pub mod renderer;
mod snippet;
Expand Down
Loading

0 comments on commit 52283f3

Please sign in to comment.