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

Label collection fixes and cleanup #343

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
74 changes: 36 additions & 38 deletions miette-derive/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl Labels {

pub(crate) fn gen_struct(&self, fields: &syn::Fields) -> Option<TokenStream> {
let (display_pat, display_members) = display_pat_members(fields);
let labels_gen_var = quote! { labels };
let labels = self.0.iter().filter_map(|highlight| {
let Label {
span,
Expand Down Expand Up @@ -194,11 +193,11 @@ impl Labels {
))
})
});
let collections = self.0.iter().filter_map(|label| {
let collections_chain = self.0.iter().filter_map(|label| {
let Label {
span,
label,
ty,
ty: _,
lbl_ty,
} = label;
if *lbl_ty != LabelType::Collection {
Expand All @@ -211,18 +210,17 @@ impl Labels {
quote! { std::option::Option::None }
};
Some(quote! {
let display = #display;
#labels_gen_var.extend(self.#span.iter().map(|label| {
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(label)
.map(|span| {
use miette::macro_helpers::{ToLabelSpanWrapper,ToLabeledSpan};
let mut labeled_span = ToLabelSpanWrapper::to_labeled_span(span.clone());
if #display.is_some() && labeled_span.label().is_none() {
labeled_span.set_label(#display)
}
labeled_span
})
}));
.chain({
let display = #display;
self.#span.iter().map(move |span| {
use miette::macro_helpers::{ToLabelSpanWrapper,ToLabeledSpan};
let mut labeled_span = ToLabelSpanWrapper::to_labeled_span(span.clone());
if display.is_some() && labeled_span.label().is_none() {
labeled_span.set_label(display.clone())
}
Some(labeled_span)
})
})
})
});

Expand All @@ -232,12 +230,13 @@ impl Labels {
use miette::macro_helpers::ToOption;
let Self #display_pat = self;

let mut #labels_gen_var = vec![
let labels_iter = vec![
#(#labels),*
];
#(#collections)*
]
.into_iter()
#(#collections_chain)*;

std::option::Option::Some(Box::new(#labels_gen_var.into_iter().filter(Option::is_some).map(Option::unwrap)))
std::option::Option::Some(Box::new(labels_iter.filter(Option::is_some).map(Option::unwrap)))
}
})
}
Expand All @@ -249,7 +248,6 @@ impl Labels {
|ident, fields, DiagnosticConcreteArgs { labels, .. }| {
let (display_pat, display_members) = display_pat_members(fields);
labels.as_ref().and_then(|labels| {
let labels_gen_var = quote! { labels };
let variant_labels = labels.0.iter().filter_map(|label| {
let Label { span, label, ty, lbl_ty } = label;
if *lbl_ty == LabelType::Collection {
Expand Down Expand Up @@ -282,8 +280,8 @@ impl Labels {
))
})
});
let collections = labels.0.iter().filter_map(|label| {
let Label { span, label, ty, lbl_ty } = label;
let collections_chain = labels.0.iter().filter_map(|label| {
let Label { span, label, ty: _, lbl_ty } = label;
if *lbl_ty != LabelType::Collection {
return None;
}
Expand All @@ -300,18 +298,17 @@ impl Labels {
quote! { std::option::Option::None }
};
Some(quote! {
let display = #display;
#labels_gen_var.extend(#field.iter().map(|label| {
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(label)
.map(|span| {
use miette::macro_helpers::{ToLabelSpanWrapper,ToLabeledSpan};
let mut labeled_span = ToLabelSpanWrapper::to_labeled_span(span.clone());
if #display.is_some() && labeled_span.label().is_none() {
labeled_span.set_label(#display)
}
labeled_span
})
}));
.chain({
let display = #display;
#field.iter().map(move |span| {
use miette::macro_helpers::{ToLabelSpanWrapper,ToLabeledSpan};
let mut labeled_span = ToLabelSpanWrapper::to_labeled_span(span.clone());
if display.is_some() && labeled_span.label().is_none() {
labeled_span.set_label(display.clone());
}
Some(labeled_span)
})
})
})
});
let variant_name = ident.clone();
Expand All @@ -320,11 +317,12 @@ impl Labels {
_ => Some(quote! {
Self::#variant_name #display_pat => {
use miette::macro_helpers::ToOption;
let mut #labels_gen_var = vec![
let labels_iter = vec![
#(#variant_labels),*
];
#(#collections)*
std::option::Option::Some(std::boxed::Box::new(#labels_gen_var.into_iter().filter(Option::is_some).map(Option::unwrap)))
]
.into_iter()
#(#collections_chain)*;
std::option::Option::Some(std::boxed::Box::new(labels_iter.filter(Option::is_some).map(Option::unwrap)))
}
}),
}
Expand Down
52 changes: 52 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//!
Expand Down Expand Up @@ -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<Range<usize>>,
//! }
//!
//! 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>, // 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.
Expand Down