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