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