Skip to main content

confluent_kafka/common/
cluster_resource.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 `ClusterResource` class encapsulates metadata for a Kafka cluster.
16
17use std::fmt;
18
19/// The `ClusterResource` class encapsulates metadata for a Kafka cluster.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.common.ClusterResource")]
22pub struct ClusterResource {
23    cluster_id: Option<String>,
24}
25
26impl ClusterResource {
27    /// Create a `ClusterResource` with a cluster id. Note that cluster id may be `None` if the
28    /// metadata request was sent to a broker without support for cluster ids.
29    ///
30    /// # Arguments
31    ///
32    /// - `cluster_id` - The cluster id
33    #[doc(alias = "org.apache.kafka.common.ClusterResource#ClusterResource")]
34    pub fn new(cluster_id: Option<String>) -> Self {
35        Self { cluster_id }
36    }
37
38    /// Returns the cluster id. Note that it may be `None` if the metadata request was sent to a
39    /// broker without support for cluster ids.
40    #[doc(alias = "org.apache.kafka.common.ClusterResource#clusterId")]
41    pub fn cluster_id(&self) -> Option<&str> {
42        self.cluster_id.as_deref()
43    }
44}
45
46impl fmt::Display for ClusterResource {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "ClusterResource(clusterId={:?})", self.cluster_id)
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_cluster_resource() {
58        let cr = ClusterResource::new(Some("test-cluster".to_string()));
59        assert_eq!(cr.cluster_id(), Some("test-cluster"));
60    }
61
62    #[test]
63    fn test_cluster_resource_none() {
64        let cr = ClusterResource::new(None);
65        assert!(cr.cluster_id().is_none());
66    }
67
68    #[test]
69    fn test_cluster_resource_equality() {
70        let cr1 = ClusterResource::new(Some("id".to_string()));
71        let cr2 = ClusterResource::new(Some("id".to_string()));
72        let cr3 = ClusterResource::new(None);
73        assert_eq!(cr1, cr2);
74        assert_ne!(cr1, cr3);
75    }
76}