1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i32, put_bool, put_i32};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 9;
19pub const MIN_VERSION: i16 = 1;
20pub const MAX_VERSION: i16 = 10;
21pub const FLEXIBLE_MIN: i16 = 6;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct OffsetFetchRequest<'a> {
28 pub group_id: &'a str,
29 pub topics: Option<Vec<OffsetFetchRequestTopic<'a>>>,
30 pub groups: Vec<OffsetFetchRequestGroup<'a>>,
31 pub require_stable: bool,
32 pub unknown_tagged_fields: UnknownTaggedFields,
33}
34
35impl<'a> Default for OffsetFetchRequest<'a> {
36 fn default() -> Self {
37 Self {
38 group_id: "",
39 topics: None,
40 groups: Vec::new(),
41 require_stable: false,
42 unknown_tagged_fields: Default::default(),
43 }
44 }
45}
46
47impl<'a> OffsetFetchRequest<'a> {
48 pub fn to_owned(&self) -> crate::owned::offset_fetch_request::OffsetFetchRequest {
49 crate::owned::offset_fetch_request::OffsetFetchRequest {
50 group_id: (self.group_id).to_string(),
51 topics: (self.topics).as_ref().map(|v| v.iter().map(|it| it.to_owned()).collect()),
52 groups: (self.groups).iter().map(|it| it.to_owned()).collect(),
53 require_stable: (self.require_stable),
54 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
55 }
56 }
57}
58
59impl<'a> Encode for OffsetFetchRequest<'a> {
60 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
61 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
62 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
63 }
64 let flex = is_flexible(version);
65 if version >= 0 && version <= 7 { if flex { put_compact_string(buf, self.group_id) } else { put_string(buf, self.group_id) } }
66 if version >= 0 && version <= 7 { if version >= 2 { { let len = (self.topics).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.topics { for it in v { it.encode(buf, version)?; } } } } else { { let v = (self.topics).as_ref().map(Vec::as_slice).unwrap_or(&[]); crate::primitives::array::put_array_len(buf, v.len(), flex); for it in v { it.encode(buf, version)?; } } } }
67 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.groups).len(), flex); for it in &self.groups { it.encode(buf, version)?; } } }
68 if version >= 7 { put_bool(buf, self.require_stable) }
69 if flex {
70 let tagged = WriteTaggedFields::new();
71 tagged.write(buf, &self.unknown_tagged_fields);
72 }
73 Ok(())
74 }
75 fn encoded_len(&self, version: i16) -> usize {
76 let flex = is_flexible(version);
77 let mut n: usize = 0;
78 if version >= 0 && version <= 7 { n += if flex { compact_string_len(self.group_id) } else { string_len(self.group_id) }; }
79 if version >= 0 && version <= 7 { n += if version >= 2 { { let opt: Option<&Vec<_>> = (self.topics).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body } } else { { let v = (self.topics).as_ref().map(Vec::as_slice).unwrap_or(&[]); let prefix = crate::primitives::array::array_len_prefix_len(v.len(), flex); let body: usize = v.iter().map(|it| it.encoded_len(version)).sum(); prefix + body } }; }
80 if version >= 8 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.groups).len(), flex); let body: usize = (self.groups).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
81 if version >= 7 { n += 1; }
82 if flex {
83 let known_pairs: Vec<(u32, usize)> = Vec::new();
84 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85 }
86 n
87 }
88}
89
90impl<'de> DecodeBorrow<'de> for OffsetFetchRequest<'de> {
91 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
92 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
93 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
94 }
95 let flex = is_flexible(version);
96 let mut out = Self::default();
97 if version >= 0 && version <= 7 { out.group_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
98 if version >= 0 && version <= 7 { out.topics = if version >= 2 { { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchRequestTopic::decode_borrow(buf, version)?); } Some(v) } } } } else { Some({ let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchRequestTopic::decode_borrow(buf, version)?); } v }) }; }
99 if version >= 8 { out.groups = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchRequestGroup::decode_borrow(buf, version)?); } v }; }
100 if version >= 7 { out.require_stable = get_bool(buf)?; }
101 if flex {
102 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
103 Ok(false)
104 })?;
105 }
106 Ok(out)
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct OffsetFetchRequestTopic<'a> {
112 pub name: &'a str,
113 pub partition_indexes: Vec<i32>,
114 pub unknown_tagged_fields: UnknownTaggedFields,
115}
116
117impl<'a> Default for OffsetFetchRequestTopic<'a> {
118 fn default() -> Self {
119 Self {
120 name: "",
121 partition_indexes: Vec::new(),
122 unknown_tagged_fields: Default::default(),
123 }
124 }
125}
126
127impl<'a> OffsetFetchRequestTopic<'a> {
128 pub fn to_owned(&self) -> crate::owned::offset_fetch_request::OffsetFetchRequestTopic {
129 crate::owned::offset_fetch_request::OffsetFetchRequestTopic {
130 name: (self.name).to_string(),
131 partition_indexes: (self.partition_indexes).clone(),
132 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
133 }
134 }
135}
136
137impl<'a> Encode for OffsetFetchRequestTopic<'a> {
138 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
139 let flex = version >= 6;
140 if version >= 0 && version <= 7 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
141 if version >= 0 && version <= 7 { { crate::primitives::array::put_array_len(buf, (self.partition_indexes).len(), flex); for it in &self.partition_indexes { put_i32(buf, *it); } } }
142 if flex {
143 let tagged = WriteTaggedFields::new();
144 tagged.write(buf, &self.unknown_tagged_fields);
145 }
146 Ok(())
147 }
148 fn encoded_len(&self, version: i16) -> usize {
149 let flex = version >= 6;
150 let mut n: usize = 0;
151 if version >= 0 && version <= 7 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
152 if version >= 0 && version <= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partition_indexes).len(), flex); let body: usize = (self.partition_indexes).iter().map(|_| 4).sum(); prefix + body }; }
153 if flex {
154 let known_pairs: Vec<(u32, usize)> = Vec::new();
155 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
156 }
157 n
158 }
159}
160
161impl<'de> DecodeBorrow<'de> for OffsetFetchRequestTopic<'de> {
162 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
163 let flex = version >= 6;
164 let mut out = Self::default();
165 if version >= 0 && version <= 7 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
166 if version >= 0 && version <= 7 { out.partition_indexes = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
167 if flex {
168 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
169 Ok(false)
170 })?;
171 }
172 Ok(out)
173 }
174}
175
176#[derive(Debug, Clone, PartialEq, Eq)]
177pub struct OffsetFetchRequestGroup<'a> {
178 pub group_id: &'a str,
179 pub member_id: Option<&'a str>,
180 pub member_epoch: i32,
181 pub topics: Option<Vec<OffsetFetchRequestTopics<'a>>>,
182 pub unknown_tagged_fields: UnknownTaggedFields,
183}
184
185impl<'a> Default for OffsetFetchRequestGroup<'a> {
186 fn default() -> Self {
187 Self {
188 group_id: "",
189 member_id: None,
190 member_epoch: -1i32,
191 topics: None,
192 unknown_tagged_fields: Default::default(),
193 }
194 }
195}
196
197impl<'a> OffsetFetchRequestGroup<'a> {
198 pub fn to_owned(&self) -> crate::owned::offset_fetch_request::OffsetFetchRequestGroup {
199 crate::owned::offset_fetch_request::OffsetFetchRequestGroup {
200 group_id: (self.group_id).to_string(),
201 member_id: (self.member_id).map(|s| s.to_string()),
202 member_epoch: (self.member_epoch),
203 topics: (self.topics).as_ref().map(|v| v.iter().map(|it| it.to_owned()).collect()),
204 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
205 }
206 }
207}
208
209impl<'a> Encode for OffsetFetchRequestGroup<'a> {
210 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
211 let flex = version >= 6;
212 if version >= 8 { if flex { put_compact_string(buf, self.group_id) } else { put_string(buf, self.group_id) } }
213 if version >= 9 { if flex { put_compact_nullable_string(buf, self.member_id) } else { put_nullable_string(buf, self.member_id) } }
214 if version >= 9 { put_i32(buf, self.member_epoch) }
215 if version >= 8 { { let len = (self.topics).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.topics { for it in v { it.encode(buf, version)?; } } } }
216 if flex {
217 let tagged = WriteTaggedFields::new();
218 tagged.write(buf, &self.unknown_tagged_fields);
219 }
220 Ok(())
221 }
222 fn encoded_len(&self, version: i16) -> usize {
223 let flex = version >= 6;
224 let mut n: usize = 0;
225 if version >= 8 { n += if flex { compact_string_len(self.group_id) } else { string_len(self.group_id) }; }
226 if version >= 9 { n += if flex { compact_nullable_string_len(self.member_id) } else { nullable_string_len(self.member_id) }; }
227 if version >= 9 { n += 4; }
228 if version >= 8 { n += { let opt: Option<&Vec<_>> = (self.topics).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body }; }
229 if flex {
230 let known_pairs: Vec<(u32, usize)> = Vec::new();
231 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
232 }
233 n
234 }
235}
236
237impl<'de> DecodeBorrow<'de> for OffsetFetchRequestGroup<'de> {
238 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
239 let flex = version >= 6;
240 let mut out = Self::default();
241 if version >= 8 { out.group_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
242 if version >= 9 { out.member_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
243 if version >= 9 { out.member_epoch = get_i32(buf)?; }
244 if version >= 8 { out.topics = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(OffsetFetchRequestTopics::decode_borrow(buf, version)?); } Some(v) } } }; }
245 if flex {
246 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
247 Ok(false)
248 })?;
249 }
250 Ok(out)
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub struct OffsetFetchRequestTopics<'a> {
256 pub name: &'a str,
257 pub topic_id: crate::primitives::uuid::Uuid,
258 pub partition_indexes: Vec<i32>,
259 pub unknown_tagged_fields: UnknownTaggedFields,
260}
261
262impl<'a> Default for OffsetFetchRequestTopics<'a> {
263 fn default() -> Self {
264 Self {
265 name: "",
266 topic_id: Default::default(),
267 partition_indexes: Vec::new(),
268 unknown_tagged_fields: Default::default(),
269 }
270 }
271}
272
273impl<'a> OffsetFetchRequestTopics<'a> {
274 pub fn to_owned(&self) -> crate::owned::offset_fetch_request::OffsetFetchRequestTopics {
275 crate::owned::offset_fetch_request::OffsetFetchRequestTopics {
276 name: (self.name).to_string(),
277 topic_id: (self.topic_id),
278 partition_indexes: (self.partition_indexes).clone(),
279 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
280 }
281 }
282}
283
284impl<'a> Encode for OffsetFetchRequestTopics<'a> {
285 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
286 let flex = version >= 6;
287 if version >= 8 && version <= 9 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
288 if version >= 10 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
289 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.partition_indexes).len(), flex); for it in &self.partition_indexes { put_i32(buf, *it); } } }
290 if flex {
291 let tagged = WriteTaggedFields::new();
292 tagged.write(buf, &self.unknown_tagged_fields);
293 }
294 Ok(())
295 }
296 fn encoded_len(&self, version: i16) -> usize {
297 let flex = version >= 6;
298 let mut n: usize = 0;
299 if version >= 8 && version <= 9 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
300 if version >= 10 { n += 16; }
301 if version >= 8 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partition_indexes).len(), flex); let body: usize = (self.partition_indexes).iter().map(|_| 4).sum(); prefix + body }; }
302 if flex {
303 let known_pairs: Vec<(u32, usize)> = Vec::new();
304 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
305 }
306 n
307 }
308}
309
310impl<'de> DecodeBorrow<'de> for OffsetFetchRequestTopics<'de> {
311 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
312 let flex = version >= 6;
313 let mut out = Self::default();
314 if version >= 8 && version <= 9 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
315 if version >= 10 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
316 if version >= 8 { out.partition_indexes = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
317 if flex {
318 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
319 Ok(false)
320 })?;
321 }
322 Ok(out)
323 }
324}