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 types::Int16.encode(buf, &self.error_code)?;
73 if version >= 3 {
74 types::CompactArray(types::Struct { version })
75 .encode(buf, &self.remaining_partitions)?;
76 } else {
77 types::Array(types::Struct { version }).encode(buf, &self.remaining_partitions)?;
78 }
79 if version >= 3 {
80 let num_tagged_fields = self.unknown_tagged_fields.len();
81 if num_tagged_fields > std::u32::MAX as usize {
82 bail!(
83 "Too many tagged fields to encode ({} fields)",
84 num_tagged_fields
85 );
86 }
87 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
88
89 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
90 }
91 Ok(())
92 }
93 fn compute_size(&self, version: i16) -> Result<usize> {
94 let mut total_size = 0;
95 total_size += types::Int16.compute_size(&self.error_code)?;
96 if version >= 3 {
97 total_size += types::CompactArray(types::Struct { version })
98 .compute_size(&self.remaining_partitions)?;
99 } else {
100 total_size +=
101 types::Array(types::Struct { version }).compute_size(&self.remaining_partitions)?;
102 }
103 if version >= 3 {
104 let num_tagged_fields = self.unknown_tagged_fields.len();
105 if num_tagged_fields > std::u32::MAX as usize {
106 bail!(
107 "Too many tagged fields to encode ({} fields)",
108 num_tagged_fields
109 );
110 }
111 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
112
113 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
114 }
115 Ok(total_size)
116 }
117}
118
119#[cfg(feature = "client")]
120impl Decodable for ControlledShutdownResponse {
121 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
122 let error_code = types::Int16.decode(buf)?;
123 let remaining_partitions = if version >= 3 {
124 types::CompactArray(types::Struct { version }).decode(buf)?
125 } else {
126 types::Array(types::Struct { version }).decode(buf)?
127 };
128 let mut unknown_tagged_fields = BTreeMap::new();
129 if version >= 3 {
130 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
131 for _ in 0..num_tagged_fields {
132 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
133 let size: u32 = types::UnsignedVarInt.decode(buf)?;
134 let unknown_value = buf.try_get_bytes(size as usize)?;
135 unknown_tagged_fields.insert(tag as i32, unknown_value);
136 }
137 }
138 Ok(Self {
139 error_code,
140 remaining_partitions,
141 unknown_tagged_fields,
142 })
143 }
144}
145
146impl Default for ControlledShutdownResponse {
147 fn default() -> Self {
148 Self {
149 error_code: 0,
150 remaining_partitions: Default::default(),
151 unknown_tagged_fields: BTreeMap::new(),
152 }
153 }
154}
155
156impl Message for ControlledShutdownResponse {
157 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
158 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
159}
160
161#[non_exhaustive]
163#[derive(Debug, Clone, PartialEq)]
164pub struct RemainingPartition {
165 pub topic_name: super::TopicName,
169
170 pub partition_index: i32,
174
175 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
177}
178
179impl RemainingPartition {
180 pub fn with_topic_name(mut self, value: super::TopicName) -> Self {
186 self.topic_name = value;
187 self
188 }
189 pub fn with_partition_index(mut self, value: i32) -> Self {
195 self.partition_index = value;
196 self
197 }
198 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
200 self.unknown_tagged_fields = value;
201 self
202 }
203 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
205 self.unknown_tagged_fields.insert(key, value);
206 self
207 }
208}
209
210#[cfg(feature = "broker")]
211impl Encodable for RemainingPartition {
212 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
213 if version >= 3 {
214 types::CompactString.encode(buf, &self.topic_name)?;
215 } else {
216 types::String.encode(buf, &self.topic_name)?;
217 }
218 types::Int32.encode(buf, &self.partition_index)?;
219 if version >= 3 {
220 let num_tagged_fields = self.unknown_tagged_fields.len();
221 if num_tagged_fields > std::u32::MAX as usize {
222 bail!(
223 "Too many tagged fields to encode ({} fields)",
224 num_tagged_fields
225 );
226 }
227 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
228
229 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
230 }
231 Ok(())
232 }
233 fn compute_size(&self, version: i16) -> Result<usize> {
234 let mut total_size = 0;
235 if version >= 3 {
236 total_size += types::CompactString.compute_size(&self.topic_name)?;
237 } else {
238 total_size += types::String.compute_size(&self.topic_name)?;
239 }
240 total_size += types::Int32.compute_size(&self.partition_index)?;
241 if version >= 3 {
242 let num_tagged_fields = self.unknown_tagged_fields.len();
243 if num_tagged_fields > std::u32::MAX as usize {
244 bail!(
245 "Too many tagged fields to encode ({} fields)",
246 num_tagged_fields
247 );
248 }
249 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
250
251 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
252 }
253 Ok(total_size)
254 }
255}
256
257#[cfg(feature = "client")]
258impl Decodable for RemainingPartition {
259 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
260 let topic_name = if version >= 3 {
261 types::CompactString.decode(buf)?
262 } else {
263 types::String.decode(buf)?
264 };
265 let partition_index = types::Int32.decode(buf)?;
266 let mut unknown_tagged_fields = BTreeMap::new();
267 if version >= 3 {
268 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
269 for _ in 0..num_tagged_fields {
270 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
271 let size: u32 = types::UnsignedVarInt.decode(buf)?;
272 let unknown_value = buf.try_get_bytes(size as usize)?;
273 unknown_tagged_fields.insert(tag as i32, unknown_value);
274 }
275 }
276 Ok(Self {
277 topic_name,
278 partition_index,
279 unknown_tagged_fields,
280 })
281 }
282}
283
284impl Default for RemainingPartition {
285 fn default() -> Self {
286 Self {
287 topic_name: Default::default(),
288 partition_index: 0,
289 unknown_tagged_fields: BTreeMap::new(),
290 }
291 }
292}
293
294impl Message for RemainingPartition {
295 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
296 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
297}
298
299impl HeaderVersion for ControlledShutdownResponse {
300 fn header_version(version: i16) -> i16 {
301 if version >= 3 {
302 1
303 } else {
304 0
305 }
306 }
307}