streamweave_transformers/split/split_transformer.rs
1use std::marker::PhantomData;
2use streamweave::TransformerConfig;
3use streamweave_error::ErrorStrategy;
4
5/// A transformer that splits a stream of items into groups based on a predicate.
6///
7/// This transformer groups consecutive items where the predicate returns `true`
8/// into separate vectors, effectively splitting the stream at points where the
9/// predicate returns `false`.
10#[derive(Clone)]
11pub struct SplitTransformer<F, T>
12where
13 F: Send + 'static,
14 T: std::fmt::Debug + Clone + Send + Sync + 'static,
15{
16 /// The predicate function used to determine split points.
17 pub predicate: F,
18 /// Phantom data to track the item type parameter.
19 pub _phantom: std::marker::PhantomData<T>,
20 /// Configuration for the transformer, including error handling strategy.
21 pub config: TransformerConfig<Vec<T>>,
22}
23
24impl<F, T> SplitTransformer<F, T>
25where
26 F: FnMut(&T) -> bool + Send + Clone + 'static,
27 T: std::fmt::Debug + Clone + Send + Sync + 'static,
28{
29 /// Creates a new `SplitTransformer` with the given predicate.
30 ///
31 /// # Arguments
32 ///
33 /// * `predicate` - The function to use for determining split points.
34 pub fn new(predicate: F) -> Self {
35 Self {
36 predicate,
37 _phantom: PhantomData,
38 config: TransformerConfig::default(),
39 }
40 }
41
42 /// Sets the error handling strategy for this transformer.
43 ///
44 /// # Arguments
45 ///
46 /// * `strategy` - The error handling strategy to use.
47 pub fn with_error_strategy(mut self, strategy: ErrorStrategy<Vec<T>>) -> Self {
48 self.config.error_strategy = strategy;
49 self
50 }
51
52 /// Sets the name for this transformer.
53 ///
54 /// # Arguments
55 ///
56 /// * `name` - The name to assign to this transformer.
57 pub fn with_name(mut self, name: String) -> Self {
58 self.config.name = Some(name);
59 self
60 }
61}