Skip to content

"modern" API, take cad.3 #14

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

Closed
wants to merge 4 commits into from
Closed
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
153 changes: 103 additions & 50 deletions benches/simple.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,126 @@
#[macro_use]
extern crate criterion;

use criterion::black_box;
use criterion::Criterion;
use criterion::{BatchSize, Criterion};

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

use annotate_snippets::renderers::ascii_default::get_renderer;
use annotate_snippets::renderers::Renderer;
use annotate_snippets::*;
use std::ops::Range;

const SOURCE: &'static str = r#") -> Option<String> {
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
(Some(start), Some(end)) if start > end_index => continue,
(Some(start), Some(end)) if start >= start_index => {
let label = if let Some(ref label) = ann.label {
format!(" {}", label)
} else {
String::from("")
};
for ann in annotations {
match (ann.range.0, ann.range.1) {
(None, None) => continue,
(Some(start), Some(end)) if start > end_index => continue,
(Some(start), Some(end)) if start >= start_index => {
let label = if let Some(ref label) = ann.label {
format!(" {}", label)
} else {
String::from("")
};

return Some(format!(
"{}{}{}",
" ".repeat(start - start_index),
"^".repeat(end - start),
label
));
return Some(format!(
"{}{}{}",
" ".repeat(start - start_index),
"^".repeat(end - start),
label
));
}
_ => continue,
}
_ => continue,
}"#;

fn source_snippet() -> Snippet<'static, WithLineNumber<&'static str>> {
Snippet {
title: Some(Title {
code: Some(&"E0308"),
message: Message {
text: &"mismatched types",
level: Level::Error,
},
}),
slices: &[Slice {
span: WithLineNumber {
line_num: 51,
data: SOURCE,
},
origin: Some(&"src/format.rs"),
annotations: &[
Annotation {
span: 5..19,
message: Some(Message {
text: &"expected `Option<String>` because of return type",
level: Level::Warning,
}),
},
Annotation {
span: 26..725,
message: Some(Message {
text: &"expected enum `std::option::Option`",
level: Level::Error,
}),
},
],
footer: &[],
}],
}
}"#;
}

fn create_snippet() {
let snippet = Snippet {
title: Some(Annotation {
id: Some("E0308"),
label: Some("mismatched types"),
annotation_type: AnnotationType::Error,
fn range_snippet() -> Snippet<'static, Range<usize>> {
Snippet {
title: Some(Title {
code: Some(&"E0308"),
message: Message {
text: &"mismatched types",
level: Level::Error,
},
}),
footer: &[],
slices: &[Slice {
source: SOURCE,
line_start: Some(51),
origin: Some("src/format.rs"),
span: 0..725,
origin: Some(&"src/format.rs"),
annotations: &[
SourceAnnotation {
label: "expected `Option<String>` because of return type",
annotation_type: AnnotationType::Warning,
range: 5..19,
Annotation {
span: 5..19,
message: Some(Message {
text: &"expected `Option<String>` because of return type",
level: Level::Warning,
}),
},
SourceAnnotation {
label: "expected enum `std::option::Option`",
annotation_type: AnnotationType::Error,
range: 23..725,
Annotation {
span: 26..725,
message: Some(Message {
text: &"expected enum `std::option::Option`",
level: Level::Error,
}),
},
],
footer: &[],
}],
};
let r = get_renderer();
let dl: DisplayList = (&snippet).into();
let mut result: Vec<u8> = Vec::new();
r.fmt(&mut result, &dl).unwrap();
}
}

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("format", |b| b.iter(|| black_box(create_snippet())));
c.bench_function("format [&str]", |b| {
b.iter_batched_ref(
|| Vec::<u8>::with_capacity(1100),
|out| {
let snippet = source_snippet();
let formatted = format(&snippet, &());
renderer::Ascii::new().render(&formatted, &(), out)
},
BatchSize::SmallInput,
)
});
c.bench_function("format [Range]", |b| {
b.iter_batched_ref(
|| Vec::<u8>::with_capacity(1100),
|out| {
let snippet = range_snippet();
let formatted = format(&snippet, &SOURCE);
renderer::Ascii::new().render(&formatted, &SOURCE, out)
},
BatchSize::SmallInput,
)
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
59 changes: 32 additions & 27 deletions examples/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use annotate_snippets::DisplayList;
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
use annotate_snippets::{Slice, Snippet};

use annotate_snippets::renderers::get_renderer;
use annotate_snippets::renderers::Renderer;
use annotate_snippets::*;
use std::io;

fn main() {
let source = r#") -> Option<String> {
Expand All @@ -30,34 +26,43 @@ fn main() {
}"#;

let snippet = Snippet {
title: Some(Annotation {
id: Some("E0308"),
label: Some("mismatched types"),
annotation_type: AnnotationType::Error,
title: Some(Title {
code: Some(&"E0308"),
message: Message {
text: &"mismatched types",
level: Level::Error,
},
}),
footer: &[],
slices: &[Slice {
source,
line_start: Some(51),
origin: Some("src/format.rs"),
span: WithLineNumber {
line_num: 51,
data: source,
},
origin: Some(&"src/format.rs"),
annotations: &[
SourceAnnotation {
label: "expected `Option<String>` because of return type",
annotation_type: AnnotationType::Warning,
range: 5..19,
Annotation {
span: 5..19,
message: Some(Message {
text: &"expected `Option<String>` because of return type",
level: Level::Warning,
}),
},
SourceAnnotation {
label: "expected enum `std::option::Option`",
annotation_type: AnnotationType::Error,
range: 23..725,
Annotation {
span: 26..725,
message: Some(Message {
text: &"expected enum `std::option::Option`",
level: Level::Error,
}),
},
],
footer: &[],
}],
};
let dl = DisplayList::from(&snippet);
let r = get_renderer();

let mut s: Vec<u8> = Vec::new();
r.fmt(&mut s, &dl).unwrap();
println!("{}", std::str::from_utf8(&s).unwrap());
let formatted = format(&snippet, &());
renderer::Ascii::new()
.ansi(true)
.box_drawing(true)
.render(&formatted, &(), &mut io::stdout().lock())
.unwrap();
}
25 changes: 0 additions & 25 deletions src/annotation.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/display_list/annotation.rs

This file was deleted.

51 changes: 0 additions & 51 deletions src/display_list/line.rs

This file was deleted.

Loading