Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
CreatePartitionsRequest.owned.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::{
6    Decode, Encode, ProtocolError, UnknownTaggedFields,
7    primitives::{
8        fixed::{get_bool, get_i32, put_bool, put_i32},
9        string_bytes::{
10            compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
11            put_string, string_len,
12        },
13    },
14    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
15};
16pub const API_KEY: i16 = 37;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 3;
19pub const LATEST_STABLE_VERSION: i16 = 3;
20pub const FLEXIBLE_MIN: i16 = 2;
21#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24    version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct CreatePartitionsRequest {
28    pub topics: Vec<CreatePartitionsTopic>,
29    pub timeout_ms: i32,
30    pub validate_only: bool,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33impl Encode for CreatePartitionsRequest {
34    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
35        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
36            return Err(ProtocolError::UnsupportedVersion {
37                api_key: API_KEY,
38                version,
39            });
40        }
41        let flex = is_flexible(version);
42        if version >= 0 {
43            {
44                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
45                for it in &self.topics {
46                    it.encode(buf, version)?;
47                }
48            };
49        }
50        if version >= 0 {
51            put_i32(buf, self.timeout_ms);
52        }
53        if version >= 0 {
54            put_bool(buf, self.validate_only);
55        }
56        if flex {
57            let tagged = WriteTaggedFields::new();
58            tagged.write(buf, &self.unknown_tagged_fields);
59        }
60        Ok(())
61    }
62    fn encoded_len(&self, version: i16) -> usize {
63        let flex = is_flexible(version);
64        let mut n: usize = 0;
65        if version >= 0 {
66            n += {
67                let prefix =
68                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
69                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
70                prefix + body
71            };
72        }
73        if version >= 0 {
74            n += 4;
75        }
76        if version >= 0 {
77            n += 1;
78        }
79        if flex {
80            let known_pairs: Vec<(u32, usize)> = Vec::new();
81            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
82        }
83        n
84    }
85}
86impl Decode<'_> for CreatePartitionsRequest {
87    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
88        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
89            return Err(ProtocolError::UnsupportedVersion {
90                api_key: API_KEY,
91                version,
92            });
93        }
94        let flex = is_flexible(version);
95        let mut out = Self::default();
96        if version >= 0 {
97            out.topics = {
98                let n = crate::primitives::array::get_array_len(buf, flex)?;
99                let mut v = Vec::with_capacity(n);
100                for _ in 0..n {
101                    v.push(CreatePartitionsTopic::decode(buf, version)?);
102                }
103                v
104            };
105        }
106        if version >= 0 {
107            out.timeout_ms = get_i32(buf)?;
108        }
109        if version >= 0 {
110            out.validate_only = get_bool(buf)?;
111        }
112        if flex {
113            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
114        }
115        Ok(out)
116    }
117}
118#[cfg(test)]
119impl CreatePartitionsRequest {
120    #[must_use]
121    pub fn populated(version: i16) -> Self {
122        let mut m = Self::default();
123        if version >= 0 {
124            m.topics = vec![CreatePartitionsTopic::populated(version)];
125        }
126        if version >= 0 {
127            m.timeout_ms = 1i32;
128        }
129        if version >= 0 {
130            m.validate_only = true;
131        }
132        m
133    }
134}
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct CreatePartitionsTopic {
137    pub name: String,
138    pub count: i32,
139    pub assignments: Option<Vec<CreatePartitionsAssignment>>,
140    pub unknown_tagged_fields: UnknownTaggedFields,
141}
142impl Default for CreatePartitionsTopic {
143    fn default() -> Self {
144        Self {
145            name: String::new(),
146            count: 0i32,
147            assignments: Some(Vec::new()),
148            unknown_tagged_fields: UnknownTaggedFields::default(),
149        }
150    }
151}
152impl Encode for CreatePartitionsTopic {
153    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
154        let flex = version >= 2;
155        if version >= 0 {
156            if flex {
157                let () = put_compact_string(buf, &self.name);
158            } else {
159                let () = put_string(buf, &self.name);
160            }
161        }
162        if version >= 0 {
163            put_i32(buf, self.count);
164        }
165        if version >= 0 {
166            {
167                let len = (self.assignments).as_ref().map(Vec::len);
168                crate::primitives::array::put_nullable_array_len(buf, len, flex);
169                if let Some(v) = &self.assignments {
170                    for it in v {
171                        it.encode(buf, version)?;
172                    }
173                }
174            };
175        }
176        if flex {
177            let tagged = WriteTaggedFields::new();
178            tagged.write(buf, &self.unknown_tagged_fields);
179        }
180        Ok(())
181    }
182    fn encoded_len(&self, version: i16) -> usize {
183        let flex = version >= 2;
184        let mut n: usize = 0;
185        if version >= 0 {
186            n += if flex {
187                compact_string_len(&self.name)
188            } else {
189                string_len(&self.name)
190            };
191        }
192        if version >= 0 {
193            n += 4;
194        }
195        if version >= 0 {
196            n += {
197                let opt: Option<&Vec<_>> = (self.assignments).as_ref();
198                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
199                    opt.map(std::vec::Vec::len),
200                    flex,
201                );
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 = crate::primitives::array::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_string();
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                crate::primitives::array::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 =
294                    crate::primitives::array::array_len_prefix_len((self.broker_ids).len(), flex);
295                let body: usize = (self.broker_ids).iter().map(|_| 4).sum();
296                prefix + body
297            };
298        }
299        if flex {
300            let known_pairs: Vec<(u32, usize)> = Vec::new();
301            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
302        }
303        n
304    }
305}
306impl Decode<'_> for CreatePartitionsAssignment {
307    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
308        let flex = version >= 2;
309        let mut out = Self::default();
310        if version >= 0 {
311            out.broker_ids = {
312                let n = crate::primitives::array::get_array_len(buf, flex)?;
313                let mut v = Vec::with_capacity(n);
314                for _ in 0..n {
315                    v.push(get_i32(buf)?);
316                }
317                v
318            };
319        }
320        if flex {
321            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
322        }
323        Ok(out)
324    }
325}
326#[cfg(test)]
327impl CreatePartitionsAssignment {
328    #[must_use]
329    pub fn populated(version: i16) -> Self {
330        let mut m = Self::default();
331        if version >= 0 {
332            m.broker_ids = vec![1i32];
333        }
334        m
335    }
336}
337/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
338/// Only includes fields valid for the given version.
339#[must_use]
340#[allow(unused_comparisons)]
341pub fn default_json(_version: i16) -> ::serde_json::Value {
342    let mut obj = ::serde_json::Map::new();
343    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
344    obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
345    obj.insert("validateOnly".to_string(), ::serde_json::Value::Bool(false));
346    ::serde_json::Value::Object(obj)
347}
348impl crate::ProtocolRequest for CreatePartitionsRequest {
349    const API_KEY: i16 = API_KEY;
350    const MIN_VERSION: i16 = MIN_VERSION;
351    const MAX_VERSION: i16 = MAX_VERSION;
352    const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
353    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
354    type Response = super::create_partitions_response::CreatePartitionsResponse;
355}