krabka_protocol/opt/rustwide/workdir/generated/
LeaveGroupRequest.borrowed.rs1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 string_bytes::{
8 compact_nullable_string_len, compact_string_len, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10 string_len,
11 },
12 string_bytes_borrowed::{
13 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
14 get_nullable_string_borrowed, get_string_borrowed,
15 },
16 },
17 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
18};
19pub const API_KEY: i16 = 13;
20pub const MIN_VERSION: i16 = 0;
21pub const MAX_VERSION: i16 = 5;
22pub const LATEST_STABLE_VERSION: i16 = 5;
23pub const FLEXIBLE_MIN: i16 = 4;
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 LeaveGroupRequest<'a> {
31 pub group_id: &'a str,
32 pub member_id: &'a str,
33 pub members: Vec<MemberIdentity<'a>>,
34 pub unknown_tagged_fields: UnknownTaggedFields,
35}
36impl LeaveGroupRequest<'_> {
37 #[must_use]
41 pub fn to_owned(&self) -> crate::owned::leave_group_request::LeaveGroupRequest {
42 crate::owned::leave_group_request::LeaveGroupRequest {
43 group_id: (self.group_id).to_string(),
44 member_id: (self.member_id).to_string(),
45 members: (self.members)
46 .iter()
47 .map(MemberIdentity::to_owned)
48 .collect(),
49 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
50 }
51 }
52}
53impl Encode for LeaveGroupRequest<'_> {
54 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
55 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56 return Err(ProtocolError::UnsupportedVersion {
57 api_key: API_KEY,
58 version,
59 });
60 }
61 let flex = is_flexible(version);
62 if version >= 0 {
63 if flex {
64 let () = put_compact_string(buf, self.group_id);
65 } else {
66 let () = put_string(buf, self.group_id);
67 }
68 }
69 if (0..=2).contains(&version) {
70 if flex {
71 let () = put_compact_string(buf, self.member_id);
72 } else {
73 let () = put_string(buf, self.member_id);
74 }
75 }
76 if version >= 3 {
77 {
78 crate::primitives::array::put_array_len(buf, (self.members).len(), flex);
79 for it in &self.members {
80 it.encode(buf, version)?;
81 }
82 };
83 }
84 if flex {
85 let tagged = WriteTaggedFields::new();
86 tagged.write(buf, &self.unknown_tagged_fields);
87 }
88 Ok(())
89 }
90 fn encoded_len(&self, version: i16) -> usize {
91 let flex = is_flexible(version);
92 let mut n: usize = 0;
93 if version >= 0 {
94 n += if flex {
95 compact_string_len(self.group_id)
96 } else {
97 string_len(self.group_id)
98 };
99 }
100 if (0..=2).contains(&version) {
101 n += if flex {
102 compact_string_len(self.member_id)
103 } else {
104 string_len(self.member_id)
105 };
106 }
107 if version >= 3 {
108 n += {
109 let prefix =
110 crate::primitives::array::array_len_prefix_len((self.members).len(), flex);
111 let body: usize = (self.members)
112 .iter()
113 .map(|it| it.encoded_len(version))
114 .sum();
115 prefix + body
116 };
117 }
118 if flex {
119 let known_pairs: Vec<(u32, usize)> = Vec::new();
120 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
121 }
122 n
123 }
124}
125impl<'de> DecodeBorrow<'de> for LeaveGroupRequest<'de> {
126 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
127 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
128 return Err(ProtocolError::UnsupportedVersion {
129 api_key: API_KEY,
130 version,
131 });
132 }
133 let flex = is_flexible(version);
134 let mut out = Self::default();
135 if version >= 0 {
136 out.group_id = if flex {
137 get_compact_string_borrowed(buf)?
138 } else {
139 get_string_borrowed(buf)?
140 };
141 }
142 if (0..=2).contains(&version) {
143 out.member_id = if flex {
144 get_compact_string_borrowed(buf)?
145 } else {
146 get_string_borrowed(buf)?
147 };
148 }
149 if version >= 3 {
150 out.members = {
151 let n = crate::primitives::array::get_array_len(buf, flex)?;
152 let mut v = Vec::with_capacity(n);
153 for _ in 0..n {
154 v.push(MemberIdentity::decode_borrow(buf, version)?);
155 }
156 v
157 };
158 }
159 if flex {
160 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
161 }
162 Ok(out)
163 }
164}
165#[cfg(test)]
166impl LeaveGroupRequest<'_> {
167 #[must_use]
168 pub fn populated(version: i16) -> Self {
169 let mut m = Self::default();
170 if version >= 0 {
171 m.group_id = "x";
172 }
173 if (0..=2).contains(&version) {
174 m.member_id = "x";
175 }
176 if version >= 3 {
177 m.members = vec![MemberIdentity::populated(version)];
178 }
179 m
180 }
181}
182#[derive(Debug, Clone, PartialEq, Eq, Default)]
183pub struct MemberIdentity<'a> {
184 pub member_id: &'a str,
185 pub group_instance_id: Option<&'a str>,
186 pub reason: Option<&'a str>,
187 pub unknown_tagged_fields: UnknownTaggedFields,
188}
189impl MemberIdentity<'_> {
190 #[must_use]
194 pub fn to_owned(&self) -> crate::owned::leave_group_request::MemberIdentity {
195 crate::owned::leave_group_request::MemberIdentity {
196 member_id: (self.member_id).to_string(),
197 group_instance_id: (self.group_instance_id).map(std::string::ToString::to_string),
198 reason: (self.reason).map(std::string::ToString::to_string),
199 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
200 }
201 }
202}
203impl Encode for MemberIdentity<'_> {
204 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
205 let flex = version >= 4;
206 if version >= 3 {
207 if flex {
208 let () = put_compact_string(buf, self.member_id);
209 } else {
210 let () = put_string(buf, self.member_id);
211 }
212 }
213 if version >= 3 {
214 if flex {
215 let () = put_compact_nullable_string(buf, self.group_instance_id);
216 } else {
217 let () = put_nullable_string(buf, self.group_instance_id);
218 }
219 }
220 if version >= 5 {
221 if flex {
222 let () = put_compact_nullable_string(buf, self.reason);
223 } else {
224 let () = put_nullable_string(buf, self.reason);
225 }
226 }
227 if flex {
228 let tagged = WriteTaggedFields::new();
229 tagged.write(buf, &self.unknown_tagged_fields);
230 }
231 Ok(())
232 }
233 fn encoded_len(&self, version: i16) -> usize {
234 let flex = version >= 4;
235 let mut n: usize = 0;
236 if version >= 3 {
237 n += if flex {
238 compact_string_len(self.member_id)
239 } else {
240 string_len(self.member_id)
241 };
242 }
243 if version >= 3 {
244 n += if flex {
245 compact_nullable_string_len(self.group_instance_id)
246 } else {
247 nullable_string_len(self.group_instance_id)
248 };
249 }
250 if version >= 5 {
251 n += if flex {
252 compact_nullable_string_len(self.reason)
253 } else {
254 nullable_string_len(self.reason)
255 };
256 }
257 if flex {
258 let known_pairs: Vec<(u32, usize)> = Vec::new();
259 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
260 }
261 n
262 }
263}
264impl<'de> DecodeBorrow<'de> for MemberIdentity<'de> {
265 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
266 let flex = version >= 4;
267 let mut out = Self::default();
268 if version >= 3 {
269 out.member_id = if flex {
270 get_compact_string_borrowed(buf)?
271 } else {
272 get_string_borrowed(buf)?
273 };
274 }
275 if version >= 3 {
276 out.group_instance_id = if flex {
277 get_compact_nullable_string_borrowed(buf)?
278 } else {
279 get_nullable_string_borrowed(buf)?
280 };
281 }
282 if version >= 5 {
283 out.reason = if flex {
284 get_compact_nullable_string_borrowed(buf)?
285 } else {
286 get_nullable_string_borrowed(buf)?
287 };
288 }
289 if flex {
290 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
291 }
292 Ok(out)
293 }
294}
295#[cfg(test)]
296impl MemberIdentity<'_> {
297 #[must_use]
298 pub fn populated(version: i16) -> Self {
299 let mut m = Self::default();
300 if version >= 3 {
301 m.member_id = "x";
302 }
303 if version >= 3 {
304 m.group_instance_id = Some("x");
305 }
306 if version >= 5 {
307 m.reason = Some("x");
308 }
309 m
310 }
311}