Skip to main content

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