streamweave_transformers/sample/
sample_transformer.rs1use std::marker::PhantomData;
2use streamweave::TransformerConfig;
3use streamweave_error::ErrorStrategy;
4
5pub struct SampleTransformer<T>
10where
11 T: std::fmt::Debug + Clone + Send + Sync + 'static,
12{
13 pub probability: f64,
15 pub config: TransformerConfig<T>,
17 pub _phantom: PhantomData<T>,
19}
20
21impl<T> SampleTransformer<T>
22where
23 T: std::fmt::Debug + Clone + Send + Sync + 'static,
24{
25 pub fn new(probability: f64) -> Self {
35 assert!(
36 (0.0..=1.0).contains(&probability),
37 "Probability must be between 0 and 1"
38 );
39 Self {
40 probability,
41 config: TransformerConfig::default(),
42 _phantom: PhantomData,
43 }
44 }
45
46 pub fn with_error_strategy(mut self, strategy: ErrorStrategy<T>) -> Self {
52 self.config.error_strategy = strategy;
53 self
54 }
55
56 pub fn with_name(mut self, name: String) -> Self {
62 self.config.name = Some(name);
63 self
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_sample_transformer_new() {
73 let transformer = SampleTransformer::<i32>::new(0.5);
74 assert_eq!(transformer.probability, 0.5);
75 }
76
77 #[test]
78 fn test_sample_transformer_new_zero() {
79 let transformer = SampleTransformer::<i32>::new(0.0);
80 assert_eq!(transformer.probability, 0.0);
81 }
82
83 #[test]
84 fn test_sample_transformer_new_one() {
85 let transformer = SampleTransformer::<i32>::new(1.0);
86 assert_eq!(transformer.probability, 1.0);
87 }
88
89 #[test]
90 #[should_panic(expected = "Probability must be between 0 and 1")]
91 fn test_sample_transformer_new_invalid_negative() {
92 let _ = SampleTransformer::<i32>::new(-0.1);
93 }
94
95 #[test]
96 #[should_panic(expected = "Probability must be between 0 and 1")]
97 fn test_sample_transformer_new_invalid_above_one() {
98 let _ = SampleTransformer::<i32>::new(1.1);
99 }
100
101 #[test]
102 fn test_sample_transformer_with_error_strategy() {
103 let transformer =
104 SampleTransformer::<i32>::new(0.5).with_error_strategy(ErrorStrategy::<i32>::Skip);
105 assert!(matches!(
106 transformer.config.error_strategy,
107 ErrorStrategy::Skip
108 ));
109 }
110
111 #[test]
112 fn test_sample_transformer_with_name() {
113 let transformer = SampleTransformer::<i32>::new(0.5).with_name("test_sample".to_string());
114 assert_eq!(transformer.config.name, Some("test_sample".to_string()));
115 }
116
117 #[test]
118 fn test_sample_transformer_chaining() {
119 let transformer = SampleTransformer::<i32>::new(0.75)
120 .with_error_strategy(ErrorStrategy::<i32>::Retry(3))
121 .with_name("chained_sample".to_string());
122 assert_eq!(transformer.probability, 0.75);
123 assert!(matches!(
124 transformer.config.error_strategy,
125 ErrorStrategy::Retry(3)
126 ));
127 assert_eq!(transformer.config.name, Some("chained_sample".to_string()));
128 }
129}