• Stars
    star
    1,088
  • Rank 42,566 (Top 0.9 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Beautiful diagnostic reporting for text-based programming languages.

codespan-reporting

Continuous integration Crates.io Docs.rs Matrix

Beautiful diagnostic reporting for text-based programming languages.

Example preview

Languages like Rust and Elm already support beautiful error reporting output, but it can take a significant amount work to implement this for new programming languages! The codespan-reporting crate aims to make beautiful error diagnostics easy and relatively painless for everyone!

We're still working on improving the crate to help it support broader use cases, and improving the quality of the diagnostic rendering, so stay tuned for updates and please give us feedback if you have it. Contributions are also very welcome!

Example

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

// `files::SimpleFile` and `files::SimpleFiles` help you get up and running with
// `codespan-reporting` quickly! More complicated use cases can be supported
// by creating custom implementations of the `files::Files` trait.

let mut files = SimpleFiles::new();

let file_id = files.add(
    "FizzBuzz.fun",
    unindent::unindent(
        r#"
            module FizzBuzz where

            fizz₁ : Nat → String
            fizz₁ num = case (mod num 5) (mod num 3) of
                0 0 => "FizzBuzz"
                0 _ => "Fizz"
                _ 0 => "Buzz"
                _ _ => num

            fizz₂ : Nat → String
            fizz₂ num =
                case (mod num 5) (mod num 3) of
                    0 0 => "FizzBuzz"
                    0 _ => "Fizz"
                    _ 0 => "Buzz"
                    _ _ => num
        "#,
    ),
);

// We normally recommend creating a custom diagnostic data type for your
// application, and then converting that to `codespan-reporting`'s diagnostic
// type, but for the sake of this example we construct it directly.

let diagnostic = Diagnostic::error()
    .with_message("`case` clauses have incompatible types")
    .with_code("E0308")
    .with_labels(vec![
        Label::primary(file_id, 328..331).with_message("expected `String`, found `Nat`"),
        Label::secondary(file_id, 211..331).with_message("`case` clauses have incompatible types"),
        Label::secondary(file_id, 258..268).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 284..290).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 306..312).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 186..192).with_message("expected type `String` found here"),
    ])
    .with_notes(vec![unindent::unindent(
        "
            expected type `String`
                found type `Nat`
        ",
    )]);

// We now set up the writer and configuration, and then finally render the
// diagnostic to standard error.

let writer = StandardStream::stderr(ColorChoice::Always);
let config = codespan_reporting::term::Config::default();

term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;

Running the CLI example

To get an idea of what the colored CLI output looks like, clone the repository and run the following shell command:

cargo run --example term

More examples of using codespan-reporting can be found in the examples directory.

Projects using codespan-reporting

codespan-reporting is currently used in the following projects:

Alternatives to codespan-reporting

There are a number of alternatives to codespan-reporting, including:

These are all ultimately inspired by rustc's excellent error reporting infrastructure.

Contributing

A guide to contributing to codespan-reporting can be found here.

Code of Conduct

Please note that this project is released with a Code of Conduct. By participating in this project you agree to abide by its terms.

More Repositories

1

gl-rs

An OpenGL function pointer loader for Rust
Rust
677
star
2

approx

Approximate floating point equality comparisons and assertions
Rust
149
star
3

language-garden

A garden of small programming language implementations 🪴
OCaml
112
star
4

rust-nbe-for-mltt

Normalization by evaluation for Martin-Löf Type Theory with dependent records
Rust
87
star
5

moniker

Automagical variable binding library for Rust
Rust
63
star
6

algebra

Abstract algebra for Rust (still very much a WIP!)
Rust
60
star
7

color-rs

A library that provides types and conversions for working with various color formats.
Rust
45
star
8

ocaml-flake-example

An overly elaborate example of building a ‘Hello World’ package with Nix flakes, OCaml, and Dune
Nix
40
star
9

chronicle

An event sourced CQRS framework for Rust
Rust
37
star
10

open.gl-tutorials

DEPRECATED
Rust
17
star
11

rusp

A minimal scripting and data language for Rust.
Rust
16
star
12

sax-rs

DEPRECATED - use https://github.com/netvl/rust-xml/ instead.
Rust
12
star
13

elm-stlc

Bidirectional type checker for the simply typed lambda calculus
Elm
12
star
14

bullet-rs

Bindings and wrapper for the Bullet physics C API.
Rust
9
star
15

mia-lang

A little concatenative language
Rust
7
star
16

vscode-mercury

Mercury language integration for VSCode
4
star
17

rust-visitors

Experimenting with visitors in Rust
Rust
3
star
18

glfwvbo

D
3
star
19

odyssey-engine-old

An experimental game engine written in D.
D
3
star
20

idris-power-of-pi

Parts of The Power of Pi, implemented in Idris
Idris
3
star
21

ieee754-rs

IEEE 754-2008 floating point arithmetic for Rust (WIP)
Rust
2
star
22

efl-rs

Servo-specific wrapper for the relevant components of the Enlightenment Foundation Libraries.
Rust
2
star
23

lean_tutorial_exercises

Answers to the exercises in the Lean tutorial
Lean
2
star
24

hedge

Indexed based half-edge mesh implementation
Rust
1
star
25

nixpkgs

My personal Nix configuration
Nix
1
star
26

rust-bay-area-2014-06

Demo OpenGL code from the Rust Bay Area meetup on Game Tech.
Rust
1
star
27

sdl2-rs

Rust bindings and wrapper for SDL2 (Incomplete!)
Rust
1
star