Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic diagnostic #262

Merged
merged 30 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
09d7d15
Remove dot
gavrilikhin-d May 5, 2023
e6d2eed
Add skeleton
gavrilikhin-d May 5, 2023
ab20069
DynamicDiagnostic -> MietteDiagnostic
gavrilikhin-d May 6, 2023
01b34a4
Make `Severity::Error` to be default severity
gavrilikhin-d May 6, 2023
61d43f8
Add severity field
gavrilikhin-d May 6, 2023
53b21ea
Add help field
gavrilikhin-d May 6, 2023
a1f602c
Add url field
gavrilikhin-d May 6, 2023
c6e977b
Add labels field
gavrilikhin-d May 6, 2023
a3ee52a
Add convenience function to `LabeledSpan`
gavrilikhin-d May 6, 2023
8aaba99
Use convenience functions in examples
gavrilikhin-d May 6, 2023
021eb01
Adjust `miette!` a little bit
gavrilikhin-d May 6, 2023
b9a892f
Use `Option<Severity>`
gavrilikhin-d May 6, 2023
c2d793e
labels: `Option<Vec<_>>`
gavrilikhin-d May 6, 2023
bcf18f8
Fully implement support for `MietteDiagnostic`-like arguments in `mie…
gavrilikhin-d May 6, 2023
287ffc5
Add `miette_diagnostic!`
gavrilikhin-d May 6, 2023
4f0bc3e
Add `ensure!` support
gavrilikhin-d May 6, 2023
52e2dcb
Add `bail!` support
gavrilikhin-d May 6, 2023
7c4dd12
Add docs
gavrilikhin-d May 6, 2023
0e5512a
Add dot
gavrilikhin-d May 7, 2023
e35a63a
description -> message
gavrilikhin-d May 7, 2023
4148552
`miette_diagnostic!` -> `diagnostic!`
gavrilikhin-d May 7, 2023
b658720
Add `and_label(s)`
gavrilikhin-d May 7, 2023
cb5a1d3
Implement interpolation
gavrilikhin-d May 7, 2023
55f41a6
Remove literal case from ensure
gavrilikhin-d May 7, 2023
3e62212
Fix macro
gavrilikhin-d May 12, 2023
5e67953
Use `mut self` in builder functions
gavrilikhin-d May 12, 2023
5c94dd0
Sync README.md
gavrilikhin-d May 12, 2023
253a0f8
Update .tpl
gavrilikhin-d May 12, 2023
4520d66
Fix clippy
gavrilikhin-d May 13, 2023
7fb4cb2
Add and use `no-format-args-capture` flag
gavrilikhin-d May 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

## Introduction

Thank you so much for your interest in contributing!. All types of contributions are encouraged and valued. See the [table of contents](#toc) for different ways to help and details about how this project handles them!📝
Thank you so much for your interest in contributing! All types of contributions are encouraged and valued. See the [table of contents](#toc) for different ways to help and details about how this project handles them!📝

Please make sure to read the relevant section before making your contribution! It will make it a lot easier for us maintainers to make the most of it and smooth out the experience for all involved. 💚

Expand Down
67 changes: 43 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
You run miette? You run her code like the software? Oh. Oh! Error code for
coder! Error code for One Thousand Lines!

### About
## About

`miette` is a diagnostic library for Rust. It includes a series of
traits/protocols that allow you to hook into its error reporting facilities,
Expand Down Expand Up @@ -32,7 +32,7 @@ output like in the screenshots above.** You should only do this in your
toplevel crate, as the fancy feature pulls in a number of dependencies that
libraries and such might not want.

### Table of Contents <!-- omit in toc -->
## Table of Contents <!-- omit in toc -->

- [About](#about)
- [Features](#features)
Expand All @@ -47,10 +47,11 @@ libraries and such might not want.
- [... multiple related errors](#-multiple-related-errors)
- [... delayed source code](#-delayed-source-code)
- [... handler options](#-handler-options)
- [... dynamic diagnostics](#-dynamic-diagnostics)
- [Acknowledgements](#acknowledgements)
- [License](#license)

### Features
## Features

- Generic [`Diagnostic`] protocol, compatible (and dependent on)
[`std::error::Error`].
Expand All @@ -75,7 +76,7 @@ the following features:
- Cause chain printing
- Turns diagnostic codes into links in [supported terminals](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda).

### Installing
## Installing

```sh
$ cargo add miette
Expand All @@ -87,7 +88,7 @@ If you want to use the fancy printer in all these screenshots:
$ cargo add miette --features fancy
```

### Example
## Example

```rust
/*
Expand Down Expand Up @@ -169,9 +170,9 @@ diagnostic help: Change int or string to be the right types and try again.
diagnostic code: nu::parser::unsupported_operation
For more details, see https://docs.rs/nu-parser/0.1.0/nu-parser/enum.ParseError.html#variant.UnsupportedOperation">

### Using
## Using

#### ... in libraries
### ... in libraries

`miette` is _fully compatible_ with library usage. Consumers who don't know
about, or don't want, `miette` features can safely use its error types as
Expand Down Expand Up @@ -205,7 +206,7 @@ Then, return this error type from all your fallible public APIs. It's a best
practice to wrap any "external" error types in your error `enum` instead of
using something like [`Report`] in a library.

#### ... in application code
### ... in application code

Application code tends to work a little differently than libraries. You
don't always need or care to define dedicated error wrappers for errors
Expand Down Expand Up @@ -247,11 +248,11 @@ pub fn some_tool() -> Result<Version> {
}
```

To construct your own simple adhoc error use the `miette::miette!` macro:
To construct your own simple adhoc error use the [`miette!`] macro:

```rust
// my_app/lib/my_internal_file.rs
use miette::{IntoDiagnostic, Result, WrapErr, miette};
use miette::{miette, IntoDiagnostic, Result, WrapErr};
use semver::Version;

pub fn some_tool() -> Result<Version> {
Expand All @@ -262,9 +263,7 @@ pub fn some_tool() -> Result<Version> {
}
```

There are also similar `miette::bail!` and `miette::ensure!` macros.

#### ... in `main()`
### ... in `main()`

`main()` is just like any other part of your application-internal code. Use
`Result` as your return value, and it will pretty-print your diagnostics
Expand Down Expand Up @@ -294,7 +293,7 @@ enabled:
miette = { version = "X.Y.Z", features = ["fancy"] }
```

#### ... diagnostic code URLs
### ... diagnostic code URLs

`miette` supports providing a URL for individual diagnostics. This URL will
be displayed as an actual link in supported terminals, like so:
Expand Down Expand Up @@ -347,7 +346,7 @@ use thiserror::Error;
struct MyErr;
```

#### ... snippets
### ... snippets

Along with its general error handling and reporting features, `miette` also
includes facilities for adding error spans/annotations/labels to your
Expand Down Expand Up @@ -395,8 +394,7 @@ pub struct MyErrorType {
}
```

##### ... help text

#### ... help text
`miette` provides two facilities for supplying help text for your errors:

The first is the `#[help()]` format attribute that applies to structs or
Expand Down Expand Up @@ -432,7 +430,7 @@ let err = Foo {
};
```

#### ... multiple related errors
### ... multiple related errors

`miette` supports collecting multiple errors into a single diagnostic, and
printing them all together nicely.
Expand All @@ -452,7 +450,7 @@ struct MyError {
}
```

#### ... delayed source code
### ... delayed source code

Sometimes it makes sense to add source code to the error message later.
One option is to use [`with_source_code()`](Report::with_source_code)
Expand Down Expand Up @@ -535,7 +533,7 @@ fn main() -> miette::Result<()> {
}
```

#### ... Diagnostic-based error sources.
### ... Diagnostic-based error sources.

When one uses the `#[source]` attribute on a field, that usually comes
from `thiserror`, and implements a method for
Expand Down Expand Up @@ -568,7 +566,7 @@ struct MyError {
struct OtherError;
```

#### ... handler options
### ... handler options

[`MietteHandler`] is the default handler, and is very customizable. In
most cases, you can simply use [`MietteHandlerOpts`] to tweak its behavior
Expand All @@ -587,13 +585,32 @@ miette::set_hook(Box::new(|_| {
.build(),
)
}))

```

See the docs for [`MietteHandlerOpts`] for more details on what you can
customize!

### Acknowledgements
### ... dynamic diagnostics

If you...
- ...don't know all the possible errors upfront
- ...need to serialize/deserialize errors
then you may want to use [`miette!`], [`diagnostic!`] macros or
[`MietteDiagnostic`] directly to create diagnostic on the fly.

```rust
let source = "2 + 2 * 2 = 8".to_string();
let report = miette!(
labels = vec[
LabeledSpan::at(12..13, "this should be 6"),
],
help = "'*' has greater precedence than '+'",
"Wrong answer"
).with_source_code(source);
println!("{:?}", report)
```

## Acknowledgements

`miette` was not developed in a void. It owes enormous credit to various
other projects and their authors:
Expand All @@ -612,7 +629,7 @@ other projects and their authors:
- [`ariadne`](https://crates.io/crates/ariadne) for pushing forward how
_pretty_ these diagnostics can really look!

### License
## License

`miette` is released to the Rust community under the [Apache license
2.0](./LICENSE).
Expand All @@ -623,11 +640,13 @@ under the Apache License. Some code is taken from
[`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.

[`miette!`]: https://docs.rs/miette/latest/miette/macro.miette.html
[`diagnostic!`]: https://docs.rs/miette/latest/miette/macro.diagnostic.html
[`std::error::Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
[`Diagnostic`]: https://docs.rs/miette/latest/miette/trait.Diagnostic.html
[`IntoDiagnostic`]: https://docs.rs/miette/latest/miette/trait.IntoDiagnostic.html
[`MietteHandlerOpts`]: https://docs.rs/miette/latest/miette/struct.MietteHandlerOpts.html
[`MietteHandler`]: https://docs.rs/miette/latest/miette/struct.MietteHandler.html
[`MietteDiagnostic`]: https://docs.rs/miette/latest/miette/struct.MietteDiagnostic.html
[`Report`]: https://docs.rs/miette/latest/miette/struct.Report.html
[`ReportHandler`]: https://docs.rs/miette/latest/miette/struct.ReportHandler.html
[`Result`]: https://docs.rs/miette/latest/miette/type.Result.html
Expand Down
4 changes: 3 additions & 1 deletion README.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
{{readme}}

[`miette!`]: https://docs.rs/miette/latest/miette/macro.miette.html
[`diagnostic!`]: https://docs.rs/miette/latest/miette/macro.diagnostic.html
[`std::error::Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
[`Diagnostic`]: https://docs.rs/miette/latest/miette/struct.Diagnostic.html
[`Diagnostic`]: https://docs.rs/miette/latest/miette/trait.Diagnostic.html
[`IntoDiagnostic`]: https://docs.rs/miette/latest/miette/trait.IntoDiagnostic.html
[`MietteHandlerOpts`]: https://docs.rs/miette/latest/miette/struct.MietteHandlerOpts.html
[`MietteHandler`]: https://docs.rs/miette/latest/miette/struct.MietteHandler.html
[`MietteDiagnostic`]: https://docs.rs/miette/latest/miette/struct.MietteDiagnostic.html
[`Report`]: https://docs.rs/miette/latest/miette/struct.Report.html
[`ReportHandler`]: https://docs.rs/miette/latest/miette/struct.ReportHandler.html
[`Result`]: https://docs.rs/miette/latest/miette/type.Result.html
Expand Down
Loading