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