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