Skip to content

rust-lang/annotate-snippets-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ad80183 · Mar 2, 2024
Mar 1, 2024
Dec 5, 2023
Dec 5, 2023
Feb 29, 2024
Mar 2, 2024
Nov 8, 2023
Nov 9, 2023
Feb 29, 2024
Mar 2, 2024
Mar 2, 2024
May 22, 2018
May 22, 2018
Dec 12, 2023
Nov 9, 2023
Nov 8, 2023
Jan 4, 2024

Repository files navigation

annotate-snippets

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

crates.io build 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::{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));
}

Local Development

cargo build
cargo test

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