odradek_protocol/messages/
list_offsets_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 >= 6
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ListOffsetsRequest {
31 pub replica_id: i32,
33 pub isolation_level: i8,
35 pub topics: Vec<ListOffsetsTopic>,
37 pub timeout_ms: i32,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for ListOffsetsRequest {
44 fn default() -> Self {
45 Self {
46 replica_id: 0,
47 isolation_level: 0,
48 topics: Vec::new(),
49 timeout_ms: 0,
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl ListOffsetsRequest {
56 pub const API_KEY: i16 = 2;
58 pub const MIN_VERSION: i16 = 1;
60 pub const MAX_VERSION: i16 = 10;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 buf.put_i32(self.replica_id);
66 if version >= 2 {
67 buf.put_i8(self.isolation_level);
68 }
69 if is_flexible(version) {
70 wire::put_compact_array_len(buf, Some(self.topics.len()));
71 } else {
72 wire::put_array_len(buf, Some(self.topics.len()))?;
73 }
74 for item in &self.topics {
75 item.encode(buf, version)?;
76 }
77 if version >= 10 {
78 buf.put_i32(self.timeout_ms);
79 }
80 if is_flexible(version) {
81 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
82 }
83 Ok(())
84 }
85
86 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
89 Self::decode_with_limits(buf, version, Limits::default())
90 }
91
92 pub fn decode_with_limits(
94 buf: &mut impl Buf,
95 version: i16,
96 limits: Limits,
97 ) -> Result<Self, DecodeError> {
98 let mut budget = limits.budget(buf.remaining());
99 Self::decode_with_budget(buf, version, &mut budget)
100 }
101
102 pub fn decode_with_budget(
104 buf: &mut impl Buf,
105 version: i16,
106 budget: &mut Budget,
107 ) -> Result<Self, DecodeError> {
108 let mut this = Self::default();
109 this.replica_id = wire::get_i32(buf)?;
110 if version >= 2 {
111 this.isolation_level = wire::get_i8(buf)?;
112 }
113 this.topics = {
114 let len = if is_flexible(version) {
115 wire::get_compact_array_len(buf)?
116 } else {
117 wire::get_array_len(buf)?
118 };
119 match len {
120 None => return Err(DecodeError::InvalidLength(-1)),
121 Some(n) => {
122 let mut items = Vec::new();
123 for _ in 0..n {
124 let mark = buf.remaining();
125 let item = ListOffsetsTopic::decode_with_budget(buf, version, budget)?;
126 budget.progress(mark, buf.remaining())?;
127 budget.push(&mut items, item)?;
128 }
129 items
130 }
131 }
132 };
133 if version >= 10 {
134 this.timeout_ms = wire::get_i32(buf)?;
135 }
136 if is_flexible(version) {
137 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
138 }
139 Ok(this)
140 }
141}
142
143impl crate::Message for ListOffsetsRequest {
144 const API_KEY: i16 = 2;
145 const MIN_VERSION: i16 = 1;
146 const MAX_VERSION: i16 = 10;
147
148 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
149 ListOffsetsRequest::encode(self, buf, version)
150 }
151 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
152 ListOffsetsRequest::decode(buf, version)
153 }
154 fn decode_with_limits(
155 buf: &mut impl Buf,
156 version: i16,
157 limits: Limits,
158 ) -> Result<Self, DecodeError> {
159 ListOffsetsRequest::decode_with_limits(buf, version, limits)
160 }
161}
162
163#[derive(Debug, Clone, PartialEq)]
165#[non_exhaustive]
166pub struct ListOffsetsTopic {
167 pub name: String,
169 pub partitions: Vec<ListOffsetsPartition>,
171 pub unknown_tagged_fields: Vec<RawTaggedField>,
173}
174
175impl Default for ListOffsetsTopic {
176 fn default() -> Self {
177 Self {
178 name: String::new(),
179 partitions: Vec::new(),
180 unknown_tagged_fields: Vec::new(),
181 }
182 }
183}
184
185impl ListOffsetsTopic {
186 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
188 if is_flexible(version) {
189 wire::put_compact_string(buf, &self.name);
190 } else {
191 wire::put_string(buf, &self.name)?;
192 }
193 if is_flexible(version) {
194 wire::put_compact_array_len(buf, Some(self.partitions.len()));
195 } else {
196 wire::put_array_len(buf, Some(self.partitions.len()))?;
197 }
198 for item in &self.partitions {
199 item.encode(buf, version)?;
200 }
201 if is_flexible(version) {
202 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
203 }
204 Ok(())
205 }
206
207 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
210 Self::decode_with_limits(buf, version, Limits::default())
211 }
212
213 pub fn decode_with_limits(
215 buf: &mut impl Buf,
216 version: i16,
217 limits: Limits,
218 ) -> Result<Self, DecodeError> {
219 let mut budget = limits.budget(buf.remaining());
220 Self::decode_with_budget(buf, version, &mut budget)
221 }
222
223 pub fn decode_with_budget(
225 buf: &mut impl Buf,
226 version: i16,
227 budget: &mut Budget,
228 ) -> Result<Self, DecodeError> {
229 let mut this = Self::default();
230 this.name = if is_flexible(version) {
231 wire::get_compact_string(buf)?
232 } else {
233 wire::get_string(buf)?
234 };
235 this.partitions = {
236 let len = if is_flexible(version) {
237 wire::get_compact_array_len(buf)?
238 } else {
239 wire::get_array_len(buf)?
240 };
241 match len {
242 None => return Err(DecodeError::InvalidLength(-1)),
243 Some(n) => {
244 let mut items = Vec::new();
245 for _ in 0..n {
246 let mark = buf.remaining();
247 let item = ListOffsetsPartition::decode_with_budget(buf, version, budget)?;
248 budget.progress(mark, buf.remaining())?;
249 budget.push(&mut items, item)?;
250 }
251 items
252 }
253 }
254 };
255 if is_flexible(version) {
256 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
257 }
258 Ok(this)
259 }
260}
261
262#[derive(Debug, Clone, PartialEq)]
264#[non_exhaustive]
265pub struct ListOffsetsPartition {
266 pub partition_index: i32,
268 pub current_leader_epoch: i32,
270 pub timestamp: i64,
272 pub unknown_tagged_fields: Vec<RawTaggedField>,
274}
275
276impl Default for ListOffsetsPartition {
277 fn default() -> Self {
278 Self {
279 partition_index: 0,
280 current_leader_epoch: -1,
281 timestamp: 0,
282 unknown_tagged_fields: Vec::new(),
283 }
284 }
285}
286
287impl ListOffsetsPartition {
288 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
290 buf.put_i32(self.partition_index);
291 if version >= 4 {
292 buf.put_i32(self.current_leader_epoch);
293 }
294 buf.put_i64(self.timestamp);
295 if is_flexible(version) {
296 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
297 }
298 Ok(())
299 }
300
301 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
304 Self::decode_with_limits(buf, version, Limits::default())
305 }
306
307 pub fn decode_with_limits(
309 buf: &mut impl Buf,
310 version: i16,
311 limits: Limits,
312 ) -> Result<Self, DecodeError> {
313 let mut budget = limits.budget(buf.remaining());
314 Self::decode_with_budget(buf, version, &mut budget)
315 }
316
317 pub fn decode_with_budget(
319 buf: &mut impl Buf,
320 version: i16,
321 budget: &mut Budget,
322 ) -> Result<Self, DecodeError> {
323 let mut this = Self::default();
324 this.partition_index = wire::get_i32(buf)?;
325 if version >= 4 {
326 this.current_leader_epoch = wire::get_i32(buf)?;
327 }
328 this.timestamp = wire::get_i64(buf)?;
329 if is_flexible(version) {
330 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
331 }
332 Ok(this)
333 }
334}