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