use std::error::Error;
use std::fmt::{self, Display};
use std::slice;
use crate::Issue;
#[derive(Debug, Clone, PartialEq)]
pub struct Issues {
issues: Vec<Issue>,
}
impl Issues {
pub(crate) fn of(found: Vec<Issue>) -> Option<Self> {
(!found.is_empty()).then_some(Self { issues: found })
}
pub fn iter(&self) -> slice::Iter<'_, Issue> {
self.issues.iter()
}
#[must_use]
pub fn as_slice(&self) -> &[Issue] {
&self.issues
}
}
impl Display for Issues {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let (first, others) = self.issues.split_first().expect("issues are never empty");
write!(formatter, "{first}")?;
for issue in others {
write!(formatter, "\n{issue}")?;
}
Ok(())
}
}
impl Error for Issues {}
impl IntoIterator for Issues {
type Item = Issue;
type IntoIter = std::vec::IntoIter<Issue>;
fn into_iter(self) -> Self::IntoIter {
self.issues.into_iter()
}
}
impl<'a> IntoIterator for &'a Issues {
type Item = &'a Issue;
type IntoIter = slice::Iter<'a, Issue>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}