kafka_protocol/messages/
stop_replica_response.rs

1//! StopReplicaResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/StopReplicaResponse.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0-4
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct StopReplicaPartitionError {
24    /// The topic name.
25    ///
26    /// Supported API versions: 0-4
27    pub topic_name: super::TopicName,
28
29    /// The partition index.
30    ///
31    /// Supported API versions: 0-4
32    pub partition_index: i32,
33
34    /// The partition error code, or 0 if there was no partition error.
35    ///
36    /// Supported API versions: 0-4
37    pub error_code: i16,
38
39    /// Other tagged fields
40    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl StopReplicaPartitionError {
44    /// Sets `topic_name` to the passed value.
45    ///
46    /// The topic name.
47    ///
48    /// Supported API versions: 0-4
49    pub fn with_topic_name(mut self, value: super::TopicName) -> Self {
50        self.topic_name = value;
51        self
52    }
53    /// Sets `partition_index` to the passed value.
54    ///
55    /// The partition index.
56    ///
57    /// Supported API versions: 0-4
58    pub fn with_partition_index(mut self, value: i32) -> Self {
59        self.partition_index = value;
60        self
61    }
62    /// Sets `error_code` to the passed value.
63    ///
64    /// The partition error code, or 0 if there was no partition error.
65    ///
66    /// Supported API versions: 0-4
67    pub fn with_error_code(mut self, value: i16) -> Self {
68        self.error_code = value;
69        self
70    }
71    /// Sets unknown_tagged_fields to the passed value.
72    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73        self.unknown_tagged_fields = value;
74        self
75    }
76    /// Inserts an entry into unknown_tagged_fields.
77    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
78        self.unknown_tagged_fields.insert(key, value);
79        self
80    }
81}
82
83#[cfg(feature = "broker")]
84impl Encodable for StopReplicaPartitionError {
85    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86        if version >= 2 {
87            types::CompactString.encode(buf, &self.topic_name)?;
88        } else {
89            types::String.encode(buf, &self.topic_name)?;
90        }
91        types::Int32.encode(buf, &self.partition_index)?;
92        types::Int16.encode(buf, &self.error_code)?;
93        if version >= 2 {
94            let num_tagged_fields = self.unknown_tagged_fields.len();
95            if num_tagged_fields > std::u32::MAX as usize {
96                bail!(
97                    "Too many tagged fields to encode ({} fields)",
98                    num_tagged_fields
99                );
100            }
101            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
102
103            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
104        }
105        Ok(())
106    }
107    fn compute_size(&self, version: i16) -> Result<usize> {
108        let mut total_size = 0;
109        if version >= 2 {
110            total_size += types::CompactString.compute_size(&self.topic_name)?;
111        } else {
112            total_size += types::String.compute_size(&self.topic_name)?;
113        }
114        total_size += types::Int32.compute_size(&self.partition_index)?;
115        total_size += types::Int16.compute_size(&self.error_code)?;
116        if version >= 2 {
117            let num_tagged_fields = self.unknown_tagged_fields.len();
118            if num_tagged_fields > std::u32::MAX as usize {
119                bail!(
120                    "Too many tagged fields to encode ({} fields)",
121                    num_tagged_fields
122                );
123            }
124            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
125
126            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
127        }
128        Ok(total_size)
129    }
130}
131
132#[cfg(feature = "client")]
133impl Decodable for StopReplicaPartitionError {
134    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
135        let topic_name = if version >= 2 {
136            types::CompactString.decode(buf)?
137        } else {
138            types::String.decode(buf)?
139        };
140        let partition_index = types::Int32.decode(buf)?;
141        let error_code = types::Int16.decode(buf)?;
142        let mut unknown_tagged_fields = BTreeMap::new();
143        if version >= 2 {
144            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
145            for _ in 0..num_tagged_fields {
146                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
147                let size: u32 = types::UnsignedVarInt.decode(buf)?;
148                let unknown_value = buf.try_get_bytes(size as usize)?;
149                unknown_tagged_fields.insert(tag as i32, unknown_value);
150            }
151        }
152        Ok(Self {
153            topic_name,
154            partition_index,
155            error_code,
156            unknown_tagged_fields,
157        })
158    }
159}
160
161impl Default for StopReplicaPartitionError {
162    fn default() -> Self {
163        Self {
164            topic_name: Default::default(),
165            partition_index: 0,
166            error_code: 0,
167            unknown_tagged_fields: BTreeMap::new(),
168        }
169    }
170}
171
172impl Message for StopReplicaPartitionError {
173    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
174    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
175}
176
177/// Valid versions: 0-4
178#[non_exhaustive]
179#[derive(Debug, Clone, PartialEq)]
180pub struct StopReplicaResponse {
181    /// The top-level error code, or 0 if there was no top-level error.
182    ///
183    /// Supported API versions: 0-4
184    pub error_code: i16,
185
186    /// The responses for each partition.
187    ///
188    /// Supported API versions: 0-4
189    pub partition_errors: Vec<StopReplicaPartitionError>,
190
191    /// Other tagged fields
192    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
193}
194
195impl StopReplicaResponse {
196    /// Sets `error_code` to the passed value.
197    ///
198    /// The top-level error code, or 0 if there was no top-level error.
199    ///
200    /// Supported API versions: 0-4
201    pub fn with_error_code(mut self, value: i16) -> Self {
202        self.error_code = value;
203        self
204    }
205    /// Sets `partition_errors` to the passed value.
206    ///
207    /// The responses for each partition.
208    ///
209    /// Supported API versions: 0-4
210    pub fn with_partition_errors(mut self, value: Vec<StopReplicaPartitionError>) -> Self {
211        self.partition_errors = value;
212        self
213    }
214    /// Sets unknown_tagged_fields to the passed value.
215    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
216        self.unknown_tagged_fields = value;
217        self
218    }
219    /// Inserts an entry into unknown_tagged_fields.
220    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
221        self.unknown_tagged_fields.insert(key, value);
222        self
223    }
224}
225
226#[cfg(feature = "broker")]
227impl Encodable for StopReplicaResponse {
228    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
229        types::Int16.encode(buf, &self.error_code)?;
230        if version >= 2 {
231            types::CompactArray(types::Struct { version }).encode(buf, &self.partition_errors)?;
232        } else {
233            types::Array(types::Struct { version }).encode(buf, &self.partition_errors)?;
234        }
235        if version >= 2 {
236            let num_tagged_fields = self.unknown_tagged_fields.len();
237            if num_tagged_fields > std::u32::MAX as usize {
238                bail!(
239                    "Too many tagged fields to encode ({} fields)",
240                    num_tagged_fields
241                );
242            }
243            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
244
245            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
246        }
247        Ok(())
248    }
249    fn compute_size(&self, version: i16) -> Result<usize> {
250        let mut total_size = 0;
251        total_size += types::Int16.compute_size(&self.error_code)?;
252        if version >= 2 {
253            total_size += types::CompactArray(types::Struct { version })
254                .compute_size(&self.partition_errors)?;
255        } else {
256            total_size +=
257                types::Array(types::Struct { version }).compute_size(&self.partition_errors)?;
258        }
259        if version >= 2 {
260            let num_tagged_fields = self.unknown_tagged_fields.len();
261            if num_tagged_fields > std::u32::MAX as usize {
262                bail!(
263                    "Too many tagged fields to encode ({} fields)",
264                    num_tagged_fields
265                );
266            }
267            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
268
269            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
270        }
271        Ok(total_size)
272    }
273}
274
275#[cfg(feature = "client")]
276impl Decodable for StopReplicaResponse {
277    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
278        let error_code = types::Int16.decode(buf)?;
279        let partition_errors = if version >= 2 {
280            types::CompactArray(types::Struct { version }).decode(buf)?
281        } else {
282            types::Array(types::Struct { version }).decode(buf)?
283        };
284        let mut unknown_tagged_fields = BTreeMap::new();
285        if version >= 2 {
286            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
287            for _ in 0..num_tagged_fields {
288                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
289                let size: u32 = types::UnsignedVarInt.decode(buf)?;
290                let unknown_value = buf.try_get_bytes(size as usize)?;
291                unknown_tagged_fields.insert(tag as i32, unknown_value);
292            }
293        }
294        Ok(Self {
295            error_code,
296            partition_errors,
297            unknown_tagged_fields,
298        })
299    }
300}
301
302impl Default for StopReplicaResponse {
303    fn default() -> Self {
304        Self {
305            error_code: 0,
306            partition_errors: Default::default(),
307            unknown_tagged_fields: BTreeMap::new(),
308        }
309    }
310}
311
312impl Message for StopReplicaResponse {
313    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
314    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
315}
316
317impl HeaderVersion for StopReplicaResponse {
318    fn header_version(version: i16) -> i16 {
319        if version >= 2 {
320            1
321        } else {
322            0
323        }
324    }
325}