1 #![allow(clippy::unit_arg)]
2 #[macro_use]
3 extern crate criterion;
4 
5 use criterion::{black_box, Criterion};
6 
7 use annotate_snippets::{
8     display_list::{DisplayList, FormatOptions},
9     snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
10 };
11 
create_snippet()12 fn create_snippet() {
13     let snippet = Snippet {
14         slices: vec![Slice {
15             source: r#") -> Option<String> {
16     for ann in annotations {
17         match (ann.range.0, ann.range.1) {
18             (None, None) => continue,
19             (Some(start), Some(end)) if start > end_index => continue,
20             (Some(start), Some(end)) if start >= start_index => {
21                 let label = if let Some(ref label) = ann.label {
22                     format!(" {}", label)
23                 } else {
24                     String::from("")
25                 };
26 
27                 return Some(format!(
28                     "{}{}{}",
29                     " ".repeat(start - start_index),
30                     "^".repeat(end - start),
31                     label
32                 ));
33             }
34             _ => continue,
35         }
36     }"#,
37             line_start: 51,
38             origin: Some("src/format.rs"),
39             fold: false,
40             annotations: vec![
41                 SourceAnnotation {
42                     label: "expected `Option<String>` because of return type",
43                     annotation_type: AnnotationType::Warning,
44                     range: (5, 19),
45                 },
46                 SourceAnnotation {
47                     label: "expected enum `std::option::Option`",
48                     annotation_type: AnnotationType::Error,
49                     range: (26, 724),
50                 },
51             ],
52         }],
53         title: Some(Annotation {
54             label: Some("mismatched types"),
55             id: Some("E0308"),
56             annotation_type: AnnotationType::Error,
57         }),
58         footer: vec![],
59         opt: FormatOptions {
60             color: true,
61             ..Default::default()
62         },
63     };
64 
65     let dl = DisplayList::from(snippet);
66     let _result = dl.to_string();
67 }
68 
criterion_benchmark(c: &mut Criterion)69 pub fn criterion_benchmark(c: &mut Criterion) {
70     c.bench_function("format", |b| b.iter(|| black_box(create_snippet())));
71 }
72 
73 criterion_group!(benches, criterion_benchmark);
74 criterion_main!(benches);
75