crabka_protocol/opt/rustwide/workdir/generated/
EndTxnRequest.owned.rs1use bytes::{Buf, 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, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
8 string_len,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{Decode, 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 {
25 pub transactional_id: String,
26 pub producer_id: i64,
27 pub producer_epoch: i16,
28 pub committed: bool,
29 pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Encode for EndTxnRequest {
32 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
33 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
34 return Err(ProtocolError::UnsupportedVersion {
35 api_key: API_KEY,
36 version,
37 });
38 }
39 let flex = is_flexible(version);
40 if version >= 0 {
41 if flex {
42 put_compact_string(buf, &self.transactional_id);
43 } else {
44 put_string(buf, &self.transactional_id);
45 }
46 }
47 if version >= 0 {
48 put_i64(buf, self.producer_id);
49 }
50 if version >= 0 {
51 put_i16(buf, self.producer_epoch);
52 }
53 if version >= 0 {
54 put_bool(buf, self.committed);
55 }
56 if flex {
57 let tagged = WriteTaggedFields::new();
58 tagged.write(buf, &self.unknown_tagged_fields);
59 }
60 Ok(())
61 }
62 fn encoded_len(&self, version: i16) -> usize {
63 let flex = is_flexible(version);
64 let mut n: usize = 0;
65 if version >= 0 {
66 n += if flex {
67 compact_string_len(&self.transactional_id)
68 } else {
69 string_len(&self.transactional_id)
70 };
71 }
72 if version >= 0 {
73 n += 8;
74 }
75 if version >= 0 {
76 n += 2;
77 }
78 if version >= 0 {
79 n += 1;
80 }
81 if flex {
82 let known_pairs: Vec<(u32, usize)> = Vec::new();
83 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
84 }
85 n
86 }
87}
88impl Decode<'_> for EndTxnRequest {
89 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
90 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
91 return Err(ProtocolError::UnsupportedVersion {
92 api_key: API_KEY,
93 version,
94 });
95 }
96 let flex = is_flexible(version);
97 let mut out = Self::default();
98 if version >= 0 {
99 out.transactional_id = if flex {
100 get_compact_string_owned(buf)?
101 } else {
102 get_string_owned(buf)?
103 };
104 }
105 if version >= 0 {
106 out.producer_id = get_i64(buf)?;
107 }
108 if version >= 0 {
109 out.producer_epoch = get_i16(buf)?;
110 }
111 if version >= 0 {
112 out.committed = get_bool(buf)?;
113 }
114 if flex {
115 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
116 }
117 Ok(out)
118 }
119}
120#[cfg(test)]
121impl EndTxnRequest {
122 #[must_use]
123 pub fn populated(version: i16) -> Self {
124 let mut m = Self::default();
125 if version >= 0 {
126 m.transactional_id = "x".to_string();
127 }
128 if version >= 0 {
129 m.producer_id = 1i64;
130 }
131 if version >= 0 {
132 m.producer_epoch = 1i16;
133 }
134 if version >= 0 {
135 m.committed = true;
136 }
137 m
138 }
139}
140
141#[must_use]
144#[allow(unused_comparisons)]
145pub fn default_json(version: i16) -> ::serde_json::Value {
146 let mut obj = ::serde_json::Map::new();
147 obj.insert(
148 "transactionalId".to_string(),
149 ::serde_json::Value::String(String::new()),
150 );
151 obj.insert("producerId".to_string(), ::serde_json::json!(0));
152 obj.insert("producerEpoch".to_string(), ::serde_json::json!(0));
153 obj.insert("committed".to_string(), ::serde_json::Value::Bool(false));
154 ::serde_json::Value::Object(obj)
155}
156
157impl crate::ProtocolRequest for EndTxnRequest {
158 const API_KEY: i16 = API_KEY;
159 const MIN_VERSION: i16 = MIN_VERSION;
160 const MAX_VERSION: i16 = MAX_VERSION;
161 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
162 type Response = super::end_txn_response::EndTxnResponse;
163}