Skip to main content

confluent_kafka/consumer/
subscription_pattern.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A regular expression used to subscribe to topics.
16//!
17//! Translated from `org.apache.kafka.clients.consumer.SubscriptionPattern`.
18
19use std::fmt;
20
21/// Represents a regular expression compatible with Google RE2/J, used to
22/// subscribe to topics. This just keeps the [`String`] representation of the
23/// pattern, and all validations to ensure it is RE2/J compatible are
24/// delegated to the broker.
25#[derive(Clone, Debug)]
26#[doc(alias = "org.apache.kafka.clients.consumer.SubscriptionPattern")]
27pub struct SubscriptionPattern {
28    pattern: String,
29}
30
31impl SubscriptionPattern {
32    /// Constructs a new subscription pattern.
33    ///
34    /// # Arguments
35    ///
36    /// - `pattern` - The regular expression pattern compatible with RE2/J
37    ///
38    /// # Returns
39    ///
40    /// The new `SubscriptionPattern`.
41    #[doc(alias = "org.apache.kafka.clients.consumer.SubscriptionPattern#SubscriptionPattern")]
42    pub fn new(pattern: impl Into<String>) -> Self {
43        Self { pattern: pattern.into() }
44    }
45
46    /// String representation the regular expression, compatible with RE2/J.
47    ///
48    /// # Returns
49    ///
50    /// Regular expression pattern compatible with RE2/J.
51    #[doc(alias = "org.apache.kafka.clients.consumer.SubscriptionPattern#pattern")]
52    pub fn pattern(&self) -> &str {
53        &self.pattern
54    }
55}
56
57impl fmt::Display for SubscriptionPattern {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.write_str(&self.pattern)
60    }
61}
62
63impl PartialEq for SubscriptionPattern {
64    fn eq(&self, other: &Self) -> bool {
65        self.pattern == other.pattern
66    }
67}
68
69impl Eq for SubscriptionPattern {}
70
71impl std::hash::Hash for SubscriptionPattern {
72    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
73        self.pattern.hash(state);
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_pattern_accessor() {
83        let p = SubscriptionPattern::new("foo.*");
84        assert_eq!(p.pattern(), "foo.*");
85    }
86
87    #[test]
88    fn test_display_is_pattern() {
89        let p = SubscriptionPattern::new("foo.*");
90        assert_eq!(p.to_string(), "foo.*");
91    }
92
93    #[test]
94    fn test_equality_and_hash() {
95        use std::collections::HashSet;
96        let p1 = SubscriptionPattern::new("a.*");
97        let p2 = SubscriptionPattern::new("a.*");
98        let p3 = SubscriptionPattern::new("b.*");
99        assert_eq!(p1, p2);
100        assert_ne!(p1, p3);
101
102        let mut set = HashSet::new();
103        set.insert(p1);
104        set.insert(p2);
105        assert_eq!(set.len(), 1);
106        set.insert(p3);
107        assert_eq!(set.len(), 2);
108    }
109}