1#![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 PartitionResult {
24 pub partition: i32,
28
29 pub error_code: i16,
33
34 pub error_message: Option<StrBytes>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl PartitionResult {
44 pub fn with_partition(mut self, value: i32) -> Self {
50 self.partition = value;
51 self
52 }
53 pub fn with_error_code(mut self, value: i16) -> Self {
59 self.error_code = value;
60 self
61 }
62 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
68 self.error_message = 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 PartitionResult {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version != 0 {
87 bail!("specified version not supported by this message type");
88 }
89 types::Int32.encode(buf, &self.partition)?;
90 types::Int16.encode(buf, &self.error_code)?;
91 types::CompactString.encode(buf, &self.error_message)?;
92 let num_tagged_fields = self.unknown_tagged_fields.len();
93 if num_tagged_fields > std::u32::MAX as usize {
94 bail!(
95 "Too many tagged fields to encode ({} fields)",
96 num_tagged_fields
97 );
98 }
99 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
100
101 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
102 Ok(())
103 }
104 fn compute_size(&self, version: i16) -> Result<usize> {
105 let mut total_size = 0;
106 total_size += types::Int32.compute_size(&self.partition)?;
107 total_size += types::Int16.compute_size(&self.error_code)?;
108 total_size += types::CompactString.compute_size(&self.error_message)?;
109 let num_tagged_fields = self.unknown_tagged_fields.len();
110 if num_tagged_fields > std::u32::MAX as usize {
111 bail!(
112 "Too many tagged fields to encode ({} fields)",
113 num_tagged_fields
114 );
115 }
116 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
117
118 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
119 Ok(total_size)
120 }
121}
122
123#[cfg(feature = "client")]
124impl Decodable for PartitionResult {
125 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
126 if version != 0 {
127 bail!("specified version not supported by this message type");
128 }
129 let partition = types::Int32.decode(buf)?;
130 let error_code = types::Int16.decode(buf)?;
131 let error_message = types::CompactString.decode(buf)?;
132 let mut unknown_tagged_fields = BTreeMap::new();
133 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
134 for _ in 0..num_tagged_fields {
135 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
136 let size: u32 = types::UnsignedVarInt.decode(buf)?;
137 let unknown_value = buf.try_get_bytes(size as usize)?;
138 unknown_tagged_fields.insert(tag as i32, unknown_value);
139 }
140 Ok(Self {
141 partition,
142 error_code,
143 error_message,
144 unknown_tagged_fields,
145 })
146 }
147}
148
149impl Default for PartitionResult {
150 fn default() -> Self {
151 Self {
152 partition: 0,
153 error_code: 0,
154 error_message: None,
155 unknown_tagged_fields: BTreeMap::new(),
156 }
157 }
158}
159
160impl Message for PartitionResult {
161 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
162 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
163}
164
165#[non_exhaustive]
167#[derive(Debug, Clone, PartialEq)]
168pub struct WriteShareGroupStateResponse {
169 pub results: Vec<WriteStateResult>,
173
174 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
176}
177
178impl WriteShareGroupStateResponse {
179 pub fn with_results(mut self, value: Vec<WriteStateResult>) -> Self {
185 self.results = value;
186 self
187 }
188 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
190 self.unknown_tagged_fields = value;
191 self
192 }
193 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
195 self.unknown_tagged_fields.insert(key, value);
196 self
197 }
198}
199
200#[cfg(feature = "broker")]
201impl Encodable for WriteShareGroupStateResponse {
202 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
203 if version != 0 {
204 bail!("specified version not supported by this message type");
205 }
206 types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
207 let num_tagged_fields = self.unknown_tagged_fields.len();
208 if num_tagged_fields > std::u32::MAX as usize {
209 bail!(
210 "Too many tagged fields to encode ({} fields)",
211 num_tagged_fields
212 );
213 }
214 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
215
216 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
217 Ok(())
218 }
219 fn compute_size(&self, version: i16) -> Result<usize> {
220 let mut total_size = 0;
221 total_size += types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
222 let num_tagged_fields = self.unknown_tagged_fields.len();
223 if num_tagged_fields > std::u32::MAX as usize {
224 bail!(
225 "Too many tagged fields to encode ({} fields)",
226 num_tagged_fields
227 );
228 }
229 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
230
231 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
232 Ok(total_size)
233 }
234}
235
236#[cfg(feature = "client")]
237impl Decodable for WriteShareGroupStateResponse {
238 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
239 if version != 0 {
240 bail!("specified version not supported by this message type");
241 }
242 let results = types::CompactArray(types::Struct { version }).decode(buf)?;
243 let mut unknown_tagged_fields = BTreeMap::new();
244 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
245 for _ in 0..num_tagged_fields {
246 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
247 let size: u32 = types::UnsignedVarInt.decode(buf)?;
248 let unknown_value = buf.try_get_bytes(size as usize)?;
249 unknown_tagged_fields.insert(tag as i32, unknown_value);
250 }
251 Ok(Self {
252 results,
253 unknown_tagged_fields,
254 })
255 }
256}
257
258impl Default for WriteShareGroupStateResponse {
259 fn default() -> Self {
260 Self {
261 results: Default::default(),
262 unknown_tagged_fields: BTreeMap::new(),
263 }
264 }
265}
266
267impl Message for WriteShareGroupStateResponse {
268 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
269 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
270}
271
272#[non_exhaustive]
274#[derive(Debug, Clone, PartialEq)]
275pub struct WriteStateResult {
276 pub topic_id: Uuid,
280
281 pub partitions: Vec<PartitionResult>,
285
286 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
288}
289
290impl WriteStateResult {
291 pub fn with_topic_id(mut self, value: Uuid) -> Self {
297 self.topic_id = value;
298 self
299 }
300 pub fn with_partitions(mut self, value: Vec<PartitionResult>) -> Self {
306 self.partitions = value;
307 self
308 }
309 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
311 self.unknown_tagged_fields = value;
312 self
313 }
314 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
316 self.unknown_tagged_fields.insert(key, value);
317 self
318 }
319}
320
321#[cfg(feature = "broker")]
322impl Encodable for WriteStateResult {
323 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
324 if version != 0 {
325 bail!("specified version not supported by this message type");
326 }
327 types::Uuid.encode(buf, &self.topic_id)?;
328 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
329 let num_tagged_fields = self.unknown_tagged_fields.len();
330 if num_tagged_fields > std::u32::MAX as usize {
331 bail!(
332 "Too many tagged fields to encode ({} fields)",
333 num_tagged_fields
334 );
335 }
336 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
337
338 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
339 Ok(())
340 }
341 fn compute_size(&self, version: i16) -> Result<usize> {
342 let mut total_size = 0;
343 total_size += types::Uuid.compute_size(&self.topic_id)?;
344 total_size +=
345 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
346 let num_tagged_fields = self.unknown_tagged_fields.len();
347 if num_tagged_fields > std::u32::MAX as usize {
348 bail!(
349 "Too many tagged fields to encode ({} fields)",
350 num_tagged_fields
351 );
352 }
353 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
354
355 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
356 Ok(total_size)
357 }
358}
359
360#[cfg(feature = "client")]
361impl Decodable for WriteStateResult {
362 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
363 if version != 0 {
364 bail!("specified version not supported by this message type");
365 }
366 let topic_id = types::Uuid.decode(buf)?;
367 let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
368 let mut unknown_tagged_fields = BTreeMap::new();
369 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
370 for _ in 0..num_tagged_fields {
371 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
372 let size: u32 = types::UnsignedVarInt.decode(buf)?;
373 let unknown_value = buf.try_get_bytes(size as usize)?;
374 unknown_tagged_fields.insert(tag as i32, unknown_value);
375 }
376 Ok(Self {
377 topic_id,
378 partitions,
379 unknown_tagged_fields,
380 })
381 }
382}
383
384impl Default for WriteStateResult {
385 fn default() -> Self {
386 Self {
387 topic_id: Uuid::nil(),
388 partitions: Default::default(),
389 unknown_tagged_fields: BTreeMap::new(),
390 }
391 }
392}
393
394impl Message for WriteStateResult {
395 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
396 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
397}
398
399impl HeaderVersion for WriteShareGroupStateResponse {
400 fn header_version(version: i16) -> i16 {
401 1
402 }
403}