Skip to content

Commit 40be8d4

Browse files
committed
take-three API
1 parent a3f9ba3 commit 40be8d4

File tree

21 files changed

+1193
-677
lines changed

21 files changed

+1193
-677
lines changed

benches/simple.rs

+100-48
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,122 @@ extern crate criterion;
44
use criterion::black_box;
55
use criterion::Criterion;
66

7-
use annotate_snippets::DisplayList;
8-
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
9-
use annotate_snippets::{Slice, Snippet};
10-
11-
use annotate_snippets::renderers::ascii_default::styles::plain::Style as PlainStyle;
12-
use annotate_snippets::renderers::ascii_default::Renderer as AsciiRenderer;
7+
use annotate_snippets::*;
8+
use std::ops::Range;
139

1410
const SOURCE: &'static str = r#") -> Option<String> {
15-
for ann in annotations {
16-
match (ann.range.0, ann.range.1) {
17-
(None, None) => continue,
18-
(Some(start), Some(end)) if start > end_index => continue,
19-
(Some(start), Some(end)) if start >= start_index => {
20-
let label = if let Some(ref label) = ann.label {
21-
format!(" {}", label)
22-
} else {
23-
String::from("")
24-
};
11+
for ann in annotations {
12+
match (ann.range.0, ann.range.1) {
13+
(None, None) => continue,
14+
(Some(start), Some(end)) if start > end_index => continue,
15+
(Some(start), Some(end)) if start >= start_index => {
16+
let label = if let Some(ref label) = ann.label {
17+
format!(" {}", label)
18+
} else {
19+
String::from("")
20+
};
2521
26-
return Some(format!(
27-
"{}{}{}",
28-
" ".repeat(start - start_index),
29-
"^".repeat(end - start),
30-
label
31-
));
22+
return Some(format!(
23+
"{}{}{}",
24+
" ".repeat(start - start_index),
25+
"^".repeat(end - start),
26+
label
27+
));
28+
}
29+
_ => continue,
3230
}
33-
_ => continue,
31+
}"#;
32+
33+
fn source_snippet() -> Snippet<'static, WithLineNumber<&'static str>> {
34+
Snippet {
35+
title: Some(Title {
36+
code: Some(&"E0308"),
37+
message: Message {
38+
text: &"mismatched types",
39+
level: Level::Error,
40+
},
41+
}),
42+
slices: &[Slice {
43+
span: WithLineNumber {
44+
line_num: 51,
45+
data: SOURCE,
46+
},
47+
origin: Some(&"src/format.rs"),
48+
annotations: &[
49+
Annotation {
50+
span: 5..19,
51+
message: Some(Message {
52+
text: &"expected `Option<String>` because of return type",
53+
level: Level::Warning,
54+
}),
55+
},
56+
Annotation {
57+
span: 26..725,
58+
message: Some(Message {
59+
text: &"expected enum `std::option::Option`",
60+
level: Level::Error,
61+
}),
62+
},
63+
],
64+
footer: &[],
65+
}],
3466
}
35-
}"#;
67+
}
3668

37-
fn create_snippet() {
38-
let snippet = Snippet {
39-
title: Some(Annotation {
40-
id: Some("E0308"),
41-
label: Some("mismatched types"),
42-
annotation_type: AnnotationType::Error,
69+
fn range_snippet() -> Snippet<'static, Range<usize>> {
70+
Snippet {
71+
title: Some(Title {
72+
code: Some(&"E0308"),
73+
message: Message {
74+
text: &"mismatched types",
75+
level: Level::Error,
76+
},
4377
}),
44-
footer: &[],
4578
slices: &[Slice {
46-
source: SOURCE,
47-
line_start: Some(51),
48-
origin: Some("src/format.rs"),
79+
span: 0..725,
80+
origin: Some(&"src/format.rs"),
4981
annotations: &[
50-
SourceAnnotation {
51-
label: "expected `Option<String>` because of return type",
52-
annotation_type: AnnotationType::Warning,
53-
range: 5..19,
82+
Annotation {
83+
span: 5..19,
84+
message: Some(Message {
85+
text: &"expected `Option<String>` because of return type",
86+
level: Level::Warning,
87+
}),
5488
},
55-
SourceAnnotation {
56-
label: "expected enum `std::option::Option`",
57-
annotation_type: AnnotationType::Error,
58-
range: 23..725,
89+
Annotation {
90+
span: 26..725,
91+
message: Some(Message {
92+
text: &"expected enum `std::option::Option`",
93+
level: Level::Error,
94+
}),
5995
},
6096
],
97+
footer: &[],
6198
}],
62-
};
63-
let r = AsciiRenderer::<PlainStyle>::new();
64-
let dl: DisplayList = (&snippet).into();
65-
let mut result: Vec<u8> = Vec::new();
66-
r.fmt(&mut result, &dl).unwrap();
99+
}
67100
}
68101

69102
pub fn criterion_benchmark(c: &mut Criterion) {
70-
c.bench_function("format", |b| b.iter(|| black_box(create_snippet())));
103+
c.bench_function("format [&str]", |b| {
104+
b.iter(|| {
105+
black_box({
106+
let snippet = source_snippet();
107+
let formatted = format(&snippet, &());
108+
let mut out: Vec<u8> = Vec::new();
109+
renderer::Ascii::plain().render(&formatted, &(), &mut out)
110+
})
111+
})
112+
});
113+
c.bench_function("format [Range]", |b| {
114+
b.iter(|| {
115+
black_box({
116+
let snippet = range_snippet();
117+
let formatted = format(&snippet, &SOURCE);
118+
let mut out: Vec<u8> = Vec::new();
119+
renderer::Ascii::plain().render(&formatted, &SOURCE, &mut out)
120+
})
121+
})
122+
});
71123
}
72124

73125
criterion_group!(benches, criterion_benchmark);

examples/format.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use annotate_snippets::DisplayList;
2-
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
3-
use annotate_snippets::{Slice, Snippet};
4-
5-
use annotate_snippets::renderers::ascii_default::get_renderer;
6-
use annotate_snippets::renderers::Renderer;
1+
use annotate_snippets::*;
2+
use std::io;
73

84
fn main() {
95
let source = r#") -> Option<String> {
@@ -30,34 +26,41 @@ fn main() {
3026
}"#;
3127

3228
let snippet = Snippet {
33-
title: Some(Annotation {
34-
id: Some("E0308"),
35-
label: Some("mismatched types"),
36-
annotation_type: AnnotationType::Error,
29+
title: Some(Title {
30+
code: Some(&"E0308"),
31+
message: Message {
32+
text: &"mismatched types",
33+
level: Level::Error,
34+
},
3735
}),
38-
footer: &[],
3936
slices: &[Slice {
40-
source,
41-
line_start: Some(51),
42-
origin: Some("src/format.rs"),
37+
span: WithLineNumber {
38+
line_num: 51,
39+
data: source,
40+
},
41+
origin: Some(&"src/format.rs"),
4342
annotations: &[
44-
SourceAnnotation {
45-
label: "expected `Option<String>` because of return type",
46-
annotation_type: AnnotationType::Warning,
47-
range: 5..19,
43+
Annotation {
44+
span: 5..19,
45+
message: Some(Message {
46+
text: &"expected `Option<String>` because of return type",
47+
level: Level::Warning,
48+
}),
4849
},
49-
SourceAnnotation {
50-
label: "expected enum `std::option::Option`",
51-
annotation_type: AnnotationType::Error,
52-
range: 23..725,
50+
Annotation {
51+
span: 26..725,
52+
message: Some(Message {
53+
text: &"expected enum `std::option::Option`",
54+
level: Level::Error,
55+
}),
5356
},
5457
],
58+
footer: &[],
5559
}],
5660
};
57-
let dl = DisplayList::from(&snippet);
58-
let r = get_renderer();
5961

60-
let mut s: Vec<u8> = Vec::new();
61-
r.fmt(&mut s, &dl).unwrap();
62-
println!("{}", std::str::from_utf8(&s).unwrap());
62+
let formatted = format(&snippet, &());
63+
renderer::Ascii::ansi()
64+
.render(&formatted, &(), &mut io::stdout().lock())
65+
.unwrap();
6366
}

src/annotation.rs

-25
This file was deleted.

src/display_list/annotation.rs

-8
This file was deleted.

src/display_list/line.rs

-50
This file was deleted.

0 commit comments

Comments
 (0)