#code #analysis #ascii #errors #debug

annotate-snippets

Library for building code annotations

15 releases

0.9.1 Sep 4, 2021
0.9.0 Jun 28, 2020
0.8.0 Apr 14, 2020
0.7.0 Mar 30, 2020
0.1.0 May 22, 2018

#59 in Rust patterns

Download history 17135/week @ 2022-06-07 20993/week @ 2022-06-14 21394/week @ 2022-06-21 20593/week @ 2022-06-28 20701/week @ 2022-07-05 28241/week @ 2022-07-12 21840/week @ 2022-07-19 23306/week @ 2022-07-26 21573/week @ 2022-08-02 20155/week @ 2022-08-09 22491/week @ 2022-08-16 22500/week @ 2022-08-23 17635/week @ 2022-08-30 18094/week @ 2022-09-06 16845/week @ 2022-09-13 14451/week @ 2022-09-20

70,170 downloads per month
Used in 86 crates (24 directly)

Apache-2.0/MIT

63KB
1.5K SLoC

annotate-snippets

annotate-snippets is a Rust library for annotation of programming code slices.

crates.io Build Status Coverage Status

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:

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

Usage

use annotate_snippets::{
    display_list::{DisplayList, FormatOptions},
    snippet::{Annotation, AnnotationType, 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: (187, 189),
                },
                SourceAnnotation {
                    label: "while parsing this struct",
                    annotation_type: AnnotationType::Info,
                    range: (34, 50),
                },
            ],
        }],
        opt: FormatOptions {
            color: true,
            ..Default::default()
        },
    };

    let dl = DisplayList::from(snippet);
    println!("{}", dl);
}

Local Development

cargo build
cargo test

When submitting a PR please use cargo fmt (nightly).

Dependencies

~93KB