diff --git a/src/lib.rs b/src/lib.rs index 5a0b83c..c0b089d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,6 +140,15 @@ pub trait IteratorExt: Iterator { fn into_non_error_diagnostic<'d>(self) -> DiagnosticResult<'d, ()> where Self: Iterator> + Sized; + + /// Converts from a type that implements `Iterator>` 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> + Sized; } impl IteratorExt for I @@ -152,6 +161,29 @@ where { Ok(Diagnosed((), self.collect())) } + + fn into_diagnostic_by_severity<'d>(self) -> DiagnosticResult<'d, ()> + where + Self: Iterator> + 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`].