]> git.proxmox.com Git - rustc.git/blame - vendor/annotate-snippets/README.md
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / annotate-snippets / README.md
CommitLineData
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
9The library helps visualize meta information annotating source code slices.
416331ca 10It takes a data structure called `Snippet` on the input and produces a `String`
dc9dc135
XL
11which may look like this:
12
13```text
14error[E0308]: mismatched types
15 --> src/format.rs:52:1
16 |
1751 | ) -> Option<String> {
18 | -------------- expected `Option<String>` because of return type
1952 | / for ann in annotations {
2053 | | match (ann.range.0, ann.range.1) {
2154 | | (None, None) => continue,
2255 | | (Some(start), Some(end)) if start > end_index => continue,
23... |
2471 | | }
2572 | | }
26 | |_____^ expected enum `std::option::Option`, found ()
27```
28
29[Documentation][]
30
31[Documentation]: https://docs.rs/annotate-snippets/
32
dc9dc135
XL
33Usage
34-----
35
36```rust
f035d41b
XL
37use annotate_snippets::{
38 display_list::DisplayList,
39 formatter::DisplayListFormatter,
40 snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
41};
dc9dc135
XL
42
43fn 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#"
54This is an example
55content of the slice
56which will be annotated
57with 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
84Local Development
85-----------------
86
87 cargo build
88 cargo test
89
90When submitting a PR please use [`cargo fmt`][] (nightly).
91
f035d41b 92[`cargo fmt`]: https://github.com/rust-lang/rustfmt