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
-
no, I haven't read how
rust-lang/rust
does it, but while making this crate, it did some pretty cool things thatsrcerr
cannot:-
multiple hints:
-
multiple line highlight
-
-
There is a
source-error
crate, but I dreamt this one up before discovering it, and wanted to make it