kafka_protocol/messages/
create_partitions_response.rs

1//! CreatePartitionsResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/CreatePartitionsResponse.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-3
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct CreatePartitionsResponse {
24    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
25    ///
26    /// Supported API versions: 0-3
27    pub throttle_time_ms: i32,
28
29    /// The partition creation results for each topic.
30    ///
31    /// Supported API versions: 0-3
32    pub results: Vec<CreatePartitionsTopicResult>,
33
34    /// Other tagged fields
35    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl CreatePartitionsResponse {
39    /// Sets `throttle_time_ms` to the passed value.
40    ///
41    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
42    ///
43    /// Supported API versions: 0-3
44    pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
45        self.throttle_time_ms = value;
46        self
47    }
48    /// Sets `results` to the passed value.
49    ///
50    /// The partition creation results for each topic.
51    ///
52    /// Supported API versions: 0-3
53    pub fn with_results(mut self, value: Vec<CreatePartitionsTopicResult>) -> Self {
54        self.results = value;
55        self
56    }
57    /// Sets unknown_tagged_fields to the passed value.
58    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59        self.unknown_tagged_fields = value;
60        self
61    }
62    /// Inserts an entry into unknown_tagged_fields.
63    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 CreatePartitionsResponse {
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::Int32.encode(buf, &self.throttle_time_ms)?;
76        if version >= 2 {
77            types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
78        } else {
79            types::Array(types::Struct { version }).encode(buf, &self.results)?;
80        }
81        if version >= 2 {
82            let num_tagged_fields = self.unknown_tagged_fields.len();
83            if num_tagged_fields > std::u32::MAX as usize {
84                bail!(
85                    "Too many tagged fields to encode ({} fields)",
86                    num_tagged_fields
87                );
88            }
89            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
90
91            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
92        }
93        Ok(())
94    }
95    fn compute_size(&self, version: i16) -> Result<usize> {
96        let mut total_size = 0;
97        total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
98        if version >= 2 {
99            total_size +=
100                types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
101        } else {
102            total_size += types::Array(types::Struct { version }).compute_size(&self.results)?;
103        }
104        if version >= 2 {
105            let num_tagged_fields = self.unknown_tagged_fields.len();
106            if num_tagged_fields > std::u32::MAX as usize {
107                bail!(
108                    "Too many tagged fields to encode ({} fields)",
109                    num_tagged_fields
110                );
111            }
112            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
113
114            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
115        }
116        Ok(total_size)
117    }
118}
119
120#[cfg(feature = "client")]
121impl Decodable for CreatePartitionsResponse {
122    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
123        if version < 0 || version > 3 {
124            bail!("specified version not supported by this message type");
125        }
126        let throttle_time_ms = types::Int32.decode(buf)?;
127        let results = if version >= 2 {
128            types::CompactArray(types::Struct { version }).decode(buf)?
129        } else {
130            types::Array(types::Struct { version }).decode(buf)?
131        };
132        let mut unknown_tagged_fields = BTreeMap::new();
133        if version >= 2 {
134            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
135            for _ in 0..num_tagged_fields {
136                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
137                let size: u32 = types::UnsignedVarInt.decode(buf)?;
138                let unknown_value = buf.try_get_bytes(size as usize)?;
139                unknown_tagged_fields.insert(tag as i32, unknown_value);
140            }
141        }
142        Ok(Self {
143            throttle_time_ms,
144            results,
145            unknown_tagged_fields,
146        })
147    }
148}
149
150impl Default for CreatePartitionsResponse {
151    fn default() -> Self {
152        Self {
153            throttle_time_ms: 0,
154            results: Default::default(),
155            unknown_tagged_fields: BTreeMap::new(),
156        }
157    }
158}
159
160impl Message for CreatePartitionsResponse {
161    const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
162    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
163}
164
165/// Valid versions: 0-3
166#[non_exhaustive]
167#[derive(Debug, Clone, PartialEq)]
168pub struct CreatePartitionsTopicResult {
169    /// The topic name.
170    ///
171    /// Supported API versions: 0-3
172    pub name: super::TopicName,
173
174    /// The result error, or zero if there was no error.
175    ///
176    /// Supported API versions: 0-3
177    pub error_code: i16,
178
179    /// The result message, or null if there was no error.
180    ///
181    /// Supported API versions: 0-3
182    pub error_message: Option<StrBytes>,
183
184    /// Other tagged fields
185    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
186}
187
188impl CreatePartitionsTopicResult {
189    /// Sets `name` to the passed value.
190    ///
191    /// The topic name.
192    ///
193    /// Supported API versions: 0-3
194    pub fn with_name(mut self, value: super::TopicName) -> Self {
195        self.name = value;
196        self
197    }
198    /// Sets `error_code` to the passed value.
199    ///
200    /// The result error, or zero if there was no error.
201    ///
202    /// Supported API versions: 0-3
203    pub fn with_error_code(mut self, value: i16) -> Self {
204        self.error_code = value;
205        self
206    }
207    /// Sets `error_message` to the passed value.
208    ///
209    /// The result message, or null if there was no error.
210    ///
211    /// Supported API versions: 0-3
212    pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
213        self.error_message = value;
214        self
215    }
216    /// Sets unknown_tagged_fields to the passed value.
217    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
218        self.unknown_tagged_fields = value;
219        self
220    }
221    /// Inserts an entry into unknown_tagged_fields.
222    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
223        self.unknown_tagged_fields.insert(key, value);
224        self
225    }
226}
227
228#[cfg(feature = "broker")]
229impl Encodable for CreatePartitionsTopicResult {
230    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
231        if version < 0 || version > 3 {
232            bail!("specified version not supported by this message type");
233        }
234        if version >= 2 {
235            types::CompactString.encode(buf, &self.name)?;
236        } else {
237            types::String.encode(buf, &self.name)?;
238        }
239        types::Int16.encode(buf, &self.error_code)?;
240        if version >= 2 {
241            types::CompactString.encode(buf, &self.error_message)?;
242        } else {
243            types::String.encode(buf, &self.error_message)?;
244        }
245        if version >= 2 {
246            let num_tagged_fields = self.unknown_tagged_fields.len();
247            if num_tagged_fields > std::u32::MAX as usize {
248                bail!(
249                    "Too many tagged fields to encode ({} fields)",
250                    num_tagged_fields
251                );
252            }
253            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
254
255            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
256        }
257        Ok(())
258    }
259    fn compute_size(&self, version: i16) -> Result<usize> {
260        let mut total_size = 0;
261        if version >= 2 {
262            total_size += types::CompactString.compute_size(&self.name)?;
263        } else {
264            total_size += types::String.compute_size(&self.name)?;
265        }
266        total_size += types::Int16.compute_size(&self.error_code)?;
267        if version >= 2 {
268            total_size += types::CompactString.compute_size(&self.error_message)?;
269        } else {
270            total_size += types::String.compute_size(&self.error_message)?;
271        }
272        if version >= 2 {
273            let num_tagged_fields = self.unknown_tagged_fields.len();
274            if num_tagged_fields > std::u32::MAX as usize {
275                bail!(
276                    "Too many tagged fields to encode ({} fields)",
277                    num_tagged_fields
278                );
279            }
280            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
281
282            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
283        }
284        Ok(total_size)
285    }
286}
287
288#[cfg(feature = "client")]
289impl Decodable for CreatePartitionsTopicResult {
290    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
291        if version < 0 || version > 3 {
292            bail!("specified version not supported by this message type");
293        }
294        let name = if version >= 2 {
295            types::CompactString.decode(buf)?
296        } else {
297            types::String.decode(buf)?
298        };
299        let error_code = types::Int16.decode(buf)?;
300        let error_message = if version >= 2 {
301            types::CompactString.decode(buf)?
302        } else {
303            types::String.decode(buf)?
304        };
305        let mut unknown_tagged_fields = BTreeMap::new();
306        if version >= 2 {
307            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
308            for _ in 0..num_tagged_fields {
309                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
310                let size: u32 = types::UnsignedVarInt.decode(buf)?;
311                let unknown_value = buf.try_get_bytes(size as usize)?;
312                unknown_tagged_fields.insert(tag as i32, unknown_value);
313            }
314        }
315        Ok(Self {
316            name,
317            error_code,
318            error_message,
319            unknown_tagged_fields,
320        })
321    }
322}
323
324impl Default for CreatePartitionsTopicResult {
325    fn default() -> Self {
326        Self {
327            name: Default::default(),
328            error_code: 0,
329            error_message: None,
330            unknown_tagged_fields: BTreeMap::new(),
331        }
332    }
333}
334
335impl Message for CreatePartitionsTopicResult {
336    const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
337    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
338}
339
340impl HeaderVersion for CreatePartitionsResponse {
341    fn header_version(version: i16) -> i16 {
342        if version >= 2 {
343            1
344        } else {
345            0
346        }
347    }
348}