#code #analysis #ascii #errors #debug

annotate-snippets

Library for building code annotations

14 unstable releases (6 breaking)

✓ Uses Rust 2018 edition

0.9.0 Jun 28, 2020
0.7.0 Mar 30, 2020
0.6.1 Jul 23, 2019
0.5.0 Jan 5, 2019
0.1.0 May 22, 2018

#18 in Rust patterns

Download history 7517/week @ 2020-03-16 9536/week @ 2020-03-23 11306/week @ 2020-03-30 10659/week @ 2020-04-06 12272/week @ 2020-04-13 13469/week @ 2020-04-20 12426/week @ 2020-04-27 14927/week @ 2020-05-04 13815/week @ 2020-05-11 13439/week @ 2020-05-18 16110/week @ 2020-05-25 15676/week @ 2020-06-01 21597/week @ 2020-06-08 19417/week @ 2020-06-15 16256/week @ 2020-06-22 17418/week @ 2020-06-29

62,716 downloads per month
Used in 54 crates (12 directly)

Apache-2.0/MIT

60KB
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: (205, 207),
                },
                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

~66KB