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