Skip to main content

confluent_kafka/admin/
new_partition_reassignment.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 new partition reassignment target.
16
17use crate::common::Error;
18
19/// A new partition reassignment, which can be applied via
20/// `Admin::alter_partition_reassignments`.
21#[derive(Clone, Debug)]
22#[doc(alias = "org.apache.kafka.clients.admin.NewPartitionReassignment")]
23pub struct NewPartitionReassignment {
24    target_replicas: Vec<i32>,
25}
26
27impl NewPartitionReassignment {
28    /// Creates a reassignment.
29    ///
30    /// # Arguments
31    ///
32    /// - `target_replicas` - the target replicas.
33    ///
34    /// # Errors
35    ///
36    /// - `Error::is_local_illegal_argument_error` if no replicas are supplied.
37    #[doc(alias = "org.apache.kafka.clients.admin.NewPartitionReassignment#NewPartitionReassignment")]
38    pub fn new(target_replicas: Vec<i32>) -> Result<Self, Error> {
39        if target_replicas.is_empty() {
40            return Err(Error::local_illegal_argument(
41                "Cannot create a new partition reassignment without any replicas",
42            ));
43        }
44        Ok(Self { target_replicas })
45    }
46
47    /// Returns the target replicas of this reassignment.
48    #[doc(alias = "org.apache.kafka.clients.admin.NewPartitionReassignment#targetReplicas")]
49    pub fn target_replicas(&self) -> &[i32] {
50        &self.target_replicas
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn new_stores_target_replicas() {
60        let r = NewPartitionReassignment::new(vec![1, 2, 3]).unwrap();
61        assert_eq!(r.target_replicas(), &[1, 2, 3]);
62    }
63
64    #[test]
65    fn new_rejects_empty_replicas() {
66        let err = NewPartitionReassignment::new(Vec::new()).unwrap_err();
67        assert!(
68            err.message()
69                .contains("Cannot create a new partition reassignment without any replicas")
70        );
71    }
72}