streamweave_window/transformers/window_transformer.rs
1use streamweave::TransformerConfig;
2use streamweave_error::ErrorStrategy;
3
4/// A transformer that creates sliding windows of items from a stream.
5///
6/// This transformer groups consecutive items into windows of a specified size,
7/// producing vectors of items as the window slides over the input stream.
8#[derive(Clone)]
9pub struct WindowTransformer<T: std::fmt::Debug + Clone + Send + Sync + 'static> {
10 /// The size of each window.
11 pub size: usize,
12 /// Configuration for the transformer, including error handling strategy.
13 pub config: TransformerConfig<T>,
14 /// Phantom data to track the type parameter.
15 pub _phantom: std::marker::PhantomData<T>,
16}
17
18impl<T: std::fmt::Debug + Clone + Send + Sync + 'static> WindowTransformer<T> {
19 /// Creates a new `WindowTransformer` with the given window size.
20 ///
21 /// # Arguments
22 ///
23 /// * `size` - The size of each window.
24 pub fn new(size: usize) -> Self {
25 Self {
26 size,
27 config: TransformerConfig::<T>::default(),
28 _phantom: std::marker::PhantomData,
29 }
30 }
31
32 /// Sets the error handling strategy for this transformer.
33 ///
34 /// # Arguments
35 ///
36 /// * `strategy` - The error handling strategy to use.
37 pub fn with_error_strategy(mut self, strategy: ErrorStrategy<T>) -> Self {
38 self.config.error_strategy = strategy;
39 self
40 }
41
42 /// Sets the name for this transformer.
43 ///
44 /// # Arguments
45 ///
46 /// * `name` - The name to assign to this transformer.
47 pub fn with_name(mut self, name: String) -> Self {
48 self.config.name = Some(name);
49 self
50 }
51}