Skip to main content

chaincodec_batch/
request.rs

1//! Batch decode request configuration.
2
3use chaincodec_core::{decoder::{ErrorMode, ProgressCallback}, event::RawEvent};
4
5/// Configuration for a batch decode job.
6pub struct BatchRequest {
7    /// The raw events to decode
8    pub logs: Vec<RawEvent>,
9    /// Chain slug — determines which decoder to use
10    pub chain: String,
11    /// Number of parallel Rayon workers (0 = use all available CPUs)
12    pub concurrency: usize,
13    /// Max events per chunk (memory safety)
14    pub chunk_size: usize,
15    /// How to handle decode errors
16    pub error_mode: ErrorMode,
17    /// Optional progress callback
18    pub on_progress: Option<Box<dyn ProgressCallback>>,
19}
20
21impl BatchRequest {
22    pub fn new(chain: impl Into<String>, logs: Vec<RawEvent>) -> Self {
23        Self {
24            logs,
25            chain: chain.into(),
26            concurrency: 0,
27            chunk_size: 10_000,
28            error_mode: ErrorMode::Skip,
29            on_progress: None,
30        }
31    }
32
33    pub fn chunk_size(mut self, n: usize) -> Self {
34        self.chunk_size = n;
35        self
36    }
37
38    pub fn error_mode(mut self, mode: ErrorMode) -> Self {
39        self.error_mode = mode;
40        self
41    }
42
43    pub fn on_progress<F: Fn(usize, usize) + Send + Sync + 'static>(
44        mut self,
45        f: F,
46    ) -> Self {
47        self.on_progress = Some(Box::new(f) as Box<dyn ProgressCallback>);
48        self
49    }
50}