`srcerr` 0.1.0 released

Heya, I've just published srcerr (repo), which provides data structures that presents errors like Rust's compilation errors. It's meant to make it easier to display errors from any source input, whether it is configuration read from a file, or JSON string from a REST API.

Releasing here for early feedback as to whether the API makes sense (e.g. suggestions are part of the top level SourceError type, instead of ErrorCode trait).

What It Does

Given the user provides the path, content, line numbers etc., it formats the errors like rustc does:

error[E1]: `chosen` value `ghi` is invalid.
  --> examples/source_ref_hint.yaml:6:9
   |
 6 | chosen: "ghi"
   |         ^^^^^
   = note: expected one of: `abc`, `def`

help: `chosen` value must come from one of `available` values:
  --> examples/source_ref_hint.yaml:2:1
   |
 2 | available:
   | ---------- hint: first defined here
 3 |   - "abc"
 4 |   - "def"
   |

and this:

error[E1]: Value `150` is invalid.
  --> /mnt/data/work/github/azriel91/srcerr/examples/long_expr_context.json:1:101
   |
 1 | .. "p":150, ..
   |        ^^^
   |        |
   |        101
   |
   = hint: expected value to be less than 26

It ships with PlainTextFormatter and AnsiColorFormatter to print the errors. The DefaultFormatter will use either, depending on whether the "ansi_color" feature is enabled (which it is by default).

Extendable Output

Users can provide their own Formatter / Styler -- see the html example, which outputs a rustc-like error in HTML (jsfiddle preview).

Trivia

  1. no, I haven't read how rust-lang/rust does it, but while making this crate, it did some pretty cool things that srcerr cannot:

  2. There is a source-error crate, but I dreamt this one up before discovering it, and wanted to make it

5 Likes

congrats on the release!

from the maintainer of https://crates.io/crates/annotate-snippets :slight_smile:

3 Likes

thank you very much :bowing_man:

and no wonder I didn't find it before beginning, always had "error" in my search term :stuck_out_tongue:

Looks nice, will give it a shot in the upcoming project!

I'd like to ask, what are the intended uses of Span? Looking through the source code, I wasn't able to find any place where it is used currently.

1 Like

ah, I think it was meant to be the byte range of the overall content, but looks like it is unused.
Shall remove it, thank you :bowing_man:!