Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolSubscription.borrowed.rs

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