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