use crate::{Batch, BatchId};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BatchEvent {
Created {
batch: Batch,
},
TopUp {
batch_id: BatchId,
new_value: u128,
},
DepthIncrease {
batch_id: BatchId,
new_depth: u8,
},
Expired {
batch_id: BatchId,
},
}
impl BatchEvent {
pub const fn batch_id(&self) -> BatchId {
match self {
Self::Created { batch } => batch.id(),
Self::TopUp { batch_id, .. } => *batch_id,
Self::DepthIncrease { batch_id, .. } => *batch_id,
Self::Expired { batch_id } => *batch_id,
}
}
}
pub trait BatchEventHandler {
type Error;
fn handle_event(&mut self, event: BatchEvent) -> Result<(), Self::Error>;
fn handle_events(&mut self, events: Vec<BatchEvent>) -> Result<(), Self::Error> {
for event in events {
self.handle_event(event)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{Address, B256};
#[test]
fn test_batch_event_batch_id() {
let batch = Batch::new(
B256::repeat_byte(1),
1000,
100,
Address::ZERO,
20,
16,
false,
);
let batch_id = batch.id();
let created = BatchEvent::Created { batch };
assert_eq!(created.batch_id(), batch_id);
let topup = BatchEvent::TopUp {
batch_id,
new_value: 2000,
};
assert_eq!(topup.batch_id(), batch_id);
let depth = BatchEvent::DepthIncrease {
batch_id,
new_depth: 21,
};
assert_eq!(depth.batch_id(), batch_id);
let expired = BatchEvent::Expired { batch_id };
assert_eq!(expired.batch_id(), batch_id);
}
}