kafka_protocol/messages/
end_txn_request.rs

1//! EndTxnRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/EndTxnRequest.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-4
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct EndTxnRequest {
24    /// The ID of the transaction to end.
25    ///
26    /// Supported API versions: 0-4
27    pub transactional_id: super::TransactionalId,
28
29    /// The producer ID.
30    ///
31    /// Supported API versions: 0-4
32    pub producer_id: super::ProducerId,
33
34    /// The current epoch associated with the producer.
35    ///
36    /// Supported API versions: 0-4
37    pub producer_epoch: i16,
38
39    /// True if the transaction was committed, false if it was aborted.
40    ///
41    /// Supported API versions: 0-4
42    pub committed: bool,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl EndTxnRequest {
49    /// Sets `transactional_id` to the passed value.
50    ///
51    /// The ID of the transaction to end.
52    ///
53    /// Supported API versions: 0-4
54    pub fn with_transactional_id(mut self, value: super::TransactionalId) -> Self {
55        self.transactional_id = value;
56        self
57    }
58    /// Sets `producer_id` to the passed value.
59    ///
60    /// The producer ID.
61    ///
62    /// Supported API versions: 0-4
63    pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
64        self.producer_id = value;
65        self
66    }
67    /// Sets `producer_epoch` to the passed value.
68    ///
69    /// The current epoch associated with the producer.
70    ///
71    /// Supported API versions: 0-4
72    pub fn with_producer_epoch(mut self, value: i16) -> Self {
73        self.producer_epoch = value;
74        self
75    }
76    /// Sets `committed` to the passed value.
77    ///
78    /// True if the transaction was committed, false if it was aborted.
79    ///
80    /// Supported API versions: 0-4
81    pub fn with_committed(mut self, value: bool) -> Self {
82        self.committed = 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 = "client")]
98impl Encodable for EndTxnRequest {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        if version < 0 || version > 4 {
101            bail!("specified version not supported by this message type");
102        }
103        if version >= 3 {
104            types::CompactString.encode(buf, &self.transactional_id)?;
105        } else {
106            types::String.encode(buf, &self.transactional_id)?;
107        }
108        types::Int64.encode(buf, &self.producer_id)?;
109        types::Int16.encode(buf, &self.producer_epoch)?;
110        types::Boolean.encode(buf, &self.committed)?;
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        if version >= 3 {
128            total_size += types::CompactString.compute_size(&self.transactional_id)?;
129        } else {
130            total_size += types::String.compute_size(&self.transactional_id)?;
131        }
132        total_size += types::Int64.compute_size(&self.producer_id)?;
133        total_size += types::Int16.compute_size(&self.producer_epoch)?;
134        total_size += types::Boolean.compute_size(&self.committed)?;
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 = "broker")]
152impl Decodable for EndTxnRequest {
153    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
154        if version < 0 || version > 4 {
155            bail!("specified version not supported by this message type");
156        }
157        let transactional_id = if version >= 3 {
158            types::CompactString.decode(buf)?
159        } else {
160            types::String.decode(buf)?
161        };
162        let producer_id = types::Int64.decode(buf)?;
163        let producer_epoch = types::Int16.decode(buf)?;
164        let committed = types::Boolean.decode(buf)?;
165        let mut unknown_tagged_fields = BTreeMap::new();
166        if version >= 3 {
167            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
168            for _ in 0..num_tagged_fields {
169                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
170                let size: u32 = types::UnsignedVarInt.decode(buf)?;
171                let unknown_value = buf.try_get_bytes(size as usize)?;
172                unknown_tagged_fields.insert(tag as i32, unknown_value);
173            }
174        }
175        Ok(Self {
176            transactional_id,
177            producer_id,
178            producer_epoch,
179            committed,
180            unknown_tagged_fields,
181        })
182    }
183}
184
185impl Default for EndTxnRequest {
186    fn default() -> Self {
187        Self {
188            transactional_id: Default::default(),
189            producer_id: (0).into(),
190            producer_epoch: 0,
191            committed: false,
192            unknown_tagged_fields: BTreeMap::new(),
193        }
194    }
195}
196
197impl Message for EndTxnRequest {
198    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
199    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
200}
201
202impl HeaderVersion for EndTxnRequest {
203    fn header_version(version: i16) -> i16 {
204        if version >= 3 {
205            2
206        } else {
207            1
208        }
209    }
210}