krabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolSubscription.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_i32, put_i32},
9 string_bytes::{
10 compact_nullable_bytes_len, compact_nullable_string_len, compact_string_len,
11 get_compact_nullable_bytes_owned, get_compact_nullable_string_owned,
12 get_compact_string_owned, get_nullable_bytes_owned, get_nullable_string_owned,
13 get_string_owned, nullable_bytes_len, nullable_string_len, put_compact_nullable_bytes,
14 put_compact_nullable_string, put_compact_string, put_nullable_bytes,
15 put_nullable_string, put_string, string_len,
16 },
17 },
18};
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 3;
21pub const FLEXIBLE_MIN: i16 = 32767;
22#[inline]
23#[must_use]
24pub fn is_flexible(version: i16) -> bool {
25 version == FLEXIBLE_MIN
26}
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct ConsumerProtocolSubscription {
29 pub topics: Vec<String>,
30 pub user_data: Option<::bytes::Bytes>,
31 pub owned_partitions: Vec<TopicPartition>,
32 pub generation_id: i32,
33 pub rack_id: Option<String>,
34 pub unknown_tagged_fields: UnknownTaggedFields,
35}
36impl Default for ConsumerProtocolSubscription {
37 fn default() -> Self {
38 Self {
39 topics: Vec::new(),
40 user_data: None,
41 owned_partitions: Vec::new(),
42 generation_id: -1i32,
43 rack_id: None,
44 unknown_tagged_fields: UnknownTaggedFields::default(),
45 }
46 }
47}
48impl Encode for ConsumerProtocolSubscription {
49 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51 return Err(ProtocolError::SchemaMismatch(
52 "ConsumerProtocolSubscription version out of range",
53 ));
54 }
55 let flex = is_flexible(version);
56 if version >= 0 {
57 {
58 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
59 for it in &self.topics {
60 if flex {
61 let () = put_compact_string(buf, it);
62 } else {
63 let () = put_string(buf, it);
64 }
65 }
66 };
67 }
68 if version >= 0 {
69 if flex {
70 let () = put_compact_nullable_bytes(buf, self.user_data.as_deref());
71 } else {
72 let () = put_nullable_bytes(buf, self.user_data.as_deref());
73 }
74 }
75 if version >= 1 {
76 {
77 crate::primitives::array::put_array_len(buf, (self.owned_partitions).len(), flex);
78 for it in &self.owned_partitions {
79 it.encode(buf, version)?;
80 }
81 };
82 }
83 if version >= 2 {
84 put_i32(buf, self.generation_id);
85 }
86 if version >= 3 {
87 if flex {
88 let () = put_compact_nullable_string(buf, self.rack_id.as_deref());
89 } else {
90 let () = put_nullable_string(buf, self.rack_id.as_deref());
91 }
92 }
93 Ok(())
94 }
95 fn encoded_len(&self, version: i16) -> usize {
96 let flex = is_flexible(version);
97 let mut n: usize = 0;
98 if version >= 0 {
99 n += {
100 let prefix =
101 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
102 let body: usize = (self.topics)
103 .iter()
104 .map(|it| {
105 if flex {
106 compact_string_len(it)
107 } else {
108 string_len(it)
109 }
110 })
111 .sum();
112 prefix + body
113 };
114 }
115 if version >= 0 {
116 n += if flex {
117 compact_nullable_bytes_len(self.user_data.as_deref())
118 } else {
119 nullable_bytes_len(self.user_data.as_deref())
120 };
121 }
122 if version >= 1 {
123 n += {
124 let prefix = crate::primitives::array::array_len_prefix_len(
125 (self.owned_partitions).len(),
126 flex,
127 );
128 let body: usize = (self.owned_partitions)
129 .iter()
130 .map(|it| it.encoded_len(version))
131 .sum();
132 prefix + body
133 };
134 }
135 if version >= 2 {
136 n += 4;
137 }
138 if version >= 3 {
139 n += if flex {
140 compact_nullable_string_len(self.rack_id.as_deref())
141 } else {
142 nullable_string_len(self.rack_id.as_deref())
143 };
144 }
145 n
146 }
147}
148impl Decode<'_> for ConsumerProtocolSubscription {
149 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
150 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
151 return Err(ProtocolError::SchemaMismatch(
152 "ConsumerProtocolSubscription version out of range",
153 ));
154 }
155 let flex = is_flexible(version);
156 let mut out = Self::default();
157 if version >= 0 {
158 out.topics = {
159 let n = crate::primitives::array::get_array_len(buf, flex)?;
160 let mut v = Vec::with_capacity(n);
161 for _ in 0..n {
162 v.push(if flex {
163 get_compact_string_owned(buf)?
164 } else {
165 get_string_owned(buf)?
166 });
167 }
168 v
169 };
170 }
171 if version >= 0 {
172 out.user_data = if flex {
173 get_compact_nullable_bytes_owned(buf)?
174 } else {
175 get_nullable_bytes_owned(buf)?
176 };
177 }
178 if version >= 1 {
179 out.owned_partitions = {
180 let n = crate::primitives::array::get_array_len(buf, flex)?;
181 let mut v = Vec::with_capacity(n);
182 for _ in 0..n {
183 v.push(TopicPartition::decode(buf, version)?);
184 }
185 v
186 };
187 }
188 if version >= 2 {
189 out.generation_id = get_i32(buf)?;
190 }
191 if version >= 3 {
192 out.rack_id = if flex {
193 get_compact_nullable_string_owned(buf)?
194 } else {
195 get_nullable_string_owned(buf)?
196 };
197 }
198 Ok(out)
199 }
200}
201#[cfg(test)]
202impl ConsumerProtocolSubscription {
203 #[must_use]
204 pub fn populated(version: i16) -> Self {
205 let mut m = Self::default();
206 if version >= 0 {
207 m.topics = vec!["x".to_string()];
208 }
209 if version >= 0 {
210 m.user_data = Some(::bytes::Bytes::from_static(b"x"));
211 }
212 if version >= 1 {
213 m.owned_partitions = vec![TopicPartition::populated(version)];
214 }
215 if version >= 2 {
216 m.generation_id = 1i32;
217 }
218 if version >= 3 {
219 m.rack_id = Some("x".to_string());
220 }
221 m
222 }
223}
224#[derive(Debug, Clone, PartialEq, Eq, Default)]
225pub struct TopicPartition {
226 pub topic: String,
227 pub partitions: Vec<i32>,
228 pub unknown_tagged_fields: UnknownTaggedFields,
229}
230impl Encode for TopicPartition {
231 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
232 let flex = version == i16::MAX;
233 if version >= 1 {
234 if flex {
235 let () = put_compact_string(buf, &self.topic);
236 } else {
237 let () = put_string(buf, &self.topic);
238 }
239 }
240 if version >= 1 {
241 {
242 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
243 for it in &self.partitions {
244 put_i32(buf, *it);
245 }
246 };
247 }
248 Ok(())
249 }
250 fn encoded_len(&self, version: i16) -> usize {
251 let flex = version == i16::MAX;
252 let mut n: usize = 0;
253 if version >= 1 {
254 n += if flex {
255 compact_string_len(&self.topic)
256 } else {
257 string_len(&self.topic)
258 };
259 }
260 if version >= 1 {
261 n += {
262 let prefix =
263 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
264 let body: usize = (self.partitions).iter().map(|_| 4).sum();
265 prefix + body
266 };
267 }
268 n
269 }
270}
271impl Decode<'_> for TopicPartition {
272 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
273 let flex = version == i16::MAX;
274 let mut out = Self::default();
275 if version >= 1 {
276 out.topic = if flex {
277 get_compact_string_owned(buf)?
278 } else {
279 get_string_owned(buf)?
280 };
281 }
282 if version >= 1 {
283 out.partitions = {
284 let n = crate::primitives::array::get_array_len(buf, flex)?;
285 let mut v = Vec::with_capacity(n);
286 for _ in 0..n {
287 v.push(get_i32(buf)?);
288 }
289 v
290 };
291 }
292 Ok(out)
293 }
294}
295#[cfg(test)]
296impl TopicPartition {
297 #[must_use]
298 pub fn populated(version: i16) -> Self {
299 let mut m = Self::default();
300 if version >= 1 {
301 m.topic = "x".to_string();
302 }
303 if version >= 1 {
304 m.partitions = vec![1i32];
305 }
306 m
307 }
308}
309#[must_use]
312#[allow(unused_comparisons)]
313pub fn default_json(version: i16) -> ::serde_json::Value {
314 let mut obj = ::serde_json::Map::new();
315 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
316 obj.insert("userData".to_string(), ::serde_json::Value::Null);
317 if version >= 1 {
318 obj.insert(
319 "ownedPartitions".to_string(),
320 ::serde_json::Value::Array(vec![]),
321 );
322 }
323 if version >= 2 {
324 obj.insert("generationId".to_string(), ::serde_json::json!(-1));
325 }
326 if version >= 3 {
327 obj.insert("rackId".to_string(), ::serde_json::Value::Null);
328 }
329 ::serde_json::Value::Object(obj)
330}