Skip to main content

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

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