kafka_protocol/messages/
stop_replica_response.rs1#![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#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct StopReplicaPartitionError {
24 pub topic_name: super::TopicName,
28
29 pub partition_index: i32,
33
34 pub error_code: i16,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl StopReplicaPartitionError {
44 pub fn with_topic_name(mut self, value: super::TopicName) -> Self {
50 self.topic_name = value;
51 self
52 }
53 pub fn with_partition_index(mut self, value: i32) -> Self {
59 self.partition_index = value;
60 self
61 }
62 pub fn with_error_code(mut self, value: i16) -> Self {
68 self.error_code = value;
69 self
70 }
71 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73 self.unknown_tagged_fields = value;
74 self
75 }
76 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 < 0 || version > 4 {
87 bail!("specified version not supported by this message type");
88 }
89 if version >= 2 {
90 types::CompactString.encode(buf, &self.topic_name)?;
91 } else {
92 types::String.encode(buf, &self.topic_name)?;
93 }
94 types::Int32.encode(buf, &self.partition_index)?;
95 types::Int16.encode(buf, &self.error_code)?;
96 if version >= 2 {
97 let num_tagged_fields = self.unknown_tagged_fields.len();
98 if num_tagged_fields > std::u32::MAX as usize {
99 bail!(
100 "Too many tagged fields to encode ({} fields)",
101 num_tagged_fields
102 );
103 }
104 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
105
106 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
107 }
108 Ok(())
109 }
110 fn compute_size(&self, version: i16) -> Result<usize> {
111 let mut total_size = 0;
112 if version >= 2 {
113 total_size += types::CompactString.compute_size(&self.topic_name)?;
114 } else {
115 total_size += types::String.compute_size(&self.topic_name)?;
116 }
117 total_size += types::Int32.compute_size(&self.partition_index)?;
118 total_size += types::Int16.compute_size(&self.error_code)?;
119 if version >= 2 {
120 let num_tagged_fields = self.unknown_tagged_fields.len();
121 if num_tagged_fields > std::u32::MAX as usize {
122 bail!(
123 "Too many tagged fields to encode ({} fields)",
124 num_tagged_fields
125 );
126 }
127 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
128
129 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
130 }
131 Ok(total_size)
132 }
133}
134
135#[cfg(feature = "client")]
136impl Decodable for StopReplicaPartitionError {
137 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
138 if version < 0 || version > 4 {
139 bail!("specified version not supported by this message type");
140 }
141 let topic_name = if version >= 2 {
142 types::CompactString.decode(buf)?
143 } else {
144 types::String.decode(buf)?
145 };
146 let partition_index = types::Int32.decode(buf)?;
147 let error_code = types::Int16.decode(buf)?;
148 let mut unknown_tagged_fields = BTreeMap::new();
149 if version >= 2 {
150 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
151 for _ in 0..num_tagged_fields {
152 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
153 let size: u32 = types::UnsignedVarInt.decode(buf)?;
154 let unknown_value = buf.try_get_bytes(size as usize)?;
155 unknown_tagged_fields.insert(tag as i32, unknown_value);
156 }
157 }
158 Ok(Self {
159 topic_name,
160 partition_index,
161 error_code,
162 unknown_tagged_fields,
163 })
164 }
165}
166
167impl Default for StopReplicaPartitionError {
168 fn default() -> Self {
169 Self {
170 topic_name: Default::default(),
171 partition_index: 0,
172 error_code: 0,
173 unknown_tagged_fields: BTreeMap::new(),
174 }
175 }
176}
177
178impl Message for StopReplicaPartitionError {
179 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
180 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
181}
182
183#[non_exhaustive]
185#[derive(Debug, Clone, PartialEq)]
186pub struct StopReplicaResponse {
187 pub error_code: i16,
191
192 pub partition_errors: Vec<StopReplicaPartitionError>,
196
197 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
199}
200
201impl StopReplicaResponse {
202 pub fn with_error_code(mut self, value: i16) -> Self {
208 self.error_code = value;
209 self
210 }
211 pub fn with_partition_errors(mut self, value: Vec<StopReplicaPartitionError>) -> Self {
217 self.partition_errors = value;
218 self
219 }
220 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
222 self.unknown_tagged_fields = value;
223 self
224 }
225 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
227 self.unknown_tagged_fields.insert(key, value);
228 self
229 }
230}
231
232#[cfg(feature = "broker")]
233impl Encodable for StopReplicaResponse {
234 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
235 if version < 0 || version > 4 {
236 bail!("specified version not supported by this message type");
237 }
238 types::Int16.encode(buf, &self.error_code)?;
239 if version >= 2 {
240 types::CompactArray(types::Struct { version }).encode(buf, &self.partition_errors)?;
241 } else {
242 types::Array(types::Struct { version }).encode(buf, &self.partition_errors)?;
243 }
244 if version >= 2 {
245 let num_tagged_fields = self.unknown_tagged_fields.len();
246 if num_tagged_fields > std::u32::MAX as usize {
247 bail!(
248 "Too many tagged fields to encode ({} fields)",
249 num_tagged_fields
250 );
251 }
252 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
253
254 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
255 }
256 Ok(())
257 }
258 fn compute_size(&self, version: i16) -> Result<usize> {
259 let mut total_size = 0;
260 total_size += types::Int16.compute_size(&self.error_code)?;
261 if version >= 2 {
262 total_size += types::CompactArray(types::Struct { version })
263 .compute_size(&self.partition_errors)?;
264 } else {
265 total_size +=
266 types::Array(types::Struct { version }).compute_size(&self.partition_errors)?;
267 }
268 if version >= 2 {
269 let num_tagged_fields = self.unknown_tagged_fields.len();
270 if num_tagged_fields > std::u32::MAX as usize {
271 bail!(
272 "Too many tagged fields to encode ({} fields)",
273 num_tagged_fields
274 );
275 }
276 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
277
278 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
279 }
280 Ok(total_size)
281 }
282}
283
284#[cfg(feature = "client")]
285impl Decodable for StopReplicaResponse {
286 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
287 if version < 0 || version > 4 {
288 bail!("specified version not supported by this message type");
289 }
290 let error_code = types::Int16.decode(buf)?;
291 let partition_errors = if version >= 2 {
292 types::CompactArray(types::Struct { version }).decode(buf)?
293 } else {
294 types::Array(types::Struct { version }).decode(buf)?
295 };
296 let mut unknown_tagged_fields = BTreeMap::new();
297 if version >= 2 {
298 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
299 for _ in 0..num_tagged_fields {
300 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
301 let size: u32 = types::UnsignedVarInt.decode(buf)?;
302 let unknown_value = buf.try_get_bytes(size as usize)?;
303 unknown_tagged_fields.insert(tag as i32, unknown_value);
304 }
305 }
306 Ok(Self {
307 error_code,
308 partition_errors,
309 unknown_tagged_fields,
310 })
311 }
312}
313
314impl Default for StopReplicaResponse {
315 fn default() -> Self {
316 Self {
317 error_code: 0,
318 partition_errors: Default::default(),
319 unknown_tagged_fields: BTreeMap::new(),
320 }
321 }
322}
323
324impl Message for StopReplicaResponse {
325 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
326 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
327}
328
329impl HeaderVersion for StopReplicaResponse {
330 fn header_version(version: i16) -> i16 {
331 if version >= 2 {
332 1
333 } else {
334 0
335 }
336 }
337}