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