crabka_protocol/opt/rustwide/workdir/generated/
AddOffsetsToTxnRequest.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i64, 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 = 25;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 4;
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 AddOffsetsToTxnRequest {
25 pub transactional_id: String,
26 pub producer_id: i64,
27 pub producer_epoch: i16,
28 pub group_id: String,
29 pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Encode for AddOffsetsToTxnRequest {
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 if flex {
55 put_compact_string(buf, &self.group_id);
56 } else {
57 put_string(buf, &self.group_id);
58 }
59 }
60 if flex {
61 let tagged = WriteTaggedFields::new();
62 tagged.write(buf, &self.unknown_tagged_fields);
63 }
64 Ok(())
65 }
66 fn encoded_len(&self, version: i16) -> usize {
67 let flex = is_flexible(version);
68 let mut n: usize = 0;
69 if version >= 0 {
70 n += if flex {
71 compact_string_len(&self.transactional_id)
72 } else {
73 string_len(&self.transactional_id)
74 };
75 }
76 if version >= 0 {
77 n += 8;
78 }
79 if version >= 0 {
80 n += 2;
81 }
82 if version >= 0 {
83 n += if flex {
84 compact_string_len(&self.group_id)
85 } else {
86 string_len(&self.group_id)
87 };
88 }
89 if flex {
90 let known_pairs: Vec<(u32, usize)> = Vec::new();
91 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
92 }
93 n
94 }
95}
96impl Decode<'_> for AddOffsetsToTxnRequest {
97 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
98 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
99 return Err(ProtocolError::UnsupportedVersion {
100 api_key: API_KEY,
101 version,
102 });
103 }
104 let flex = is_flexible(version);
105 let mut out = Self::default();
106 if version >= 0 {
107 out.transactional_id = if flex {
108 get_compact_string_owned(buf)?
109 } else {
110 get_string_owned(buf)?
111 };
112 }
113 if version >= 0 {
114 out.producer_id = get_i64(buf)?;
115 }
116 if version >= 0 {
117 out.producer_epoch = get_i16(buf)?;
118 }
119 if version >= 0 {
120 out.group_id = if flex {
121 get_compact_string_owned(buf)?
122 } else {
123 get_string_owned(buf)?
124 };
125 }
126 if flex {
127 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
128 }
129 Ok(out)
130 }
131}
132#[cfg(test)]
133impl AddOffsetsToTxnRequest {
134 #[must_use]
135 pub fn populated(version: i16) -> Self {
136 let mut m = Self::default();
137 if version >= 0 {
138 m.transactional_id = "x".to_string();
139 }
140 if version >= 0 {
141 m.producer_id = 1i64;
142 }
143 if version >= 0 {
144 m.producer_epoch = 1i16;
145 }
146 if version >= 0 {
147 m.group_id = "x".to_string();
148 }
149 m
150 }
151}
152
153#[must_use]
156#[allow(unused_comparisons)]
157pub fn default_json(version: i16) -> ::serde_json::Value {
158 let mut obj = ::serde_json::Map::new();
159 obj.insert(
160 "transactionalId".to_string(),
161 ::serde_json::Value::String(String::new()),
162 );
163 obj.insert("producerId".to_string(), ::serde_json::json!(0));
164 obj.insert("producerEpoch".to_string(), ::serde_json::json!(0));
165 obj.insert(
166 "groupId".to_string(),
167 ::serde_json::Value::String(String::new()),
168 );
169 ::serde_json::Value::Object(obj)
170}
171
172impl crate::ProtocolRequest for AddOffsetsToTxnRequest {
173 const API_KEY: i16 = API_KEY;
174 const MIN_VERSION: i16 = MIN_VERSION;
175 const MAX_VERSION: i16 = MAX_VERSION;
176 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
177 type Response = super::add_offsets_to_txn_response::AddOffsetsToTxnResponse;
178}