Skip to content

Commit 666b2b9

Browse files
authored
Merge pull request #93 from epage/readme
docs: Shift example code from README to lib.rs
2 parents 4b5820a + 77050ea commit 666b2b9

13 files changed

+308
-84
lines changed

Cargo.lock

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ itertools = "0.12.1"
2929
unicode-width = "0.1.11"
3030

3131
[dev-dependencies]
32+
anstream = "0.6.13"
3233
criterion = "0.5.1"
3334
difference = "2.0.0"
3435
glob = "0.3.1"
3536
serde = { version = "1.0.197", features = ["derive"] }
36-
snapbox = { version = "0.5.8", features = ["diff", "harness", "path", "term-svg"] }
37+
snapbox = { version = "0.5.8", features = ["diff", "harness", "path", "term-svg", "cmd", "examples"] }
3738
toml = "0.5.11"
3839

3940
[[bench]]

README.md

+4-61
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,14 @@
33
`annotate-snippets` is a Rust library for annotation of programming code slices.
44

55
[![crates.io](https://img.shields.io/crates/v/annotate-snippets.svg)](https://crates.io/crates/annotate-snippets)
6+
[![documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]
67
![build status](https://github.com/rust-lang/annotate-snippets-rs/actions/workflows/ci.yml/badge.svg)
78

89
The library helps visualize meta information annotating source code slices.
910
It takes a data structure called `Snippet` on the input and produces a `String`
1011
which may look like this:
1112

12-
```text
13-
error[E0308]: mismatched types
14-
--> src/format.rs:52:1
15-
|
16-
51 | ) -> Option<String> {
17-
| -------------- expected `Option<String>` because of return type
18-
52 | / for ann in annotations {
19-
53 | | match (ann.range.0, ann.range.1) {
20-
54 | | (None, None) => continue,
21-
55 | | (Some(start), Some(end)) if start > end_index => continue,
22-
... |
23-
71 | | }
24-
72 | | }
25-
| |_____^ expected enum `std::option::Option`, found ()
26-
```
27-
28-
[Documentation][]
29-
30-
[Documentation]: https://docs.rs/annotate-snippets/
31-
32-
Usage
33-
-----
34-
35-
```rust
36-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
37-
38-
fn main() {
39-
let snippet = Snippet {
40-
title: Some(Annotation {
41-
label: Some("expected type, found `22`"),
42-
id: None,
43-
annotation_type: AnnotationType::Error,
44-
}),
45-
footer: vec![],
46-
slices: vec![Slice {
47-
source: r#" annotations: vec![SourceAnnotation {
48-
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
49-
,
50-
range: <22, 25>,"#,
51-
line_start: 26,
52-
origin: Some("examples/footer.rs"),
53-
fold: true,
54-
annotations: vec![
55-
SourceAnnotation {
56-
label: "",
57-
annotation_type: AnnotationType::Error,
58-
range: (193, 195),
59-
},
60-
SourceAnnotation {
61-
label: "while parsing this struct",
62-
annotation_type: AnnotationType::Info,
63-
range: (34, 50),
64-
},
65-
],
66-
}],
67-
};
68-
69-
let renderer = Renderer::plain();
70-
println!("{}", renderer.render(snippet));
71-
}
72-
```
13+
![Screenshot](./examples/expected_type.svg)
7314

7415
Local Development
7516
-----------------
@@ -80,3 +21,5 @@ Local Development
8021
When submitting a PR please use [`cargo fmt`][] (nightly).
8122

8223
[`cargo fmt`]: https://github.com/rust-lang/rustfmt
24+
25+
[Documentation]: https://docs.rs/annotate-snippets/

examples/expected_type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fn main() {
1818
.annotation(Label::info("while parsing this struct").span(34..50)),
1919
);
2020

21-
let renderer = Renderer::plain();
22-
println!("{}", renderer.render(snippet));
21+
let renderer = Renderer::styled();
22+
anstream::println!("{}", renderer.render(snippet));
2323
}

examples/expected_type.svg

+44
Loading

examples/footer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ fn main() {
1717
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
1818
));
1919

20-
let renderer = Renderer::plain();
21-
println!("{}", renderer.render(snippet));
20+
let renderer = Renderer::styled();
21+
anstream::println!("{}", renderer.render(snippet));
2222
}

examples/footer.svg

+43
Loading

examples/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ fn main() {
3232
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
3333
);
3434

35-
let renderer = Renderer::plain();
36-
println!("{}", renderer.render(snippet));
35+
let renderer = Renderer::styled();
36+
anstream::println!("{}", renderer.render(snippet));
3737
}

examples/format.svg

+85
Loading

examples/multislice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ fn main() {
55
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
66
.slice(Slice::new("Faa", 129).origin("src/display.rs"));
77

8-
let renderer = Renderer::plain();
9-
println!("{}", renderer.render(snippet));
8+
let renderer = Renderer::styled();
9+
anstream::println!("{}", renderer.render(snippet));
1010
}

0 commit comments

Comments
 (0)