Skip to main content

confluent_kafka/common/
topic_partition_replica.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//! The topic name, partition number and the brokerId of a replica.
16
17use std::fmt;
18
19/// The topic name, partition number and the brokerId of the replica.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.common.TopicPartitionReplica")]
22pub struct TopicPartitionReplica {
23    broker_id: i32,
24    partition: i32,
25    topic: String,
26}
27
28impl TopicPartitionReplica {
29    /// Creates a new `TopicPartitionReplica`.
30    ///
31    /// # Arguments
32    ///
33    /// - `topic` - the topic name
34    /// - `partition` - the partition number
35    /// - `broker_id` - the id of the broker holding the replica
36    #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#TopicPartitionReplica")]
37    pub fn new(topic: impl Into<String>, partition: i32, broker_id: i32) -> Self {
38        Self { broker_id, partition, topic: topic.into() }
39    }
40
41    /// Returns the topic name.
42    #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#topic")]
43    pub fn topic(&self) -> &str {
44        &self.topic
45    }
46
47    /// Returns the partition number.
48    #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#partition")]
49    pub fn partition(&self) -> i32 {
50        self.partition
51    }
52
53    /// Returns the broker id.
54    #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#brokerId")]
55    pub fn broker_id(&self) -> i32 {
56        self.broker_id
57    }
58}
59
60impl fmt::Display for TopicPartitionReplica {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(f, "{}-{}-{}", self.topic, self.partition, self.broker_id)
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use std::collections::HashSet;
70
71    #[test]
72    fn accessors() {
73        let tpr = TopicPartitionReplica::new("topic", 12, 1);
74        assert_eq!(tpr.topic(), "topic");
75        assert_eq!(tpr.partition(), 12);
76        assert_eq!(tpr.broker_id(), 1);
77    }
78
79    #[test]
80    fn equality_and_hash() {
81        let a = TopicPartitionReplica::new("topic", 12, 1);
82        let b = TopicPartitionReplica::new("topic", 12, 1);
83        let c = TopicPartitionReplica::new("topic", 12, 2);
84        let d = TopicPartitionReplica::new("other", 12, 1);
85        assert_eq!(a, b);
86        assert_ne!(a, c);
87        assert_ne!(a, d);
88
89        let mut set = HashSet::new();
90        set.insert(a.clone());
91        set.insert(b);
92        assert_eq!(set.len(), 1);
93        set.insert(c);
94        assert_eq!(set.len(), 2);
95    }
96
97    /// Mirrors Java's `TopicPartitionReplica.toString` (`"%s-%d-%d"`).
98    #[test]
99    fn display_format() {
100        let tpr = TopicPartitionReplica::new("topic", 12, 3);
101        assert_eq!(tpr.to_string(), "topic-12-3");
102    }
103}