Skip to content

Commit 7dfec9b

Browse files
committed
docs: Shift example code from README to lib.rs
We now pull from an example - We verify it builds (wasn't doing that with README) - We show the output for the actual code (couldn't do that from a rustdoc code fence)
1 parent 3822689 commit 7dfec9b

File tree

2 files changed

+8
-75
lines changed

2 files changed

+8
-75
lines changed

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/

src/lib.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@
77
//!
88
//! # Example
99
//!
10-
//! ```text
11-
//! error[E0308]: mismatched types
12-
//! --> src/format.rs:52:1
13-
//! |
14-
//! 51 | ) -> Option<String> {
15-
//! | -------------- expected `Option<String>` because of return type
16-
//! 52 | / for ann in annotations {
17-
//! 53 | | match (ann.range.0, ann.range.1) {
18-
//! 54 | | (None, None) => continue,
19-
//! 55 | | (Some(start), Some(end)) if start > end_index => continue,
20-
//! ... |
21-
//! 71 | | }
22-
//! 72 | | }
23-
//! | |_____^ expected enum `std::option::Option`, found ()
10+
//! ```rust
11+
#![doc = include_str!("../examples/expected_type.rs")]
2412
//! ```
2513
//!
14+
#![doc = include_str!("../examples/expected_type.svg")]
15+
//!
2616
//! The crate uses a three stage process with two conversions between states:
2717
//!
2818
//! ```text

0 commit comments

Comments
 (0)