odradek_protocol/messages/
end_txn_request.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 3
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct EndTxnRequest {
31 pub transactional_id: String,
33 pub producer_id: i64,
35 pub producer_epoch: i16,
37 pub committed: bool,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for EndTxnRequest {
44 fn default() -> Self {
45 Self {
46 transactional_id: String::new(),
47 producer_id: 0,
48 producer_epoch: 0,
49 committed: false,
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl EndTxnRequest {
56 pub const API_KEY: i16 = 26;
58 pub const MIN_VERSION: i16 = 0;
60 pub const MAX_VERSION: i16 = 5;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 if is_flexible(version) {
66 wire::put_compact_string(buf, &self.transactional_id);
67 } else {
68 wire::put_string(buf, &self.transactional_id)?;
69 }
70 buf.put_i64(self.producer_id);
71 buf.put_i16(self.producer_epoch);
72 buf.put_u8(u8::from(self.committed));
73 if is_flexible(version) {
74 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
75 }
76 Ok(())
77 }
78
79 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
82 Self::decode_with_limits(buf, version, Limits::default())
83 }
84
85 pub fn decode_with_limits(
87 buf: &mut impl Buf,
88 version: i16,
89 limits: Limits,
90 ) -> Result<Self, DecodeError> {
91 let mut budget = limits.budget(buf.remaining());
92 Self::decode_with_budget(buf, version, &mut budget)
93 }
94
95 pub fn decode_with_budget(
97 buf: &mut impl Buf,
98 version: i16,
99 budget: &mut Budget,
100 ) -> Result<Self, DecodeError> {
101 let mut this = Self::default();
102 this.transactional_id = if is_flexible(version) {
103 wire::get_compact_string(buf)?
104 } else {
105 wire::get_string(buf)?
106 };
107 this.producer_id = wire::get_i64(buf)?;
108 this.producer_epoch = wire::get_i16(buf)?;
109 this.committed = wire::get_bool(buf)?;
110 if is_flexible(version) {
111 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
112 }
113 Ok(this)
114 }
115}
116
117impl crate::Message for EndTxnRequest {
118 const API_KEY: i16 = 26;
119 const MIN_VERSION: i16 = 0;
120 const MAX_VERSION: i16 = 5;
121
122 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
123 EndTxnRequest::encode(self, buf, version)
124 }
125 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
126 EndTxnRequest::decode(buf, version)
127 }
128 fn decode_with_limits(
129 buf: &mut impl Buf,
130 version: i16,
131 limits: Limits,
132 ) -> Result<Self, DecodeError> {
133 EndTxnRequest::decode_with_limits(buf, version, limits)
134 }
135}