1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10 string_len,
11};
12use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 79;
16pub const MIN_VERSION: i16 = 1;
17pub const MAX_VERSION: i16 = 2;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct ShareAcknowledgeResponse {
25 pub throttle_time_ms: i32,
26 pub error_code: i16,
27 pub error_message: Option<String>,
28 pub acquisition_lock_timeout_ms: i32,
29 pub responses: Vec<ShareAcknowledgeTopicResponse>,
30 pub node_endpoints: Vec<NodeEndpoint>,
31 pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl Encode for ShareAcknowledgeResponse {
35 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
38 }
39 let flex = is_flexible(version);
40 if version >= 0 { put_i32(buf, self.throttle_time_ms) }
41 if version >= 0 { put_i16(buf, self.error_code) }
42 if version >= 0 { if flex { put_compact_nullable_string(buf, self.error_message.as_deref()) } else { put_nullable_string(buf, self.error_message.as_deref()) } }
43 if version >= 2 { put_i32(buf, self.acquisition_lock_timeout_ms) }
44 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.responses).len(), flex); for it in &self.responses { it.encode(buf, version)?; } } }
45 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.node_endpoints).len(), flex); for it in &self.node_endpoints { it.encode(buf, version)?; } } }
46 if flex {
47 let tagged = WriteTaggedFields::new();
48 tagged.write(buf, &self.unknown_tagged_fields);
49 }
50 Ok(())
51 }
52 fn encoded_len(&self, version: i16) -> usize {
53 let flex = is_flexible(version);
54 let mut n: usize = 0;
55 if version >= 0 { n += 4; }
56 if version >= 0 { n += 2; }
57 if version >= 0 { n += if flex { compact_nullable_string_len(self.error_message.as_deref()) } else { nullable_string_len(self.error_message.as_deref()) }; }
58 if version >= 2 { n += 4; }
59 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.responses).len(), flex); let body: usize = (self.responses).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
60 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.node_endpoints).len(), flex); let body: usize = (self.node_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
61 if flex {
62 let known_pairs: Vec<(u32, usize)> = Vec::new();
63 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
64 }
65 n
66 }
67}
68
69impl<'de> Decode<'de> for ShareAcknowledgeResponse {
70 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
71 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
72 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
73 }
74 let flex = is_flexible(version);
75 let mut out = Self::default();
76 if version >= 0 { out.throttle_time_ms = get_i32(buf)?; }
77 if version >= 0 { out.error_code = get_i16(buf)?; }
78 if version >= 0 { out.error_message = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
79 if version >= 2 { out.acquisition_lock_timeout_ms = get_i32(buf)?; }
80 if version >= 0 { out.responses = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(ShareAcknowledgeTopicResponse::decode(buf, version)?); } v }; }
81 if version >= 0 { out.node_endpoints = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(NodeEndpoint::decode(buf, version)?); } v }; }
82 if flex {
83 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
84 Ok(false)
85 })?;
86 }
87 Ok(out)
88 }
89}
90
91#[derive(Debug, Clone, PartialEq, Eq, Default)]
92pub struct ShareAcknowledgeTopicResponse {
93 pub topic_id: crate::primitives::uuid::Uuid,
94 pub partitions: Vec<PartitionData>,
95 pub unknown_tagged_fields: UnknownTaggedFields,
96}
97
98impl Encode for ShareAcknowledgeTopicResponse {
99 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
100 let flex = version >= 0;
101 if version >= 0 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
102 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
103 if flex {
104 let tagged = WriteTaggedFields::new();
105 tagged.write(buf, &self.unknown_tagged_fields);
106 }
107 Ok(())
108 }
109 fn encoded_len(&self, version: i16) -> usize {
110 let flex = version >= 0;
111 let mut n: usize = 0;
112 if version >= 0 { n += 16; }
113 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
114 if flex {
115 let known_pairs: Vec<(u32, usize)> = Vec::new();
116 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
117 }
118 n
119 }
120}
121
122impl<'de> Decode<'de> for ShareAcknowledgeTopicResponse {
123 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
124 let flex = version >= 0;
125 let mut out = Self::default();
126 if version >= 0 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
127 if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionData::decode(buf, version)?); } v }; }
128 if flex {
129 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
130 Ok(false)
131 })?;
132 }
133 Ok(out)
134 }
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Default)]
138pub struct PartitionData {
139 pub partition_index: i32,
140 pub error_code: i16,
141 pub error_message: Option<String>,
142 pub current_leader: LeaderIdAndEpoch,
143 pub unknown_tagged_fields: UnknownTaggedFields,
144}
145
146impl Encode for PartitionData {
147 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
148 let flex = version >= 0;
149 if version >= 0 { put_i32(buf, self.partition_index) }
150 if version >= 0 { put_i16(buf, self.error_code) }
151 if version >= 0 { if flex { put_compact_nullable_string(buf, self.error_message.as_deref()) } else { put_nullable_string(buf, self.error_message.as_deref()) } }
152 if version >= 0 { self.current_leader.encode(buf, version)? }
153 if flex {
154 let tagged = WriteTaggedFields::new();
155 tagged.write(buf, &self.unknown_tagged_fields);
156 }
157 Ok(())
158 }
159 fn encoded_len(&self, version: i16) -> usize {
160 let flex = version >= 0;
161 let mut n: usize = 0;
162 if version >= 0 { n += 4; }
163 if version >= 0 { n += 2; }
164 if version >= 0 { n += if flex { compact_nullable_string_len(self.error_message.as_deref()) } else { nullable_string_len(self.error_message.as_deref()) }; }
165 if version >= 0 { n += self.current_leader.encoded_len(version); }
166 if flex {
167 let known_pairs: Vec<(u32, usize)> = Vec::new();
168 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
169 }
170 n
171 }
172}
173
174impl<'de> Decode<'de> for PartitionData {
175 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
176 let flex = version >= 0;
177 let mut out = Self::default();
178 if version >= 0 { out.partition_index = get_i32(buf)?; }
179 if version >= 0 { out.error_code = get_i16(buf)?; }
180 if version >= 0 { out.error_message = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
181 if version >= 0 { out.current_leader = LeaderIdAndEpoch::decode(buf, version)?; }
182 if flex {
183 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
184 Ok(false)
185 })?;
186 }
187 Ok(out)
188 }
189}
190
191#[derive(Debug, Clone, PartialEq, Eq, Default)]
192pub struct LeaderIdAndEpoch {
193 pub leader_id: i32,
194 pub leader_epoch: i32,
195 pub unknown_tagged_fields: UnknownTaggedFields,
196}
197
198impl Encode for LeaderIdAndEpoch {
199 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
200 let flex = version >= 0;
201 if version >= 0 { put_i32(buf, self.leader_id) }
202 if version >= 0 { put_i32(buf, self.leader_epoch) }
203 if flex {
204 let tagged = WriteTaggedFields::new();
205 tagged.write(buf, &self.unknown_tagged_fields);
206 }
207 Ok(())
208 }
209 fn encoded_len(&self, version: i16) -> usize {
210 let flex = version >= 0;
211 let mut n: usize = 0;
212 if version >= 0 { n += 4; }
213 if version >= 0 { n += 4; }
214 if flex {
215 let known_pairs: Vec<(u32, usize)> = Vec::new();
216 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
217 }
218 n
219 }
220}
221
222impl<'de> Decode<'de> for LeaderIdAndEpoch {
223 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
224 let flex = version >= 0;
225 let mut out = Self::default();
226 if version >= 0 { out.leader_id = get_i32(buf)?; }
227 if version >= 0 { out.leader_epoch = get_i32(buf)?; }
228 if flex {
229 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
230 Ok(false)
231 })?;
232 }
233 Ok(out)
234 }
235}
236
237#[derive(Debug, Clone, PartialEq, Eq, Default)]
238pub struct NodeEndpoint {
239 pub node_id: i32,
240 pub host: String,
241 pub port: i32,
242 pub rack: Option<String>,
243 pub unknown_tagged_fields: UnknownTaggedFields,
244}
245
246impl Encode for NodeEndpoint {
247 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
248 let flex = version >= 0;
249 if version >= 0 { put_i32(buf, self.node_id) }
250 if version >= 0 { if flex { put_compact_string(buf, &self.host) } else { put_string(buf, &self.host) } }
251 if version >= 0 { put_i32(buf, self.port) }
252 if version >= 0 { if flex { put_compact_nullable_string(buf, self.rack.as_deref()) } else { put_nullable_string(buf, self.rack.as_deref()) } }
253 if flex {
254 let tagged = WriteTaggedFields::new();
255 tagged.write(buf, &self.unknown_tagged_fields);
256 }
257 Ok(())
258 }
259 fn encoded_len(&self, version: i16) -> usize {
260 let flex = version >= 0;
261 let mut n: usize = 0;
262 if version >= 0 { n += 4; }
263 if version >= 0 { n += if flex { compact_string_len(&self.host) } else { string_len(&self.host) }; }
264 if version >= 0 { n += 4; }
265 if version >= 0 { n += if flex { compact_nullable_string_len(self.rack.as_deref()) } else { nullable_string_len(self.rack.as_deref()) }; }
266 if flex {
267 let known_pairs: Vec<(u32, usize)> = Vec::new();
268 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
269 }
270 n
271 }
272}
273
274impl<'de> Decode<'de> for NodeEndpoint {
275 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
276 let flex = version >= 0;
277 let mut out = Self::default();
278 if version >= 0 { out.node_id = get_i32(buf)?; }
279 if version >= 0 { out.host = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
280 if version >= 0 { out.port = get_i32(buf)?; }
281 if version >= 0 { out.rack = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
282 if flex {
283 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
284 Ok(false)
285 })?;
286 }
287 Ok(out)
288 }
289}
290
291#[must_use]
294#[allow(unused_comparisons)]
295pub fn default_json(version: i16) -> ::serde_json::Value {
296 let mut obj = ::serde_json::Map::new();
297 obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
298 obj.insert("errorCode".to_string(), ::serde_json::json!(0));
299 obj.insert("errorMessage".to_string(), ::serde_json::Value::Null);
300 if version >= 2 {
301 obj.insert("acquisitionLockTimeoutMs".to_string(), ::serde_json::json!(0));
302 }
303 obj.insert("responses".to_string(), ::serde_json::Value::Array(vec![]));
304 obj.insert("nodeEndpoints".to_string(), ::serde_json::Value::Array(vec![]));
305 ::serde_json::Value::Object(obj)
306}