Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolSubscription.owned.rs

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