Skip to content

Commit

Permalink
Support conversion into DiagnosticResult by Severity.
Browse files Browse the repository at this point in the history
This change introduces the `into_diagnostic_by_severity` extension
method, which converts an iterator of diagnostics into a
`DiagnosticResult` by examining the `Severity`s.
  • Loading branch information
olson-sean-k committed Nov 20, 2024
1 parent a486a47 commit d9002c8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ pub trait IteratorExt: Iterator {
fn into_non_error_diagnostic<'d>(self) -> DiagnosticResult<'d, ()>
where
Self: Iterator<Item = BoxedDiagnostic<'d>> + Sized;

/// Converts from a type that implements `Iterator<Item = BoxedDiagnostic<'d>>` into
/// `DiagnosticResult<'d, ()>` by [`Severity`].
///
/// If any [`Diagnostic`] item has an [error `Severity`][`Severity::Error`], then the items are
/// interpreted as errors. Otherwise, the items are interpreted as non-errors.
fn into_diagnostic_by_severity<'d>(self) -> DiagnosticResult<'d, ()>
where
Self: Iterator<Item = BoxedDiagnostic<'d>> + Sized;
}

impl<I> IteratorExt for I
Expand All @@ -152,6 +161,29 @@ where
{
Ok(Diagnosed((), self.collect()))
}

fn into_diagnostic_by_severity<'d>(self) -> DiagnosticResult<'d, ()>
where
Self: Iterator<Item = BoxedDiagnostic<'d>> + Sized,
{
let diagnostics: Vec<_> = self.collect();
match Vec1::try_from(diagnostics) {
Ok(diagnostics) => {
if diagnostics
.iter()
.map(AsRef::as_ref)
.flat_map(Diagnostic::severity)
.any(|severity| matches!(severity, Severity::Error))
{
Err(Error(diagnostics))
}
else {
Ok(Diagnosed((), diagnostics.into()))
}
}
_ => Diagnosed::ok(()),
}
}
}

/// Extension methods for [`Iterator1`].
Expand Down

0 comments on commit d9002c8

Please sign in to comment.