diff --git a/src/lib.rs b/src/lib.rs index 7674f2b..7891511 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,7 @@ //! - [... handler options](#-handler-options) //! - [... dynamic diagnostics](#-dynamic-diagnostics) //! - [... syntax highlighting](#-syntax-highlighting) +//! - [... collection of labels](#-collection-of-labels) //! - [Acknowledgements](#acknowledgements) //! - [License](#license) //! @@ -672,6 +673,57 @@ //! [`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,ignore +//! #[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())); +//! ``` +//! +//! A collection can also be of `LabeledSpan` if you want to have different text +//! for different labels. Labels with no text will use the one from the `label` +//! attribute +//! +//! ```rust,ignore +//! #[derive(Debug, Diagnostic, Error)] +//! #[error("oops!")] +//! struct MyError { +//! #[label("main issue")] +//! primary_span: SourceSpan, +//! +//! #[label(collection, "related to this")] +//! other_spans: Vec, // LabeledSpan +//! } +//! +//! let report: miette::Report = MyError { +//! primary_span: (6, 9).into(), +//! other_spans: vec![ +//! LabeledSpan::new(None, 19, 7), // Use default text `related to this` +//! LabeledSpan::new(Some("and also this".to_string()), 30, 11), // Use specific text +//! ], +//! }.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.