pipeflow 0.0.4

A lightweight, configuration-driven data pipeline framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Filter step for conditional filtering
//!
//! Allows messages to pass through based on configurable condition expressions.
//! Supports single condition or multiple conditions with AND/OR logic.

use serde::{Deserialize, Serialize};

use crate::common::message::Message;
use crate::error::{Error, Result};
use crate::transform::json_path;
use crate::transform::step::Step;

/// Comparison operators for filter conditions
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FilterOperator {
    /// Equal (==)
    #[default]
    Eq,
    /// Not equal (!=)
    Ne,
    /// Greater than (>)
    Gt,
    /// Greater than or equal (>=)
    Ge,
    /// Less than (<)
    Lt,
    /// Less than or equal (<=)
    Le,
    /// String contains substring
    Contains,
    /// String matches regex pattern
    Matches,
    /// Absolute value greater than (|a| > |b|)
    AbsGt,
    /// Absolute value greater than or equal (|a| >= |b|)
    AbsGe,
    /// Absolute value less than (|a| < |b|)
    AbsLt,
    /// Absolute value less than or equal (|a| <= |b|)
    AbsLe,
}

/// Condition mode for combining multiple conditions
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FilterMode {
    /// All conditions must match (AND logic)
    #[default]
    And,
    /// At least one condition must match (OR logic)
    Or,
}

/// A single filter condition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterCondition {
    /// JSONPath-like field accessor (e.g., `$.status`, `$.data.value`)
    pub field: String,
    /// Comparison operator
    #[serde(default)]
    pub operator: FilterOperator,
    /// Expected value for comparison
    pub value: serde_json::Value,
}

/// Filter step configuration
///
/// Supports either a single condition or multiple conditions with AND/OR mode.
///
/// # Deserialization
///
/// Uses `#[serde(untagged)]` to allow both formats without a discriminator field.
/// serde tries variants in declaration order, so `Multi` is attempted first.
///
/// **Single condition** (simpler syntax):
/// ```yaml
/// field: "$.status"
/// operator: eq
/// value: "success"
/// ```
///
/// **Multiple conditions** (with mode):
/// ```yaml
/// mode: and  # or "or"
/// conditions:
///   - field: "$.status"
///     value: "active"
///   - field: "$.score"
///     operator: ge
///     value: 50
/// ```
///
/// # Edge Cases
///
/// Since this uses untagged deserialization, avoid using `mode` or `conditions`
/// as field names in your single-condition payload, as this may cause ambiguity.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilterStepConfig {
    /// Multiple conditions with mode (tried first during deserialization)
    Multi {
        /// Condition combining mode (and/or)
        #[serde(default)]
        mode: FilterMode,
        /// List of conditions to evaluate
        conditions: Vec<FilterCondition>,
    },
    /// Single condition (for simpler configs)
    Single(FilterCondition),
}

/// Compiled condition with pre-compiled path and optional regex
struct CompiledCondition {
    /// Pre-compiled JSONPath for efficient field access
    path: json_path::CompiledPath,
    /// Comparison operator
    operator: FilterOperator,
    /// Expected value to compare against
    expected: serde_json::Value,
    /// Pre-compiled regex (only for Matches operator)
    regex: Option<regex::Regex>,
}

impl CompiledCondition {
    fn new(condition: FilterCondition) -> Result<Self> {
        let path = json_path::CompiledPath::compile(&condition.field)?;

        let regex = if condition.operator == FilterOperator::Matches {
            let pattern = condition
                .value
                .as_str()
                .ok_or_else(|| Error::config("Matches operator requires a string pattern"))?;
            Some(
                regex::Regex::new(pattern)
                    .map_err(|e| Error::config(format!("Invalid regex pattern: {}", e)))?,
            )
        } else {
            None
        };

        Ok(Self {
            path,
            operator: condition.operator,
            expected: condition.value,
            regex,
        })
    }

    /// Evaluate this condition against a payload (no string parsing at runtime)
    fn evaluate(&self, payload: &serde_json::Value) -> bool {
        let Some(field_value) = self.path.extract(payload) else {
            return false; // Field not found = condition fails
        };

        use FilterOperator::*;
        use std::cmp::Ordering::{Equal, Greater, Less};

        match self.operator {
            Eq => field_value == &self.expected,
            Ne => field_value != &self.expected,
            Gt => compare_values(field_value, &self.expected) == Some(Greater),
            Ge => matches!(
                compare_values(field_value, &self.expected),
                Some(Greater | Equal)
            ),
            Lt => compare_values(field_value, &self.expected) == Some(Less),
            Le => matches!(
                compare_values(field_value, &self.expected),
                Some(Less | Equal)
            ),
            Contains => matches!(
                (field_value.as_str(), self.expected.as_str()),
                (Some(haystack), Some(needle)) if haystack.contains(needle)
            ),
            Matches => matches!(
                (field_value.as_str(), &self.regex),
                (Some(text), Some(re)) if re.is_match(text)
            ),
            AbsGt => compare_abs_values(field_value, &self.expected) == Some(Greater),
            AbsGe => matches!(
                compare_abs_values(field_value, &self.expected),
                Some(Greater | Equal)
            ),
            AbsLt => compare_abs_values(field_value, &self.expected) == Some(Less),
            AbsLe => matches!(
                compare_abs_values(field_value, &self.expected),
                Some(Less | Equal)
            ),
        }
    }
}

/// Compare absolute values of two numbers
fn compare_abs_values(a: &serde_json::Value, b: &serde_json::Value) -> Option<std::cmp::Ordering> {
    match (a, b) {
        (serde_json::Value::Number(a), serde_json::Value::Number(b)) => {
            let a_f64 = a.as_f64()?.abs();
            let b_f64 = b.as_f64()?.abs();
            a_f64.partial_cmp(&b_f64)
        }
        _ => None,
    }
}

/// Compare two JSON values, returns ordering for numeric comparisons
fn compare_values(a: &serde_json::Value, b: &serde_json::Value) -> Option<std::cmp::Ordering> {
    match (a, b) {
        (serde_json::Value::Number(a), serde_json::Value::Number(b)) => {
            let a_f64 = a.as_f64()?;
            let b_f64 = b.as_f64()?;
            a_f64.partial_cmp(&b_f64)
        }
        (serde_json::Value::String(a), serde_json::Value::String(b)) => Some(a.cmp(b)),
        _ => None,
    }
}

/// Filter step that conditionally passes or filters out messages
pub struct FilterStep {
    mode: FilterMode,
    conditions: Vec<CompiledCondition>,
}

impl FilterStep {
    /// Create a new filter step from configuration
    pub fn new(config: FilterStepConfig) -> Result<Self> {
        let (mode, raw_conditions) = match config {
            FilterStepConfig::Multi { mode, conditions } => (mode, conditions),
            FilterStepConfig::Single(cond) => (FilterMode::And, vec![cond]),
        };

        if raw_conditions.is_empty() {
            return Err(Error::config("Filter step requires at least one condition"));
        }

        let mut conditions = Vec::with_capacity(raw_conditions.len());
        for cond in raw_conditions {
            conditions.push(CompiledCondition::new(cond)?);
        }

        Ok(Self { mode, conditions })
    }

    /// Evaluate all conditions against a message payload
    fn evaluate(&self, payload: &serde_json::Value) -> bool {
        match self.mode {
            FilterMode::And => self.conditions.iter().all(|c| c.evaluate(payload)),
            FilterMode::Or => self.conditions.iter().any(|c| c.evaluate(payload)),
        }
    }
}

impl Step for FilterStep {
    fn step_type(&self) -> &'static str {
        "filter"
    }

    fn process(&self, msg: Message) -> Result<Option<Message>> {
        let passes = self.evaluate(&msg.payload);

        tracing::debug!(
            conditions_count = self.conditions.len(),
            mode = ?self.mode,
            result = if passes { "passed" } else { "rejected" },
            "Filter step evaluation"
        );

        Ok(if passes { Some(msg) } else { None })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // ========== Config Deserialize Tests ==========

    #[test]
    fn test_single_condition_config() {
        let yaml = r#"
field: $.status
operator: eq
value: "success"
"#;
        let config: FilterStepConfig = serde_yaml::from_str(yaml).unwrap();
        match config {
            FilterStepConfig::Single(c) => {
                assert_eq!(c.field, "$.status");
                assert_eq!(c.operator, FilterOperator::Eq);
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    fn test_multi_condition_config() {
        let yaml = r#"
mode: and
conditions:
  - field: $.status
    operator: eq
    value: "active"
  - field: $.score
    operator: ge
    value: 50
"#;
        let config: FilterStepConfig = serde_yaml::from_str(yaml).unwrap();
        match config {
            FilterStepConfig::Multi { mode, conditions } => {
                assert_eq!(mode, FilterMode::And);
                assert_eq!(conditions.len(), 2);
            }
            _ => panic!("Expected Multi variant"),
        }
    }

    #[test]
    fn test_multi_condition_or_mode() {
        let yaml = r#"
mode: or
conditions:
  - field: $.type
    value: "premium"
  - field: $.score
    operator: gt
    value: 100
"#;
        let config: FilterStepConfig = serde_yaml::from_str(yaml).unwrap();
        match config {
            FilterStepConfig::Multi { mode, .. } => {
                assert_eq!(mode, FilterMode::Or);
            }
            _ => panic!("Expected Multi variant"),
        }
    }

    // ========== FilterStep Tests ==========

    fn make_msg(payload: serde_json::Value) -> Message {
        Message::new("test", payload)
    }

    #[test]
    fn test_filter_single_eq_pass() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.status".into(),
            operator: FilterOperator::Eq,
            value: json!("success"),
        });
        let step = FilterStep::new(config).unwrap();
        let msg = make_msg(json!({"status": "success"}));

        let result = step.process(msg).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_filter_single_eq_reject() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.status".into(),
            operator: FilterOperator::Eq,
            value: json!("success"),
        });
        let step = FilterStep::new(config).unwrap();
        let msg = make_msg(json!({"status": "failure"}));

        let result = step.process(msg).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_filter_multi_and_all_pass() {
        let config = FilterStepConfig::Multi {
            mode: FilterMode::And,
            conditions: vec![
                FilterCondition {
                    field: "$.status".into(),
                    operator: FilterOperator::Eq,
                    value: json!("active"),
                },
                FilterCondition {
                    field: "$.score".into(),
                    operator: FilterOperator::Ge,
                    value: json!(50),
                },
            ],
        };
        let step = FilterStep::new(config).unwrap();

        // Both conditions pass
        let msg = make_msg(json!({"status": "active", "score": 75}));
        assert!(step.process(msg).unwrap().is_some());
    }

    #[test]
    fn test_filter_multi_and_one_fails() {
        let config = FilterStepConfig::Multi {
            mode: FilterMode::And,
            conditions: vec![
                FilterCondition {
                    field: "$.status".into(),
                    operator: FilterOperator::Eq,
                    value: json!("active"),
                },
                FilterCondition {
                    field: "$.score".into(),
                    operator: FilterOperator::Ge,
                    value: json!(50),
                },
            ],
        };
        let step = FilterStep::new(config).unwrap();

        // Second condition fails (score < 50)
        let msg = make_msg(json!({"status": "active", "score": 30}));
        assert!(step.process(msg).unwrap().is_none());
    }

    #[test]
    fn test_filter_multi_or_one_passes() {
        let config = FilterStepConfig::Multi {
            mode: FilterMode::Or,
            conditions: vec![
                FilterCondition {
                    field: "$.type".into(),
                    operator: FilterOperator::Eq,
                    value: json!("premium"),
                },
                FilterCondition {
                    field: "$.score".into(),
                    operator: FilterOperator::Gt,
                    value: json!(100),
                },
            ],
        };
        let step = FilterStep::new(config).unwrap();

        // First condition passes, second fails
        let msg = make_msg(json!({"type": "premium", "score": 50}));
        assert!(step.process(msg).unwrap().is_some());

        // First fails, second passes
        let msg = make_msg(json!({"type": "basic", "score": 150}));
        assert!(step.process(msg).unwrap().is_some());
    }

    #[test]
    fn test_filter_multi_or_all_fail() {
        let config = FilterStepConfig::Multi {
            mode: FilterMode::Or,
            conditions: vec![
                FilterCondition {
                    field: "$.type".into(),
                    operator: FilterOperator::Eq,
                    value: json!("premium"),
                },
                FilterCondition {
                    field: "$.score".into(),
                    operator: FilterOperator::Gt,
                    value: json!(100),
                },
            ],
        };
        let step = FilterStep::new(config).unwrap();

        // Both conditions fail
        let msg = make_msg(json!({"type": "basic", "score": 50}));
        assert!(step.process(msg).unwrap().is_none());
    }

    #[test]
    fn test_filter_nested_field() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.data.user.role".into(),
            operator: FilterOperator::Eq,
            value: json!("admin"),
        });
        let step = FilterStep::new(config).unwrap();

        let msg = make_msg(json!({"data": {"user": {"role": "admin"}}}));
        assert!(step.process(msg).unwrap().is_some());
    }

    #[test]
    fn test_filter_array_index() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.items[0].id".into(),
            operator: FilterOperator::Eq,
            value: json!(1),
        });
        let step = FilterStep::new(config).unwrap();

        let msg = make_msg(json!({"items": [{"id": 1}, {"id": 2}]}));
        assert!(step.process(msg).unwrap().is_some());
    }

    #[test]
    fn test_filter_contains_operator() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.message".into(),
            operator: FilterOperator::Contains,
            value: json!("error"),
        });
        let step = FilterStep::new(config).unwrap();

        let msg = make_msg(json!({"message": "An error occurred"}));
        assert!(step.process(msg).unwrap().is_some());

        let msg = make_msg(json!({"message": "All good"}));
        assert!(step.process(msg).unwrap().is_none());
    }

    #[test]
    fn test_filter_matches_operator() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.email".into(),
            operator: FilterOperator::Matches,
            value: json!(r"^[a-z]+@example\.com$"),
        });
        let step = FilterStep::new(config).unwrap();

        let msg = make_msg(json!({"email": "alice@example.com"}));
        assert!(step.process(msg).unwrap().is_some());

        let msg = make_msg(json!({"email": "alice@other.com"}));
        assert!(step.process(msg).unwrap().is_none());
    }

    #[test]
    fn test_filter_abs_gt_operator() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.change".into(),
            operator: FilterOperator::AbsGt,
            value: json!(2.0),
        });
        let step = FilterStep::new(config).unwrap();

        // 3.0 > 2.0 -> pass
        let msg = make_msg(json!({"change": 3.0}));
        assert!(step.process(msg).unwrap().is_some());

        // -5.0 (abs 5.0) > 2.0 -> pass
        let msg = make_msg(json!({"change": -5.0}));
        assert!(step.process(msg).unwrap().is_some());

        // 1.0 < 2.0 -> fail
        let msg = make_msg(json!({"change": 1.0}));
        assert!(step.process(msg).unwrap().is_none());

        // -1.0 (abs 1.0) < 2.0 -> fail
        let msg = make_msg(json!({"change": -1.0}));
        assert!(step.process(msg).unwrap().is_none());
    }

    #[test]
    fn test_filter_invalid_regex_error() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.x".into(),
            operator: FilterOperator::Matches,
            value: json!("[invalid regex"),
        });
        assert!(FilterStep::new(config).is_err());
    }

    #[test]
    fn test_filter_empty_conditions_error() {
        let config = FilterStepConfig::Multi {
            mode: FilterMode::And,
            conditions: vec![],
        };
        assert!(FilterStep::new(config).is_err());
    }

    #[test]
    fn test_filter_missing_field_fails() {
        let config = FilterStepConfig::Single(FilterCondition {
            field: "$.nonexistent".into(),
            operator: FilterOperator::Eq,
            value: json!("value"),
        });
        let step = FilterStep::new(config).unwrap();

        let msg = make_msg(json!({"other": "field"}));
        assert!(step.process(msg).unwrap().is_none());
    }
}