ass_core/parser/streaming/delta/
batch.rs1use super::ParseDelta;
7use alloc::vec::Vec;
8
9#[derive(Debug, Clone)]
14pub struct DeltaBatch<'a> {
15 deltas: Vec<ParseDelta<'a>>,
17}
18
19impl<'a> DeltaBatch<'a> {
20 #[must_use]
22 pub const fn new() -> Self {
23 Self { deltas: Vec::new() }
24 }
25
26 #[must_use]
28 pub const fn from_deltas(deltas: Vec<ParseDelta<'a>>) -> Self {
29 Self { deltas }
30 }
31
32 pub fn push(&mut self, delta: ParseDelta<'a>) {
34 self.deltas.push(delta);
35 }
36
37 pub fn extend(&mut self, other_deltas: impl IntoIterator<Item = ParseDelta<'a>>) {
39 self.deltas.extend(other_deltas);
40 }
41
42 #[must_use]
44 #[allow(clippy::missing_const_for_fn)]
45 pub fn deltas(&self) -> &[ParseDelta<'a>] {
46 &self.deltas
47 }
48
49 #[must_use]
51 pub fn into_deltas(self) -> Vec<ParseDelta<'a>> {
52 self.deltas
53 }
54
55 #[must_use]
57 pub fn is_empty(&self) -> bool {
58 self.deltas.is_empty()
59 }
60
61 #[must_use]
63 pub fn len(&self) -> usize {
64 self.deltas.len()
65 }
66
67 #[must_use]
69 pub fn filter<F>(&self, predicate: F) -> Self
70 where
71 F: Fn(&ParseDelta<'a>) -> bool,
72 {
73 let filtered = self
74 .deltas
75 .iter()
76 .filter(|d| predicate(d))
77 .cloned()
78 .collect();
79 DeltaBatch::from_deltas(filtered)
80 }
81
82 #[must_use]
84 pub fn structural_only(&self) -> Self {
85 self.filter(ParseDelta::is_structural)
86 }
87
88 #[must_use]
90 pub fn errors_only(&self) -> Self {
91 self.filter(ParseDelta::is_error)
92 }
93
94 pub fn has_errors(&self) -> bool {
96 self.deltas.iter().any(ParseDelta::is_error)
97 }
98}
99
100impl Default for DeltaBatch<'_> {
101 fn default() -> Self {
102 Self::new()
103 }
104}
105
106impl<'a> FromIterator<ParseDelta<'a>> for DeltaBatch<'a> {
107 fn from_iter<T: IntoIterator<Item = ParseDelta<'a>>>(iter: T) -> Self {
108 Self::from_deltas(iter.into_iter().collect())
109 }
110}