Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolAssignment.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_string_len, put_compact_nullable_bytes, put_compact_string, put_nullable_bytes,
10            put_string, string_len,
11        },
12        string_bytes_borrowed::{
13            get_compact_nullable_bytes_borrowed, get_compact_string_borrowed,
14            get_nullable_bytes_borrowed, get_string_borrowed,
15        },
16    },
17};
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 3;
20pub const FLEXIBLE_MIN: i16 = 32767;
21#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24    version == FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct ConsumerProtocolAssignment<'a> {
28    pub assigned_partitions: Vec<TopicPartition<'a>>,
29    pub user_data: Option<&'a [u8]>,
30    pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl ConsumerProtocolAssignment<'_> {
33    /// # Panics
34    ///
35    /// Panics if a records field contains an invalid encoded record batch.
36    #[must_use]
37    pub fn to_owned(
38        &self,
39    ) -> crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
40        crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
41            assigned_partitions: (self.assigned_partitions)
42                .iter()
43                .map(TopicPartition::to_owned)
44                .collect(),
45            user_data: (self.user_data).map(Bytes::copy_from_slice),
46            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
47        }
48    }
49}
50impl Encode for ConsumerProtocolAssignment<'_> {
51    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
52        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
53            return Err(ProtocolError::SchemaMismatch(
54                "ConsumerProtocolAssignment version out of range",
55            ));
56        }
57        let flex = is_flexible(version);
58        if version >= 0 {
59            {
60                crate::primitives::array::put_array_len(
61                    buf,
62                    (self.assigned_partitions).len(),
63                    flex,
64                );
65                for it in &self.assigned_partitions {
66                    it.encode(buf, version)?;
67                }
68            };
69        }
70        if version >= 0 {
71            if flex {
72                let () = put_compact_nullable_bytes(buf, self.user_data);
73            } else {
74                let () = put_nullable_bytes(buf, self.user_data);
75            }
76        }
77        Ok(())
78    }
79    fn encoded_len(&self, version: i16) -> usize {
80        let flex = is_flexible(version);
81        let mut n: usize = 0;
82        if version >= 0 {
83            n += {
84                let prefix = crate::primitives::array::array_len_prefix_len(
85                    (self.assigned_partitions).len(),
86                    flex,
87                );
88                let body: usize = (self.assigned_partitions)
89                    .iter()
90                    .map(|it| it.encoded_len(version))
91                    .sum();
92                prefix + body
93            };
94        }
95        if version >= 0 {
96            n += match self.user_data {
97                None => {
98                    if flex {
99                        1
100                    } else {
101                        4
102                    }
103                }
104                Some(b) => {
105                    if flex {
106                        crate::primitives::varint::uvarint_len(u32::try_from(b.len() + 1).unwrap())
107                            + b.len()
108                    } else {
109                        4 + b.len()
110                    }
111                }
112            };
113        }
114        n
115    }
116}
117impl<'de> DecodeBorrow<'de> for ConsumerProtocolAssignment<'de> {
118    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
119        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
120            return Err(ProtocolError::SchemaMismatch(
121                "ConsumerProtocolAssignment version out of range",
122            ));
123        }
124        let flex = is_flexible(version);
125        let mut out = Self::default();
126        if version >= 0 {
127            out.assigned_partitions = {
128                let n = crate::primitives::array::get_array_len(buf, flex)?;
129                let mut v = Vec::with_capacity(n);
130                for _ in 0..n {
131                    v.push(TopicPartition::decode_borrow(buf, version)?);
132                }
133                v
134            };
135        }
136        if version >= 0 {
137            out.user_data = if flex {
138                get_compact_nullable_bytes_borrowed(buf)?
139            } else {
140                get_nullable_bytes_borrowed(buf)?
141            };
142        }
143        Ok(out)
144    }
145}
146#[cfg(test)]
147impl ConsumerProtocolAssignment<'_> {
148    #[must_use]
149    pub fn populated(version: i16) -> Self {
150        let mut m = Self::default();
151        if version >= 0 {
152            m.assigned_partitions = vec![TopicPartition::populated(version)];
153        }
154        if version >= 0 {
155            m.user_data = Some(&b"x"[..]);
156        }
157        m
158    }
159}
160#[derive(Debug, Clone, PartialEq, Eq, Default)]
161pub struct TopicPartition<'a> {
162    pub topic: &'a str,
163    pub partitions: Vec<i32>,
164    pub unknown_tagged_fields: UnknownTaggedFields,
165}
166impl TopicPartition<'_> {
167    /// # Panics
168    ///
169    /// Panics if a records field contains an invalid encoded record batch.
170    #[must_use]
171    pub fn to_owned(&self) -> crate::owned::consumer_protocol_assignment::TopicPartition {
172        crate::owned::consumer_protocol_assignment::TopicPartition {
173            topic: (self.topic).to_string(),
174            partitions: (self.partitions).clone(),
175            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
176        }
177    }
178}
179impl Encode for TopicPartition<'_> {
180    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
181        let flex = version == i16::MAX;
182        if version >= 0 {
183            if flex {
184                let () = put_compact_string(buf, self.topic);
185            } else {
186                let () = put_string(buf, self.topic);
187            }
188        }
189        if version >= 0 {
190            {
191                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
192                for it in &self.partitions {
193                    put_i32(buf, *it);
194                }
195            };
196        }
197        Ok(())
198    }
199    fn encoded_len(&self, version: i16) -> usize {
200        let flex = version == i16::MAX;
201        let mut n: usize = 0;
202        if version >= 0 {
203            n += if flex {
204                compact_string_len(self.topic)
205            } else {
206                string_len(self.topic)
207            };
208        }
209        if version >= 0 {
210            n += {
211                let prefix =
212                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
213                let body: usize = (self.partitions).iter().map(|_| 4).sum();
214                prefix + body
215            };
216        }
217        n
218    }
219}
220impl<'de> DecodeBorrow<'de> for TopicPartition<'de> {
221    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
222        let flex = version == i16::MAX;
223        let mut out = Self::default();
224        if version >= 0 {
225            out.topic = if flex {
226                get_compact_string_borrowed(buf)?
227            } else {
228                get_string_borrowed(buf)?
229            };
230        }
231        if version >= 0 {
232            out.partitions = {
233                let n = crate::primitives::array::get_array_len(buf, flex)?;
234                let mut v = Vec::with_capacity(n);
235                for _ in 0..n {
236                    v.push(get_i32(buf)?);
237                }
238                v
239            };
240        }
241        Ok(out)
242    }
243}
244#[cfg(test)]
245impl TopicPartition<'_> {
246    #[must_use]
247    pub fn populated(version: i16) -> Self {
248        let mut m = Self::default();
249        if version >= 0 {
250            m.topic = "x";
251        }
252        if version >= 0 {
253            m.partitions = vec![1i32];
254        }
255        m
256    }
257}