use super::ParseDelta;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct DeltaBatch<'a> {
deltas: Vec<ParseDelta<'a>>,
}
impl<'a> DeltaBatch<'a> {
#[must_use]
pub const fn new() -> Self {
Self { deltas: Vec::new() }
}
#[must_use]
pub const fn from_deltas(deltas: Vec<ParseDelta<'a>>) -> Self {
Self { deltas }
}
pub fn push(&mut self, delta: ParseDelta<'a>) {
self.deltas.push(delta);
}
pub fn extend(&mut self, other_deltas: impl IntoIterator<Item = ParseDelta<'a>>) {
self.deltas.extend(other_deltas);
}
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn deltas(&self) -> &[ParseDelta<'a>] {
&self.deltas
}
#[must_use]
pub fn into_deltas(self) -> Vec<ParseDelta<'a>> {
self.deltas
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.deltas.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.deltas.len()
}
#[must_use]
pub fn filter<F>(&self, predicate: F) -> Self
where
F: Fn(&ParseDelta<'a>) -> bool,
{
let filtered = self
.deltas
.iter()
.filter(|d| predicate(d))
.cloned()
.collect();
DeltaBatch::from_deltas(filtered)
}
#[must_use]
pub fn structural_only(&self) -> Self {
self.filter(ParseDelta::is_structural)
}
#[must_use]
pub fn errors_only(&self) -> Self {
self.filter(ParseDelta::is_error)
}
pub fn has_errors(&self) -> bool {
self.deltas.iter().any(ParseDelta::is_error)
}
}
impl Default for DeltaBatch<'_> {
fn default() -> Self {
Self::new()
}
}
impl<'a> FromIterator<ParseDelta<'a>> for DeltaBatch<'a> {
fn from_iter<T: IntoIterator<Item = ParseDelta<'a>>>(iter: T) -> Self {
Self::from_deltas(iter.into_iter().collect())
}
}