cougr_core/standards/
batch.rs1use alloc::vec::Vec;
2
3use super::error::StandardsError;
4
5#[derive(Clone, Debug)]
7pub struct BatchExecutor {
8 max_size: usize,
9}
10
11impl BatchExecutor {
12 pub fn new(max_size: usize) -> Self {
13 Self { max_size }
14 }
15
16 pub fn max_size(&self) -> usize {
17 self.max_size
18 }
19
20 pub fn validate_len(&self, len: usize) -> Result<(), StandardsError> {
21 if len == 0 {
22 return Err(StandardsError::BatchEmpty);
23 }
24 if len > self.max_size {
25 return Err(StandardsError::BatchTooLarge);
26 }
27 Ok(())
28 }
29
30 pub fn execute<T, R>(
31 &self,
32 items: &[T],
33 mut executor: impl FnMut(&T) -> Result<R, StandardsError>,
34 ) -> Result<Vec<R>, StandardsError> {
35 self.validate_len(items.len())?;
36
37 let mut results = Vec::with_capacity(items.len());
38 for item in items {
39 results.push(executor(item)?);
40 }
41 Ok(results)
42 }
43}