Skip to content

docs: Shift example code from README to lib.rs #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ itertools = "0.12.1"
unicode-width = "0.1.11"

[dev-dependencies]
anstream = "0.6.13"
criterion = "0.5.1"
difference = "2.0.0"
glob = "0.3.1"
serde = { version = "1.0.197", features = ["derive"] }
snapbox = { version = "0.5.8", features = ["diff", "harness", "path", "term-svg"] }
snapbox = { version = "0.5.8", features = ["diff", "harness", "path", "term-svg", "cmd", "examples"] }
toml = "0.5.11"

[[bench]]
Expand Down
65 changes: 4 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,14 @@
`annotate-snippets` is a Rust library for annotation of programming code slices.

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

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

```text
error[E0308]: mismatched types
--> src/format.rs:52:1
|
51 | ) -> Option<String> {
| -------------- expected `Option<String>` because of return type
52 | / for ann in annotations {
53 | | match (ann.range.0, ann.range.1) {
54 | | (None, None) => continue,
55 | | (Some(start), Some(end)) if start > end_index => continue,
... |
71 | | }
72 | | }
| |_____^ expected enum `std::option::Option`, found ()
```

[Documentation][]

[Documentation]: https://docs.rs/annotate-snippets/

Usage
-----

```rust
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};

fn main() {
let snippet = Snippet {
title: Some(Annotation {
label: Some("expected type, found `22`"),
id: None,
annotation_type: AnnotationType::Error,
}),
footer: vec![],
slices: vec![Slice {
source: r#" annotations: vec![SourceAnnotation {
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
,
range: <22, 25>,"#,
line_start: 26,
origin: Some("examples/footer.rs"),
fold: true,
annotations: vec![
SourceAnnotation {
label: "",
annotation_type: AnnotationType::Error,
range: (193, 195),
},
SourceAnnotation {
label: "while parsing this struct",
annotation_type: AnnotationType::Info,
range: (34, 50),
},
],
}],
};

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
}
```
![Screenshot](./examples/expected_type.svg)

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

[`cargo fmt`]: https://github.com/rust-lang/rustfmt

[Documentation]: https://docs.rs/annotate-snippets/
4 changes: 2 additions & 2 deletions examples/expected_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ fn main() {
.annotation(Label::info("while parsing this struct").span(34..50)),
);

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(snippet));
}
44 changes: 44 additions & 0 deletions examples/expected_type.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main() {
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`",
));

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(snippet));
}
43 changes: 43 additions & 0 deletions examples/footer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ fn main() {
.annotation(Label::error("expected enum `std::option::Option`").span(26..724)),
);

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(snippet));
}
85 changes: 85 additions & 0 deletions examples/format.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/multislice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ fn main() {
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
.slice(Slice::new("Faa", 129).origin("src/display.rs"));

let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
let renderer = Renderer::styled();
anstream::println!("{}", renderer.render(snippet));
}
Loading