smarty_rust_sdk/sdk/
batch.rs1use crate::sdk::MAX_BATCH_SIZE;
2
3use thiserror::Error;
4
5#[derive(Clone)]
6pub struct Batch<T> {
9 lookups: Vec<T>,
10}
11
12impl<T> Default for Batch<T> {
13 fn default() -> Self {
14 Self { lookups: vec![] }
15 }
16}
17
18#[derive(Error, Debug)]
19#[error("Batch is full")]
20pub struct BatchError;
21
22impl<T> Batch<T> {
23 pub fn push(&mut self, lookup: T) -> Result<(), BatchError> {
25 if self.is_full() {
26 return Err(BatchError);
27 }
28
29 self.lookups.push(lookup);
30
31 Ok(())
32 }
33
34 pub fn is_full(&self) -> bool {
36 self.lookups.len() >= MAX_BATCH_SIZE
37 }
38
39 pub fn is_empty(&self) -> bool {
41 self.lookups.is_empty()
42 }
43
44 pub fn len(&self) -> usize {
46 self.lookups.len()
47 }
48
49 pub fn records(&self) -> &Vec<T> {
53 &self.lookups
54 }
55
56 pub fn records_mut(&mut self) -> &mut Vec<T> {
60 &mut self.lookups
61 }
62
63 pub fn clear(&mut self) {
65 self.lookups.clear();
66 }
67}