1use bytes::{Buf, 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, 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 = 9;
16pub const MIN_VERSION: i16 = 1;
17pub const MAX_VERSION: i16 = 10;
18pub const FLEXIBLE_MIN: i16 = 6;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct OffsetFetchRequest {
25 pub group_id: String,
26 pub topics: Option<Vec<OffsetFetchRequestTopic>>,
27 pub groups: Vec<OffsetFetchRequestGroup>,
28 pub require_stable: bool,
29 pub unknown_tagged_fields: UnknownTaggedFields,
30}
31
32impl Encode for OffsetFetchRequest {
33 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
36 }
37 let flex = is_flexible(version);
38 if version >= 0 && version <= 7 { if flex { put_compact_string(buf, &self.group_id) } else { put_string(buf, &self.group_id) } }
39 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)?; } } } }
40 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.groups).len(), flex); for it in &self.groups { it.encode(buf, version)?; } } }
41 if version >= 7 { put_bool(buf, self.require_stable) }
42 if flex {
43 let tagged = WriteTaggedFields::new();
44 tagged.write(buf, &self.unknown_tagged_fields);
45 }
46 Ok(())
47 }
48 fn encoded_len(&self, version: i16) -> usize {
49 let flex = is_flexible(version);
50 let mut n: usize = 0;
51 if version >= 0 && version <= 7 { n += if flex { compact_string_len(&self.group_id) } else { string_len(&self.group_id) }; }
52 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 } }; }
53 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 }; }
54 if version >= 7 { n += 1; }
55 if flex {
56 let known_pairs: Vec<(u32, usize)> = Vec::new();
57 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
58 }
59 n
60 }
61}
62
63impl<'de> Decode<'de> for OffsetFetchRequest {
64 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
65 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
66 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
67 }
68 let flex = is_flexible(version);
69 let mut out = Self::default();
70 if version >= 0 && version <= 7 { out.group_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
71 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(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(buf, version)?); } v }) }; }
72 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(buf, version)?); } v }; }
73 if version >= 7 { out.require_stable = get_bool(buf)?; }
74 if flex {
75 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
76 Ok(false)
77 })?;
78 }
79 Ok(out)
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq, Default)]
84pub struct OffsetFetchRequestTopic {
85 pub name: String,
86 pub partition_indexes: Vec<i32>,
87 pub unknown_tagged_fields: UnknownTaggedFields,
88}
89
90impl Encode for OffsetFetchRequestTopic {
91 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
92 let flex = version >= 6;
93 if version >= 0 && version <= 7 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
94 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); } } }
95 if flex {
96 let tagged = WriteTaggedFields::new();
97 tagged.write(buf, &self.unknown_tagged_fields);
98 }
99 Ok(())
100 }
101 fn encoded_len(&self, version: i16) -> usize {
102 let flex = version >= 6;
103 let mut n: usize = 0;
104 if version >= 0 && version <= 7 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
105 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 }; }
106 if flex {
107 let known_pairs: Vec<(u32, usize)> = Vec::new();
108 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
109 }
110 n
111 }
112}
113
114impl<'de> Decode<'de> for OffsetFetchRequestTopic {
115 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
116 let flex = version >= 6;
117 let mut out = Self::default();
118 if version >= 0 && version <= 7 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
119 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 }; }
120 if flex {
121 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
122 Ok(false)
123 })?;
124 }
125 Ok(out)
126 }
127}
128
129#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct OffsetFetchRequestGroup {
131 pub group_id: String,
132 pub member_id: Option<String>,
133 pub member_epoch: i32,
134 pub topics: Option<Vec<OffsetFetchRequestTopics>>,
135 pub unknown_tagged_fields: UnknownTaggedFields,
136}
137
138impl Default for OffsetFetchRequestGroup {
139 fn default() -> Self {
140 Self {
141 group_id: String::new(),
142 member_id: None,
143 member_epoch: -1i32,
144 topics: None,
145 unknown_tagged_fields: Default::default(),
146 }
147 }
148}
149
150impl Encode for OffsetFetchRequestGroup {
151 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
152 let flex = version >= 6;
153 if version >= 8 { if flex { put_compact_string(buf, &self.group_id) } else { put_string(buf, &self.group_id) } }
154 if version >= 9 { if flex { put_compact_nullable_string(buf, self.member_id.as_deref()) } else { put_nullable_string(buf, self.member_id.as_deref()) } }
155 if version >= 9 { put_i32(buf, self.member_epoch) }
156 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)?; } } } }
157 if flex {
158 let tagged = WriteTaggedFields::new();
159 tagged.write(buf, &self.unknown_tagged_fields);
160 }
161 Ok(())
162 }
163 fn encoded_len(&self, version: i16) -> usize {
164 let flex = version >= 6;
165 let mut n: usize = 0;
166 if version >= 8 { n += if flex { compact_string_len(&self.group_id) } else { string_len(&self.group_id) }; }
167 if version >= 9 { n += if flex { compact_nullable_string_len(self.member_id.as_deref()) } else { nullable_string_len(self.member_id.as_deref()) }; }
168 if version >= 9 { n += 4; }
169 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 }; }
170 if flex {
171 let known_pairs: Vec<(u32, usize)> = Vec::new();
172 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
173 }
174 n
175 }
176}
177
178impl<'de> Decode<'de> for OffsetFetchRequestGroup {
179 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
180 let flex = version >= 6;
181 let mut out = Self::default();
182 if version >= 8 { out.group_id = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
183 if version >= 9 { out.member_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
184 if version >= 9 { out.member_epoch = get_i32(buf)?; }
185 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(buf, version)?); } Some(v) } } }; }
186 if flex {
187 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
188 Ok(false)
189 })?;
190 }
191 Ok(out)
192 }
193}
194
195#[derive(Debug, Clone, PartialEq, Eq, Default)]
196pub struct OffsetFetchRequestTopics {
197 pub name: String,
198 pub topic_id: crate::primitives::uuid::Uuid,
199 pub partition_indexes: Vec<i32>,
200 pub unknown_tagged_fields: UnknownTaggedFields,
201}
202
203impl Encode for OffsetFetchRequestTopics {
204 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
205 let flex = version >= 6;
206 if version >= 8 && version <= 9 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
207 if version >= 10 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
208 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); } } }
209 if flex {
210 let tagged = WriteTaggedFields::new();
211 tagged.write(buf, &self.unknown_tagged_fields);
212 }
213 Ok(())
214 }
215 fn encoded_len(&self, version: i16) -> usize {
216 let flex = version >= 6;
217 let mut n: usize = 0;
218 if version >= 8 && version <= 9 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
219 if version >= 10 { n += 16; }
220 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 }; }
221 if flex {
222 let known_pairs: Vec<(u32, usize)> = Vec::new();
223 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
224 }
225 n
226 }
227}
228
229impl<'de> Decode<'de> for OffsetFetchRequestTopics {
230 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
231 let flex = version >= 6;
232 let mut out = Self::default();
233 if version >= 8 && version <= 9 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
234 if version >= 10 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
235 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 }; }
236 if flex {
237 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
238 Ok(false)
239 })?;
240 }
241 Ok(out)
242 }
243}
244
245#[must_use]
248#[allow(unused_comparisons)]
249pub fn default_json(version: i16) -> ::serde_json::Value {
250 let mut obj = ::serde_json::Map::new();
251 if version <= 7 {
252 obj.insert("groupId".to_string(), ::serde_json::Value::String(String::new()));
253 }
254 if version <= 7 {
255 obj.insert("topics".to_string(), if version >= 2 && version <= 7 { ::serde_json::Value::Null } else { ::serde_json::Value::Array(vec![]) });
256 }
257 if version >= 8 {
258 obj.insert("groups".to_string(), ::serde_json::Value::Array(vec![]));
259 }
260 if version >= 7 {
261 obj.insert("requireStable".to_string(), ::serde_json::Value::Bool(false));
262 }
263 ::serde_json::Value::Object(obj)
264}
265
266impl crate::ProtocolRequest for OffsetFetchRequest {
267 const API_KEY: i16 = API_KEY;
268 const MIN_VERSION: i16 = MIN_VERSION;
269 const MAX_VERSION: i16 = MAX_VERSION;
270 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
271 type Response = super::offset_fetch_response::OffsetFetchResponse;
272}