kafka_protocol/messages/
controlled_shutdown_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 ControlledShutdownResponse {
24 pub error_code: i16,
28
29 pub remaining_partitions: Vec<RemainingPartition>,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl ControlledShutdownResponse {
39 pub fn with_error_code(mut self, value: i16) -> Self {
45 self.error_code = value;
46 self
47 }
48 pub fn with_remaining_partitions(mut self, value: Vec<RemainingPartition>) -> Self {
54 self.remaining_partitions = value;
55 self
56 }
57 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59 self.unknown_tagged_fields = value;
60 self
61 }
62 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
64 self.unknown_tagged_fields.insert(key, value);
65 self
66 }
67}
68
69#[cfg(feature = "broker")]
70impl Encodable for ControlledShutdownResponse {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 if version < 0 || version > 3 {
73 bail!("specified version not supported by this message type");
74 }
75 types::Int16.encode(buf, &self.error_code)?;
76 if version >= 3 {
77 types::CompactArray(types::Struct { version })
78 .encode(buf, &self.remaining_partitions)?;
79 } else {
80 types::Array(types::Struct { version }).encode(buf, &self.remaining_partitions)?;
81 }
82 if version >= 3 {
83 let num_tagged_fields = self.unknown_tagged_fields.len();
84 if num_tagged_fields > std::u32::MAX as usize {
85 bail!(
86 "Too many tagged fields to encode ({} fields)",
87 num_tagged_fields
88 );
89 }
90 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
91
92 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
93 }
94 Ok(())
95 }
96 fn compute_size(&self, version: i16) -> Result<usize> {
97 let mut total_size = 0;
98 total_size += types::Int16.compute_size(&self.error_code)?;
99 if version >= 3 {
100 total_size += types::CompactArray(types::Struct { version })
101 .compute_size(&self.remaining_partitions)?;
102 } else {
103 total_size +=
104 types::Array(types::Struct { version }).compute_size(&self.remaining_partitions)?;
105 }
106 if version >= 3 {
107 let num_tagged_fields = self.unknown_tagged_fields.len();
108 if num_tagged_fields > std::u32::MAX as usize {
109 bail!(
110 "Too many tagged fields to encode ({} fields)",
111 num_tagged_fields
112 );
113 }
114 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
115
116 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
117 }
118 Ok(total_size)
119 }
120}
121
122#[cfg(feature = "client")]
123impl Decodable for ControlledShutdownResponse {
124 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
125 if version < 0 || version > 3 {
126 bail!("specified version not supported by this message type");
127 }
128 let error_code = types::Int16.decode(buf)?;
129 let remaining_partitions = if version >= 3 {
130 types::CompactArray(types::Struct { version }).decode(buf)?
131 } else {
132 types::Array(types::Struct { version }).decode(buf)?
133 };
134 let mut unknown_tagged_fields = BTreeMap::new();
135 if version >= 3 {
136 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
137 for _ in 0..num_tagged_fields {
138 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
139 let size: u32 = types::UnsignedVarInt.decode(buf)?;
140 let unknown_value = buf.try_get_bytes(size as usize)?;
141 unknown_tagged_fields.insert(tag as i32, unknown_value);
142 }
143 }
144 Ok(Self {
145 error_code,
146 remaining_partitions,
147 unknown_tagged_fields,
148 })
149 }
150}
151
152impl Default for ControlledShutdownResponse {
153 fn default() -> Self {
154 Self {
155 error_code: 0,
156 remaining_partitions: Default::default(),
157 unknown_tagged_fields: BTreeMap::new(),
158 }
159 }
160}
161
162impl Message for ControlledShutdownResponse {
163 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
164 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
165}
166
167#[non_exhaustive]
169#[derive(Debug, Clone, PartialEq)]
170pub struct RemainingPartition {
171 pub topic_name: super::TopicName,
175
176 pub partition_index: i32,
180
181 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
183}
184
185impl RemainingPartition {
186 pub fn with_topic_name(mut self, value: super::TopicName) -> Self {
192 self.topic_name = value;
193 self
194 }
195 pub fn with_partition_index(mut self, value: i32) -> Self {
201 self.partition_index = value;
202 self
203 }
204 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
206 self.unknown_tagged_fields = value;
207 self
208 }
209 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
211 self.unknown_tagged_fields.insert(key, value);
212 self
213 }
214}
215
216#[cfg(feature = "broker")]
217impl Encodable for RemainingPartition {
218 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
219 if version < 0 || version > 3 {
220 bail!("specified version not supported by this message type");
221 }
222 if version >= 3 {
223 types::CompactString.encode(buf, &self.topic_name)?;
224 } else {
225 types::String.encode(buf, &self.topic_name)?;
226 }
227 types::Int32.encode(buf, &self.partition_index)?;
228 if version >= 3 {
229 let num_tagged_fields = self.unknown_tagged_fields.len();
230 if num_tagged_fields > std::u32::MAX as usize {
231 bail!(
232 "Too many tagged fields to encode ({} fields)",
233 num_tagged_fields
234 );
235 }
236 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
237
238 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
239 }
240 Ok(())
241 }
242 fn compute_size(&self, version: i16) -> Result<usize> {
243 let mut total_size = 0;
244 if version >= 3 {
245 total_size += types::CompactString.compute_size(&self.topic_name)?;
246 } else {
247 total_size += types::String.compute_size(&self.topic_name)?;
248 }
249 total_size += types::Int32.compute_size(&self.partition_index)?;
250 if version >= 3 {
251 let num_tagged_fields = self.unknown_tagged_fields.len();
252 if num_tagged_fields > std::u32::MAX as usize {
253 bail!(
254 "Too many tagged fields to encode ({} fields)",
255 num_tagged_fields
256 );
257 }
258 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
259
260 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
261 }
262 Ok(total_size)
263 }
264}
265
266#[cfg(feature = "client")]
267impl Decodable for RemainingPartition {
268 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
269 if version < 0 || version > 3 {
270 bail!("specified version not supported by this message type");
271 }
272 let topic_name = if version >= 3 {
273 types::CompactString.decode(buf)?
274 } else {
275 types::String.decode(buf)?
276 };
277 let partition_index = types::Int32.decode(buf)?;
278 let mut unknown_tagged_fields = BTreeMap::new();
279 if version >= 3 {
280 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
281 for _ in 0..num_tagged_fields {
282 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
283 let size: u32 = types::UnsignedVarInt.decode(buf)?;
284 let unknown_value = buf.try_get_bytes(size as usize)?;
285 unknown_tagged_fields.insert(tag as i32, unknown_value);
286 }
287 }
288 Ok(Self {
289 topic_name,
290 partition_index,
291 unknown_tagged_fields,
292 })
293 }
294}
295
296impl Default for RemainingPartition {
297 fn default() -> Self {
298 Self {
299 topic_name: Default::default(),
300 partition_index: 0,
301 unknown_tagged_fields: BTreeMap::new(),
302 }
303 }
304}
305
306impl Message for RemainingPartition {
307 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
308 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
309}
310
311impl HeaderVersion for ControlledShutdownResponse {
312 fn header_version(version: i16) -> i16 {
313 if version >= 3 {
314 1
315 } else {
316 0
317 }
318 }
319}