crabka_protocol/opt/rustwide/workdir/generated/
EndTxnRequest.borrowed.rs1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i64, put_bool, put_i16, put_i64};
6use crate::primitives::string_bytes::{
7 compact_string_len, put_compact_string, put_string, string_len,
8};
9use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
12
13pub const API_KEY: i16 = 26;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 5;
16pub const FLEXIBLE_MIN: i16 = 3;
17
18#[inline]
19fn is_flexible(version: i16) -> bool {
20 version >= FLEXIBLE_MIN
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct EndTxnRequest<'a> {
25 pub transactional_id: &'a str,
26 pub producer_id: i64,
27 pub producer_epoch: i16,
28 pub committed: bool,
29 pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl EndTxnRequest<'_> {
32 pub fn to_owned(&self) -> crate::owned::end_txn_request::EndTxnRequest {
33 crate::owned::end_txn_request::EndTxnRequest {
34 transactional_id: (self.transactional_id).to_string(),
35 producer_id: (self.producer_id),
36 producer_epoch: (self.producer_epoch),
37 committed: (self.committed),
38 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
39 }
40 }
41}
42impl Encode for EndTxnRequest<'_> {
43 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
44 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
45 return Err(ProtocolError::UnsupportedVersion {
46 api_key: API_KEY,
47 version,
48 });
49 }
50 let flex = is_flexible(version);
51 if version >= 0 {
52 if flex {
53 put_compact_string(buf, self.transactional_id);
54 } else {
55 put_string(buf, self.transactional_id);
56 }
57 }
58 if version >= 0 {
59 put_i64(buf, self.producer_id);
60 }
61 if version >= 0 {
62 put_i16(buf, self.producer_epoch);
63 }
64 if version >= 0 {
65 put_bool(buf, self.committed);
66 }
67 if flex {
68 let tagged = WriteTaggedFields::new();
69 tagged.write(buf, &self.unknown_tagged_fields);
70 }
71 Ok(())
72 }
73 fn encoded_len(&self, version: i16) -> usize {
74 let flex = is_flexible(version);
75 let mut n: usize = 0;
76 if version >= 0 {
77 n += if flex {
78 compact_string_len(self.transactional_id)
79 } else {
80 string_len(self.transactional_id)
81 };
82 }
83 if version >= 0 {
84 n += 8;
85 }
86 if version >= 0 {
87 n += 2;
88 }
89 if version >= 0 {
90 n += 1;
91 }
92 if flex {
93 let known_pairs: Vec<(u32, usize)> = Vec::new();
94 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
95 }
96 n
97 }
98}
99impl<'de> DecodeBorrow<'de> for EndTxnRequest<'de> {
100 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
101 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
102 return Err(ProtocolError::UnsupportedVersion {
103 api_key: API_KEY,
104 version,
105 });
106 }
107 let flex = is_flexible(version);
108 let mut out = Self::default();
109 if version >= 0 {
110 out.transactional_id = if flex {
111 get_compact_string_borrowed(buf)?
112 } else {
113 get_string_borrowed(buf)?
114 };
115 }
116 if version >= 0 {
117 out.producer_id = get_i64(buf)?;
118 }
119 if version >= 0 {
120 out.producer_epoch = get_i16(buf)?;
121 }
122 if version >= 0 {
123 out.committed = get_bool(buf)?;
124 }
125 if flex {
126 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
127 }
128 Ok(out)
129 }
130}
131#[cfg(test)]
132impl EndTxnRequest<'_> {
133 #[must_use]
134 pub fn populated(version: i16) -> Self {
135 let mut m = Self::default();
136 if version >= 0 {
137 m.transactional_id = "x";
138 }
139 if version >= 0 {
140 m.producer_id = 1i64;
141 }
142 if version >= 0 {
143 m.producer_epoch = 1i16;
144 }
145 if version >= 0 {
146 m.committed = true;
147 }
148 m
149 }
150}