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

proposal: add Report::with_labels #47

Merged
merged 1 commit into from Sep 26, 2022
Merged

Conversation

superhawk610
Copy link
Contributor

This PR adds Report::with_labels, a chainable equivalent of Report::add_labels. This brings a minor DX improvement for the common use case where a report includes an iterable of errors, such as when parsing input with chumsky.

Before

if let Err(errors) = parser().parse(&input) {
    let mut report =
        Report::build(ReportKind::Error, &filename, 0).with_message("Error parsing file");
    report.add_labels(errors.iter().map(|error| {
        Label::new((&filename, error.span())).with_message(format!("{}", error))
    }));
    report
        .finish()
        .eprint((&filename, Source::from(&input)))
        .unwrap();
}

After

if let Err(errors) = parser().parse(&input) {
    Report::build(ReportKind::Error, &filename, 0)
        .with_message("Error parsing file");
        .with_labels(errors.iter().map(|error| {
            Label::new((&filename, error.span())).with_message(format!("{}", error))
        }))
        .finish()
        .eprint((&filename, Source::from(&input)))
        .unwrap();
}

@zesterer
Copy link
Owner

This is a nice improvement, thanks!

@zesterer zesterer merged commit 21b54f4 into zesterer:main Sep 26, 2022
@superhawk610 superhawk610 deleted the patch-1 branch September 26, 2022 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants