rocketmq-filter 0.8.0

A filter for RocketMQ messages
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;

use crate::expression::evaluation_context::EvaluationContext;
use crate::expression::Expression;

/// Trait for boolean expression evaluation in message filtering.
///
/// This trait represents boolean expressions that can be evaluated against a context
/// to produce a boolean result. It is the foundation of RocketMQ's SQL92 filter and
/// custom expression evaluators.
///
/// # Design Philosophy
///
/// - **Deterministic**: Same input always produces same output
/// - **Non-panicking**: Never panics; edge cases return `false`
/// - **Object-safe**: Can be used as `Box<dyn BooleanExpression>` or `Arc<dyn BooleanExpression>`
/// - **Thread-safe**: Can be safely shared across threads when wrapped in `Arc`
///
/// # Edge Case Handling
///
/// Following Java RocketMQ semantics:
/// - Missing variables in context → return `false`
/// - Type mismatch in comparison → return `false`
/// - Invalid operations → return `false`
/// - Null/None values → return `false`
///
/// # Thread Safety
///
/// Implementations should be thread-safe for read-only operations. The trait requires
/// `Send + Sync` to enable safe concurrent evaluation across multiple threads.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{BooleanExpression, MessageEvaluationContext};
///
/// // Custom expression implementation
/// struct PropertyEqualsExpression {
///     property_name: String,
///     expected_value: String,
/// }
///
/// impl BooleanExpression for PropertyEqualsExpression {
///     fn matches(&self, context: &dyn EvaluationContext) -> bool {
///         context
///             .get(&self.property_name)
///             .map(|v| v.as_str() == self.expected_value.as_str())
///             .unwrap_or(false)
///     }
/// }
///
/// // Usage
/// let expr = PropertyEqualsExpression {
///     property_name: "region".to_string(),
///     expected_value: "us-west".to_string(),
/// };
///
/// let mut context = MessageEvaluationContext::new();
/// context.put("region", "us-west");
///
/// assert!(expr.matches(&context)); // true
/// ```
///
/// # Compatibility
///
/// This trait is semantically equivalent to the Java interface:
///
/// ```java
/// public interface BooleanExpression extends Expression {
///     boolean matches(EvaluationContext context) throws Exception;
/// }
/// ```
///
/// Key differences from Java version:
/// - No checked exceptions; errors are handled by returning `false`
/// - Explicit `Send + Sync` bounds for thread safety
/// - Immutable `&self` receiver for better Rust ergonomics
pub trait BooleanExpression: Send + Sync + Expression {
    /// Evaluates the boolean expression against the provided context.
    ///
    /// # Arguments
    ///
    /// * `context` - The evaluation context containing message properties and variables
    ///
    /// # Returns
    ///
    /// Returns `true` if the expression evaluates to true, `false` otherwise.
    ///
    /// # Guarantees
    ///
    /// - **Non-panicking**: This method will never panic
    /// - **Deterministic**: Same context produces same result
    /// - **Idempotent**: Can be called multiple times safely
    ///
    /// # Error Handling
    ///
    /// Unlike the Java version which throws `Exception`, this method returns `false`
    /// for any error conditions:
    /// - Variable not found in context
    /// - Type conversion failures
    /// - Invalid operations (e.g., comparing incompatible types)
    /// - Any other evaluation errors
    ///
    /// # Example
    ///
    /// ```ignore
    /// let expr = SomeExpression::new();
    /// let context = MessageEvaluationContext::new();
    ///
    /// if expr.matches(&context) {
    ///     println!("Expression matched!");
    /// }
    /// ```
    fn matches(&self, context: &dyn EvaluationContext) -> bool;
}

/// Always returns `true` regardless of context.
///
/// This is a trivial implementation useful for testing, debugging,
/// or as a default "accept all" filter.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{BooleanExpression, AlwaysTrueExpression};
///
/// let expr = AlwaysTrueExpression;
/// let context = MessageEvaluationContext::new();
///
/// assert!(expr.matches(&context));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AlwaysTrueExpression;

impl BooleanExpression for AlwaysTrueExpression {
    #[inline]
    fn matches(&self, _context: &dyn EvaluationContext) -> bool {
        true
    }
}

impl Expression for AlwaysTrueExpression {
    fn evaluate(
        &self,
        _context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(true))
    }
}

impl fmt::Display for AlwaysTrueExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TRUE")
    }
}

/// Always returns `false` regardless of context.
///
/// This is a trivial implementation useful for testing, debugging,
/// or as a default "reject all" filter.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{BooleanExpression, AlwaysFalseExpression};
///
/// let expr = AlwaysFalseExpression;
/// let context = MessageEvaluationContext::new();
///
/// assert!(!expr.matches(&context));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AlwaysFalseExpression;

impl BooleanExpression for AlwaysFalseExpression {
    #[inline]
    fn matches(&self, _context: &dyn EvaluationContext) -> bool {
        false
    }
}

impl Expression for AlwaysFalseExpression {
    fn evaluate(
        &self,
        _context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(false))
    }
}

impl fmt::Display for AlwaysFalseExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FALSE")
    }
}

/// Checks if a property in the context equals a specific value.
///
/// Returns `false` if:
/// - The property does not exist in the context
/// - The property value does not match the expected value
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{PropertyEqualsExpression, MessageEvaluationContext};
///
/// let expr = PropertyEqualsExpression::new("region", "us-west");
///
/// let mut context = MessageEvaluationContext::new();
/// context.put("region", "us-west");
///
/// assert!(expr.matches(&context));
///
/// context.put("region", "eu-central");
/// assert!(!expr.matches(&context));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PropertyEqualsExpression {
    property_name: String,
    expected_value: String,
}

impl PropertyEqualsExpression {
    /// Creates a new property equals expression.
    ///
    /// # Arguments
    ///
    /// * `property_name` - The name of the property to check
    /// * `expected_value` - The expected value of the property
    ///
    /// # Returns
    ///
    /// A new `PropertyEqualsExpression` instance.
    pub fn new(property_name: impl Into<String>, expected_value: impl Into<String>) -> Self {
        Self {
            property_name: property_name.into(),
            expected_value: expected_value.into(),
        }
    }

    /// Gets the property name being checked.
    #[inline]
    pub fn property_name(&self) -> &str {
        &self.property_name
    }

    /// Gets the expected value.
    #[inline]
    pub fn expected_value(&self) -> &str {
        &self.expected_value
    }
}

impl BooleanExpression for PropertyEqualsExpression {
    fn matches(&self, context: &dyn EvaluationContext) -> bool {
        context
            .get(&self.property_name)
            .map(|v| v.as_str() == self.expected_value.as_str())
            .unwrap_or(false)
    }
}

impl Expression for PropertyEqualsExpression {
    fn evaluate(
        &self,
        context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(self.matches(context)))
    }
}

impl fmt::Display for PropertyEqualsExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({} = '{}')", self.property_name, self.expected_value)
    }
}

/// Logical AND expression that returns `true` only if both operands evaluate to `true`.
///
/// # Short-circuit Evaluation
///
/// This implementation uses short-circuit evaluation: if the left operand
/// evaluates to `false`, the right operand is not evaluated.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{AndExpression, PropertyEqualsExpression};
///
/// let left = PropertyEqualsExpression::new("region", "us-west");
/// let right = PropertyEqualsExpression::new("env", "production");
/// let expr = AndExpression::new(Box::new(left), Box::new(right));
///
/// // Only matches if both conditions are true
/// ```
pub struct AndExpression {
    left: Box<dyn BooleanExpression>,
    right: Box<dyn BooleanExpression>,
}

impl AndExpression {
    /// Creates a new AND expression.
    ///
    /// # Arguments
    ///
    /// * `left` - The left operand
    /// * `right` - The right operand
    pub fn new(left: Box<dyn BooleanExpression>, right: Box<dyn BooleanExpression>) -> Self {
        Self { left, right }
    }
}

impl BooleanExpression for AndExpression {
    fn matches(&self, context: &dyn EvaluationContext) -> bool {
        self.left.matches(context) && self.right.matches(context)
    }
}

impl Expression for AndExpression {
    fn evaluate(
        &self,
        context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(self.matches(context)))
    }
}

impl fmt::Display for AndExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({} AND {})", self.left, self.right)
    }
}

/// Logical OR expression that returns `true` if either operand evaluates to `true`.
///
/// # Short-circuit Evaluation
///
/// This implementation uses short-circuit evaluation: if the left operand
/// evaluates to `true`, the right operand is not evaluated.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{OrExpression, PropertyEqualsExpression};
///
/// let left = PropertyEqualsExpression::new("region", "us-west");
/// let right = PropertyEqualsExpression::new("region", "us-east");
/// let expr = OrExpression::new(Box::new(left), Box::new(right));
///
/// // Matches if either condition is true
/// ```
pub struct OrExpression {
    left: Box<dyn BooleanExpression>,
    right: Box<dyn BooleanExpression>,
}

impl OrExpression {
    /// Creates a new OR expression.
    ///
    /// # Arguments
    ///
    /// * `left` - The left operand
    /// * `right` - The right operand
    pub fn new(left: Box<dyn BooleanExpression>, right: Box<dyn BooleanExpression>) -> Self {
        Self { left, right }
    }
}

impl BooleanExpression for OrExpression {
    fn matches(&self, context: &dyn EvaluationContext) -> bool {
        self.left.matches(context) || self.right.matches(context)
    }
}

impl Expression for OrExpression {
    fn evaluate(
        &self,
        context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(self.matches(context)))
    }
}

impl fmt::Display for OrExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({} OR {})", self.left, self.right)
    }
}

/// Logical NOT expression that inverts the result of its operand.
///
/// # Example
///
/// ```ignore
/// use rocketmq_filter::expression::{NotExpression, PropertyEqualsExpression};
///
/// let inner = PropertyEqualsExpression::new("env", "test");
/// let expr = NotExpression::new(Box::new(inner));
///
/// // Matches if the inner expression is false
/// ```
pub struct NotExpression {
    operand: Box<dyn BooleanExpression>,
}

impl NotExpression {
    /// Creates a new NOT expression.
    ///
    /// # Arguments
    ///
    /// * `operand` - The expression to negate
    pub fn new(operand: Box<dyn BooleanExpression>) -> Self {
        Self { operand }
    }
}

impl BooleanExpression for NotExpression {
    fn matches(&self, context: &dyn EvaluationContext) -> bool {
        !self.operand.matches(context)
    }
}

impl Expression for NotExpression {
    fn evaluate(
        &self,
        context: &dyn EvaluationContext,
    ) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
        Ok(crate::expression::Value::Boolean(self.matches(context)))
    }
}

impl fmt::Display for NotExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "NOT {}", self.operand)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::expression::MessageEvaluationContext;

    #[test]
    fn test_always_true_expression() {
        let expr = AlwaysTrueExpression;
        let context = MessageEvaluationContext::new();
        assert!(expr.matches(&context));
    }

    #[test]
    fn test_always_false_expression() {
        let expr = AlwaysFalseExpression;
        let context = MessageEvaluationContext::new();
        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_property_equals_expression_match() {
        let expr = PropertyEqualsExpression::new("region", "us-west");
        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-west");

        assert!(expr.matches(&context));
    }

    #[test]
    fn test_property_equals_expression_no_match() {
        let expr = PropertyEqualsExpression::new("region", "us-west");
        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-east");

        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_property_equals_expression_missing_property() {
        let expr = PropertyEqualsExpression::new("region", "us-west");
        let context = MessageEvaluationContext::new();

        // Missing property should return false
        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_and_expression_both_true() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("env", "production");
        let expr = AndExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-west");
        context.put("env", "production");

        assert!(expr.matches(&context));
    }

    #[test]
    fn test_and_expression_left_false() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("env", "production");
        let expr = AndExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-east");
        context.put("env", "production");

        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_and_expression_right_false() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("env", "production");
        let expr = AndExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-west");
        context.put("env", "test");

        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_or_expression_both_true() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("region", "us-east");
        let expr = OrExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-west");

        assert!(expr.matches(&context));
    }

    #[test]
    fn test_or_expression_left_true() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("region", "us-east");
        let expr = OrExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-west");

        assert!(expr.matches(&context));
    }

    #[test]
    fn test_or_expression_right_true() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("region", "us-east");
        let expr = OrExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "us-east");

        assert!(expr.matches(&context));
    }

    #[test]
    fn test_or_expression_both_false() {
        let left = PropertyEqualsExpression::new("region", "us-west");
        let right = PropertyEqualsExpression::new("region", "us-east");
        let expr = OrExpression::new(Box::new(left), Box::new(right));

        let mut context = MessageEvaluationContext::new();
        context.put("region", "eu-central");

        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_not_expression_true() {
        let inner = AlwaysFalseExpression;
        let expr = NotExpression::new(Box::new(inner));

        let context = MessageEvaluationContext::new();
        assert!(expr.matches(&context));
    }

    #[test]
    fn test_not_expression_false() {
        let inner = AlwaysTrueExpression;
        let expr = NotExpression::new(Box::new(inner));

        let context = MessageEvaluationContext::new();
        assert!(!expr.matches(&context));
    }

    #[test]
    fn test_complex_expression() {
        // (region == "us-west" AND env == "production") OR (region == "us-east" AND env == "test")
        let left_and = AndExpression::new(
            Box::new(PropertyEqualsExpression::new("region", "us-west")),
            Box::new(PropertyEqualsExpression::new("env", "production")),
        );

        let right_and = AndExpression::new(
            Box::new(PropertyEqualsExpression::new("region", "us-east")),
            Box::new(PropertyEqualsExpression::new("env", "test")),
        );

        let expr = OrExpression::new(Box::new(left_and), Box::new(right_and));

        // Test case 1: us-west + production -> true
        let mut context1 = MessageEvaluationContext::new();
        context1.put("region", "us-west");
        context1.put("env", "production");
        assert!(expr.matches(&context1));

        // Test case 2: us-east + test -> true
        let mut context2 = MessageEvaluationContext::new();
        context2.put("region", "us-east");
        context2.put("env", "test");
        assert!(expr.matches(&context2));

        // Test case 3: us-west + test -> false
        let mut context3 = MessageEvaluationContext::new();
        context3.put("region", "us-west");
        context3.put("env", "test");
        assert!(!expr.matches(&context3));
    }

    #[test]
    fn test_trait_object_usage() {
        let expr: Box<dyn BooleanExpression> = Box::new(AlwaysTrueExpression);
        let context = MessageEvaluationContext::new();
        assert!(expr.matches(&context));
    }

    #[test]
    fn test_send_sync_bounds() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<AlwaysTrueExpression>();
        assert_send_sync::<AlwaysFalseExpression>();
        assert_send_sync::<PropertyEqualsExpression>();
    }
}