]>
Commit | Line | Data |
---|---|---|
dc9dc135 XL |
1 | # annotate-snippets |
2 | ||
3 | `annotate-snippets` is a Rust library for annotation of programming code slices. | |
4 | ||
f035d41b XL |
5 | [![crates.io](https://meritbadge.herokuapp.com/annotate-snippets)](https://crates.io/crates/annotate-snippets) |
6 | [![Build Status](https://travis-ci.com/rust-lang/annotate-snippets-rs.svg?branch=master)](https://travis-ci.com/rust-lang/annotate-snippets-rs) | |
416331ca | 7 | [![Coverage Status](https://coveralls.io/repos/github/rust-lang/annotate-snippets-rs/badge.svg?branch=master)](https://coveralls.io/github/rust-lang/annotate-snippets-rs?branch=master) |
dc9dc135 XL |
8 | |
9 | The library helps visualize meta information annotating source code slices. | |
416331ca | 10 | It takes a data structure called `Snippet` on the input and produces a `String` |
dc9dc135 XL |
11 | which may look like this: |
12 | ||
13 | ```text | |
14 | error[E0308]: mismatched types | |
15 | --> src/format.rs:52:1 | |
16 | | | |
17 | 51 | ) -> Option<String> { | |
18 | | -------------- expected `Option<String>` because of return type | |
19 | 52 | / for ann in annotations { | |
20 | 53 | | match (ann.range.0, ann.range.1) { | |
21 | 54 | | (None, None) => continue, | |
22 | 55 | | (Some(start), Some(end)) if start > end_index => continue, | |
23 | ... | | |
24 | 71 | | } | |
25 | 72 | | } | |
26 | | |_____^ expected enum `std::option::Option`, found () | |
27 | ``` | |
28 | ||
29 | [Documentation][] | |
30 | ||
31 | [Documentation]: https://docs.rs/annotate-snippets/ | |
32 | ||
dc9dc135 XL |
33 | Usage |
34 | ----- | |
35 | ||
36 | ```rust | |
f035d41b XL |
37 | use annotate_snippets::{ |
38 | display_list::DisplayList, | |
39 | formatter::DisplayListFormatter, | |
40 | snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation}, | |
41 | }; | |
dc9dc135 XL |
42 | |
43 | fn main() { | |
44 | let snippet = Snippet { | |
45 | title: Some(Annotation { | |
46 | label: Some("expected type, found `22`".to_string()), | |
47 | id: None, | |
48 | annotation_type: AnnotationType::Error, | |
49 | }), | |
50 | footer: vec![], | |
51 | slices: vec![ | |
52 | Slice { | |
53 | source: r#" | |
54 | This is an example | |
55 | content of the slice | |
56 | which will be annotated | |
57 | with the list of annotations below. | |
58 | "#.to_string(), | |
59 | line_start: 26, | |
60 | origin: Some("examples/example.txt".to_string()), | |
61 | fold: false, | |
62 | annotations: vec![ | |
63 | SourceAnnotation { | |
64 | label: "Example error annotation".to_string(), | |
65 | annotation_type: AnnotationType::Error, | |
66 | range: (13, 18), | |
67 | }, | |
68 | SourceAnnotation { | |
69 | label: "and here's a warning".to_string(), | |
70 | annotation_type: AnnotationType::Warning, | |
71 | range: (34, 50), | |
72 | }, | |
73 | ], | |
74 | }, | |
75 | ], | |
76 | }; | |
77 | ||
78 | let dl = DisplayList::from(snippet); | |
f035d41b XL |
79 | let dlf = DisplayListFormatter::new(true, false); |
80 | println!("{}", dlf.format(&dl)); | |
dc9dc135 XL |
81 | } |
82 | ``` | |
83 | ||
84 | Local Development | |
85 | ----------------- | |
86 | ||
87 | cargo build | |
88 | cargo test | |
89 | ||
90 | When submitting a PR please use [`cargo fmt`][] (nightly). | |
91 | ||
f035d41b | 92 | [`cargo fmt`]: https://github.com/rust-lang/rustfmt |