kafka_protocol/messages/
init_producer_id_response.rs

1//! InitProducerIdResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/InitProducerIdResponse.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-5
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct InitProducerIdResponse {
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-5
27    pub throttle_time_ms: i32,
28
29    /// The error code, or 0 if there was no error.
30    ///
31    /// Supported API versions: 0-5
32    pub error_code: i16,
33
34    /// The current producer id.
35    ///
36    /// Supported API versions: 0-5
37    pub producer_id: super::ProducerId,
38
39    /// The current epoch associated with the producer id.
40    ///
41    /// Supported API versions: 0-5
42    pub producer_epoch: i16,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl InitProducerIdResponse {
49    /// Sets `throttle_time_ms` to the passed value.
50    ///
51    /// 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.
52    ///
53    /// Supported API versions: 0-5
54    pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
55        self.throttle_time_ms = value;
56        self
57    }
58    /// Sets `error_code` to the passed value.
59    ///
60    /// The error code, or 0 if there was no error.
61    ///
62    /// Supported API versions: 0-5
63    pub fn with_error_code(mut self, value: i16) -> Self {
64        self.error_code = value;
65        self
66    }
67    /// Sets `producer_id` to the passed value.
68    ///
69    /// The current producer id.
70    ///
71    /// Supported API versions: 0-5
72    pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
73        self.producer_id = value;
74        self
75    }
76    /// Sets `producer_epoch` to the passed value.
77    ///
78    /// The current epoch associated with the producer id.
79    ///
80    /// Supported API versions: 0-5
81    pub fn with_producer_epoch(mut self, value: i16) -> Self {
82        self.producer_epoch = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for InitProducerIdResponse {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        if version < 0 || version > 5 {
101            bail!("specified version not supported by this message type");
102        }
103        types::Int32.encode(buf, &self.throttle_time_ms)?;
104        types::Int16.encode(buf, &self.error_code)?;
105        types::Int64.encode(buf, &self.producer_id)?;
106        types::Int16.encode(buf, &self.producer_epoch)?;
107        if version >= 2 {
108            let num_tagged_fields = self.unknown_tagged_fields.len();
109            if num_tagged_fields > std::u32::MAX as usize {
110                bail!(
111                    "Too many tagged fields to encode ({} fields)",
112                    num_tagged_fields
113                );
114            }
115            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
116
117            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
118        }
119        Ok(())
120    }
121    fn compute_size(&self, version: i16) -> Result<usize> {
122        let mut total_size = 0;
123        total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
124        total_size += types::Int16.compute_size(&self.error_code)?;
125        total_size += types::Int64.compute_size(&self.producer_id)?;
126        total_size += types::Int16.compute_size(&self.producer_epoch)?;
127        if version >= 2 {
128            let num_tagged_fields = self.unknown_tagged_fields.len();
129            if num_tagged_fields > std::u32::MAX as usize {
130                bail!(
131                    "Too many tagged fields to encode ({} fields)",
132                    num_tagged_fields
133                );
134            }
135            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
136
137            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
138        }
139        Ok(total_size)
140    }
141}
142
143#[cfg(feature = "client")]
144impl Decodable for InitProducerIdResponse {
145    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
146        if version < 0 || version > 5 {
147            bail!("specified version not supported by this message type");
148        }
149        let throttle_time_ms = types::Int32.decode(buf)?;
150        let error_code = types::Int16.decode(buf)?;
151        let producer_id = types::Int64.decode(buf)?;
152        let producer_epoch = types::Int16.decode(buf)?;
153        let mut unknown_tagged_fields = BTreeMap::new();
154        if version >= 2 {
155            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
156            for _ in 0..num_tagged_fields {
157                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
158                let size: u32 = types::UnsignedVarInt.decode(buf)?;
159                let unknown_value = buf.try_get_bytes(size as usize)?;
160                unknown_tagged_fields.insert(tag as i32, unknown_value);
161            }
162        }
163        Ok(Self {
164            throttle_time_ms,
165            error_code,
166            producer_id,
167            producer_epoch,
168            unknown_tagged_fields,
169        })
170    }
171}
172
173impl Default for InitProducerIdResponse {
174    fn default() -> Self {
175        Self {
176            throttle_time_ms: 0,
177            error_code: 0,
178            producer_id: (-1).into(),
179            producer_epoch: 0,
180            unknown_tagged_fields: BTreeMap::new(),
181        }
182    }
183}
184
185impl Message for InitProducerIdResponse {
186    const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
187    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
188}
189
190impl HeaderVersion for InitProducerIdResponse {
191    fn header_version(version: i16) -> i16 {
192        if version >= 2 {
193            1
194        } else {
195            0
196        }
197    }
198}