Skip to main content

confluent_kafka/admin/
member_assignment.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 description of the assignments of a specific group member.
16
17use std::collections::HashSet;
18
19use crate::common::TopicPartition;
20
21/// A description of the assignments of a specific group member.
22#[derive(Clone, Debug, Default, PartialEq, Eq)]
23#[doc(alias = "org.apache.kafka.clients.admin.MemberAssignment")]
24pub struct MemberAssignment {
25    topic_partitions: HashSet<TopicPartition>,
26}
27
28impl MemberAssignment {
29    /// Creates an instance with the specified topic partitions.
30    ///
31    /// # Arguments
32    ///
33    /// - `topic_partitions` - List of topic partitions
34    #[doc(alias = "org.apache.kafka.clients.admin.MemberAssignment#MemberAssignment")]
35    pub fn new(topic_partitions: HashSet<TopicPartition>) -> Self {
36        Self { topic_partitions }
37    }
38
39    /// Returns the topic partitions assigned to a group member.
40    #[doc(alias = "org.apache.kafka.clients.admin.MemberAssignment#topicPartitions")]
41    pub fn topic_partitions(&self) -> &HashSet<TopicPartition> {
42        &self.topic_partitions
43    }
44}
45
46impl std::hash::Hash for MemberAssignment {
47    // Java's `MemberAssignment.hashCode()` hashes `topicPartitions`, a set,
48    // independently of its order.
49    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
50        crate::common::utils::Utils::hash_unordered(&self.topic_partitions, state);
51    }
52}
53
54impl std::fmt::Display for MemberAssignment {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        let mut parts: Vec<String> = self.topic_partitions.iter().map(|tp| tp.to_string()).collect();
57        parts.sort();
58        write!(f, "(topicPartitions={})", parts.join(","))
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn equality_is_set_based() {
68        let a = MemberAssignment::new(HashSet::from([TopicPartition::new("t", 0), TopicPartition::new("t", 1)]));
69        let b = MemberAssignment::new(HashSet::from([TopicPartition::new("t", 1), TopicPartition::new("t", 0)]));
70        assert_eq!(a, b);
71    }
72
73    #[test]
74    fn empty_default() {
75        assert!(MemberAssignment::default().topic_partitions().is_empty());
76    }
77}