From 6d01ca1c2da79b4e86889a3c0fa99c76bda86bd4 Mon Sep 17 00:00:00 2001 From: Nahor Date: Thu, 15 Feb 2024 14:11:30 -0800 Subject: [PATCH] feat(collection): add support for collection of labels (#315) Allow errors to have a number of labels determined at runtime. An example of this is when the rust compiler labels all the arms of a `match` expression when one of them has an incompatible type --- README.md | 26 ++++ miette-derive/src/label.rs | 213 +++++++++++++++++++--------- tests/test_derive_collection.rs | 236 ++++++++++++++++++++++++++++++++ 3 files changed, 409 insertions(+), 66 deletions(-) create mode 100644 tests/test_derive_collection.rs diff --git a/README.md b/README.md index 0e936b3..19a7917 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ libraries and such might not want. - [... handler options](#-handler-options) - [... dynamic diagnostics](#-dynamic-diagnostics) - [... syntax highlighting](#-syntax-highlighting) + - [... collection of labels](#-collection-of-labels) - [Acknowledgements](#acknowledgements) - [License](#license) @@ -671,6 +672,31 @@ trait to [`MietteHandlerOpts`] by calling the [`with_syntax_highlighting`](MietteHandlerOpts::with_syntax_highlighting) method. See the [`highlighters`] module docs for more details. +#### ... collection of labels + +When the number of labels is unknown, you can use a collection of `SourceSpan` +(or any type convertible into `SourceSpan`). For this, add the `collection` +parameter to `label` and use any type than can be iterated over for the field. + +```rust +#[derive(Debug, Diagnostic, Error)] +#[error("oops!")] +struct MyError { + #[label("main issue")] + primary_span: SourceSpan, + + #[label(collection, "related to this")] + other_spans: Vec>, +} + +let report: miette::Report = MyError { + primary_span: (6, 9).into(), + other_spans: vec![19..26, 30..41], +}.into(); + +println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string())); +``` + ### MSRV This crate requires rustc 1.70.0 or later. diff --git a/miette-derive/src/label.rs b/miette-derive/src/label.rs index dd5ec69..fa4d29a 100644 --- a/miette-derive/src/label.rs +++ b/miette-derive/src/label.rs @@ -16,16 +16,23 @@ use crate::{ pub struct Labels(Vec