Skip to main content

confluent_kafka/admin/
supported_version_range.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 range of versions that a particular broker supports for a feature.
16
17use crate::common::Error;
18
19/// Represents a range of versions that a particular broker supports for some
20/// feature.
21#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22#[doc(alias = "org.apache.kafka.clients.admin.SupportedVersionRange")]
23pub struct SupportedVersionRange {
24    min_version: i16,
25    max_version: i16,
26}
27
28impl SupportedVersionRange {
29    /// Raises an error unless the following conditions are met: `0 <= min_version <=
30    /// max_version`.
31    ///
32    /// # Arguments
33    ///
34    /// - `min_version` - The minimum version value.
35    /// - `max_version` - The maximum version value.
36    ///
37    /// # Errors
38    ///
39    /// - `Error::is_local_illegal_argument_error` Raised when the condition described above is not
40    ///   met.
41    #[doc(alias = "org.apache.kafka.clients.admin.SupportedVersionRange#SupportedVersionRange")]
42    pub fn new(min_version: i16, max_version: i16) -> Result<Self, Error> {
43        if min_version < 0 || max_version < 0 || max_version < min_version {
44            return Err(Error::local_illegal_argument(format!(
45                "Expected 0 <= minVersion <= maxVersion but received minVersion:{min_version}, \
46                 maxVersion:{max_version}."
47            )));
48        }
49        Ok(Self { min_version, max_version })
50    }
51
52    /// Returns the minimum version value.
53    #[doc(alias = "org.apache.kafka.clients.admin.SupportedVersionRange#minVersion")]
54    pub fn min_version(&self) -> i16 {
55        self.min_version
56    }
57
58    /// Returns the maximum version value.
59    #[doc(alias = "org.apache.kafka.clients.admin.SupportedVersionRange#maxVersion")]
60    pub fn max_version(&self) -> i16 {
61        self.max_version
62    }
63}
64
65impl std::fmt::Display for SupportedVersionRange {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        write!(
68            f,
69            "SupportedVersionRange[min_version:{}, max_version:{}]",
70            self.min_version, self.max_version
71        )
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn new_valid_range() {
81        let range = SupportedVersionRange::new(1, 5).unwrap();
82        assert_eq!(range.min_version(), 1);
83        assert_eq!(range.max_version(), 5);
84    }
85
86    #[test]
87    fn new_rejects_negative_and_inverted_ranges() {
88        let expected = "Expected 0 <= minVersion <= maxVersion but received minVersion:-1, maxVersion:2.";
89        assert_eq!(SupportedVersionRange::new(-1, 2).unwrap_err().message(), expected);
90        assert!(SupportedVersionRange::new(5, 2).is_err());
91    }
92
93    #[test]
94    fn to_string_matches_java() {
95        let range = SupportedVersionRange::new(1, 5).unwrap();
96        assert_eq!(range.to_string(), "SupportedVersionRange[min_version:1, max_version:5]");
97    }
98}