kafka_protocol/messages/
end_txn_response.rs

1//! EndTxnResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/EndTxnResponse.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 EndTxnResponse {
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 producer ID.
35    ///
36    /// Supported API versions: 5
37    pub producer_id: super::ProducerId,
38
39    /// The current epoch associated with the producer.
40    ///
41    /// Supported API versions: 5
42    pub producer_epoch: i16,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl EndTxnResponse {
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 producer ID.
70    ///
71    /// Supported API versions: 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.
79    ///
80    /// Supported API versions: 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 EndTxnResponse {
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        if version >= 5 {
106            types::Int64.encode(buf, &self.producer_id)?;
107        }
108        if version >= 5 {
109            types::Int16.encode(buf, &self.producer_epoch)?;
110        }
111        if version >= 3 {
112            let num_tagged_fields = self.unknown_tagged_fields.len();
113            if num_tagged_fields > std::u32::MAX as usize {
114                bail!(
115                    "Too many tagged fields to encode ({} fields)",
116                    num_tagged_fields
117                );
118            }
119            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
120
121            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
122        }
123        Ok(())
124    }
125    fn compute_size(&self, version: i16) -> Result<usize> {
126        let mut total_size = 0;
127        total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
128        total_size += types::Int16.compute_size(&self.error_code)?;
129        if version >= 5 {
130            total_size += types::Int64.compute_size(&self.producer_id)?;
131        }
132        if version >= 5 {
133            total_size += types::Int16.compute_size(&self.producer_epoch)?;
134        }
135        if version >= 3 {
136            let num_tagged_fields = self.unknown_tagged_fields.len();
137            if num_tagged_fields > std::u32::MAX as usize {
138                bail!(
139                    "Too many tagged fields to encode ({} fields)",
140                    num_tagged_fields
141                );
142            }
143            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
144
145            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
146        }
147        Ok(total_size)
148    }
149}
150
151#[cfg(feature = "client")]
152impl Decodable for EndTxnResponse {
153    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
154        if version < 0 || version > 5 {
155            bail!("specified version not supported by this message type");
156        }
157        let throttle_time_ms = types::Int32.decode(buf)?;
158        let error_code = types::Int16.decode(buf)?;
159        let producer_id = if version >= 5 {
160            types::Int64.decode(buf)?
161        } else {
162            (-1).into()
163        };
164        let producer_epoch = if version >= 5 {
165            types::Int16.decode(buf)?
166        } else {
167            -1
168        };
169        let mut unknown_tagged_fields = BTreeMap::new();
170        if version >= 3 {
171            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
172            for _ in 0..num_tagged_fields {
173                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
174                let size: u32 = types::UnsignedVarInt.decode(buf)?;
175                let unknown_value = buf.try_get_bytes(size as usize)?;
176                unknown_tagged_fields.insert(tag as i32, unknown_value);
177            }
178        }
179        Ok(Self {
180            throttle_time_ms,
181            error_code,
182            producer_id,
183            producer_epoch,
184            unknown_tagged_fields,
185        })
186    }
187}
188
189impl Default for EndTxnResponse {
190    fn default() -> Self {
191        Self {
192            throttle_time_ms: 0,
193            error_code: 0,
194            producer_id: (-1).into(),
195            producer_epoch: -1,
196            unknown_tagged_fields: BTreeMap::new(),
197        }
198    }
199}
200
201impl Message for EndTxnResponse {
202    const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
203    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
204}
205
206impl HeaderVersion for EndTxnResponse {
207    fn header_version(version: i16) -> i16 {
208        if version >= 3 {
209            1
210        } else {
211            0
212        }
213    }
214}